public async Task <Response <TResult> > Execute <TApi, TResult>(Expression <Func <TApi, Task <TResult> > > executeApiMethod)
        {
            if (!_refitCacheController.IsMethodCacheable(executeApiMethod))
            {
                return(await _decoratedRestService.Execute(executeApiMethod).ConfigureAwait(false));
            }

            var cacheKey    = _refitCacheController.GetCacheKey(executeApiMethod);
            var cachedValue = await persistedCache.Get <TResult>(cacheKey);

            if (cachedValue != null)
            {
                return(new Response <TResult>(cachedValue));
            }

            var restResponse = await _decoratedRestService.Execute(executeApiMethod);

            if (restResponse.IsSuccess)
            {
                var refitCacheAttributes = _refitCacheController.GetRefitCacheAttribute <TApi, TResult>(executeApiMethod);

                await persistedCache.Save(cacheKey, restResponse.Results, refitCacheAttributes.CacheAttribute.CacheTtl);
            }

            return(restResponse);
        }
Exemplo n.º 2
0
        public Task UpdateCache<TApi, TResult>(Expression<Func<TApi, Task<TResult>>> forApiMethodCall, TResult newCacheValue)
        {
            if (!_refitCacheController.IsMethodCacheable(forApiMethodCall))
                return Task.FromResult(true);

            var cacheKey = _refitCacheController.GetCacheKey(forApiMethodCall);
            var refitCacheAttribute = _refitCacheController.GetRefitCacheAttribute(forApiMethodCall);

            return persistedCache.Save(cacheKey, newCacheValue, refitCacheAttribute.CacheAttribute.CacheTtl);
        }