Caching web API Get方法上的IDistributedCache

Caching web API Get方法上的IDistributedCache,caching,Caching,我需要一些帮助如何在异步调用中执行下面的缓存。缓存被注入,我只需要在任务调用中实现它 public SvcBooking( DbContextSMATA ctx, IMapper mapper, IDistributedCache cache) { _ctx = ctx; _mapper = mapper; _Bookings = ctx.Set<Booking>();

我需要一些帮助如何在异步调用中执行下面的缓存。缓存被注入,我只需要在任务调用中实现它

 public SvcBooking(
        DbContextSMATA ctx,
        IMapper mapper,
        IDistributedCache cache)
    {
        _ctx = ctx;
        _mapper = mapper;
        _Bookings = ctx.Set<Booking>();
        _cache = cache;
    }

    #endregion

    #region Public Methods


    public async Task<IList<IDtoBooking>> GetAll()
    {

        //      _cache.GetAsync() not sure how to do this

        IList<Booking> settings = await _Bookings.ToListAsync();

        return _mapper.Map<IList<IDtoBooking>>(settings);
    }

找到了一种方法。。哎呀

public async Task<IList<Booking>> GetAll()
{

    //First assign a KEY (anything that you think of that makes
    // sense to the application and Unique)
     IList<Booking> bookings;
     byte[] bookingsInBytes= await _cache.GetAsync($"bookingsSampleKey");

    if (bookingsSampleKey== null)
     {

    bookings= await _Bookings.ToListAsync();

     //Now we are caching here, the data is saved into cache so that when a 
     //concurrent user tries to RE-query it within 60 seconds
     //, it would go to the ELSE below

     bookings= await _svcBooking.GetAll();                 

     string bookingsSerialized= JsonConvert.SerializeObject(bookings);

     bookingsInBytes= Encoding.UTF8.GetBytes(bookingsSerialized);

   await _cache.SetAsync($"bookingsSampleKey", bookingsInBytes, 
   new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = 
   TimeSpan.FromSeconds(60) });


    }
    else
    {
     //if I see some DATA already cached, I will retrieve it and return it 
     //instead of hitting the inner service   

     bookings= JsonConvert.DeserializeObject<IList<Booking>>(Encoding.UTF8.GetString(bookingsInBytes));


            }
  return bookings;
}