Exemplo n.º 1
0
        protected async Task <T> ExecuteAsync <T>(Func <Task <T> > func, int cachedInMinutes, params object[] args)
        {
            if (!AllowCached)
            {
                return(await func.Invoke());
            }

            string cacheKey = CreateCacheKey(func, args);
            T      obj      = await cacheClient.GetAsync <T>(cacheKey, _httpContextAccessor.HttpContext);

            if (obj == null || EqualityComparer <T> .Default.Equals(obj, default(T)))
            {
                obj = await func.Invoke();

                if (obj != null && !EqualityComparer <T> .Default.Equals(obj, default(T)))
                {
                    cachedInMinutes = cachedInMinutes > 0 ? cachedInMinutes : shortCacheDuration;
                    await cacheClient.SetAsync(cacheKey, obj, cachedInMinutes);
                }
            }

            return(obj);
        }