Beispiel #1
0
 /// <summary>Seta um objeto do tipo TResult, onde o nome da chave é o seu Nome completo do tipo</summary>
 /// <typeparam name="TValue">O tipo do objeto a ser inserido</typeparam>
 /// <param name="cacheService">Serviço de cache</param>
 /// <param name="objectValue">O valor do objeto a ser inserido na cache</param>
 /// <param name="timeTimeLive">Tempo de expiração do objeto</param>
 public static Task <bool> SetCacheResponseAsync <TValue>(
     this IResponseCacheService cacheService,
     TValue objectValue,
     TimeSpan?timeTimeLive
     )
 => cacheService
 .SetCacheResponseAsync(typeof(TValue).FullName, objectValue, timeTimeLive);
Beispiel #2
0
        /// <summary>Seta um objeto do tipo TResult, onde o nome da chave é o seu Nome completo do tipo</summary>
        /// <typeparam name="TValue">O tipo do objeto a ser inserido</typeparam>
        /// <param name="cacheService">Serviço de cache</param>
        /// <param name="cacheKey">Nome da chave de cache</param>
        /// <param name="objectValue">O valor do objeto a ser inserido na cache</param>
        /// <param name="timeTimeLive">Tempo de expiração do objeto</param>
        public static Task <bool> SetCacheResponseAsync <TValue>(
            this IResponseCacheService cacheService,
            string cacheKey,
            TValue objectValue,
            TimeSpan?timeTimeLive
            )
        {
            if (objectValue == null)
            {
                return(Task.FromResult(false));
            }

            return(cacheService
                   .SetCacheResponseAsync(cacheKey, JsonSerializer.Serialize(objectValue), timeTimeLive));
        }
Beispiel #3
0
        public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            var cachedConfigurationProvider = context.ServiceProvider.GetService <IOptionsMonitor <FilterCachedConfiguration> >();
            var configCache = context.GetCacheConfigurationByMethodName(cachedConfigurationProvider.CurrentValue);

            var methodReturnType = context.ProxyMethod.ReturnType;

            if (
                configCache == null ||
                methodReturnType == typeof(void) ||
                methodReturnType == typeof(Task) ||
                methodReturnType == typeof(ValueTask)
                )
            {
                await next(context);

                return;
            }

            if (string.IsNullOrWhiteSpace(CacheName))
            {
                CacheName = context.GetGenerateKeyByMethodNameAndValues();
            }

            ResponseCacheService = context.ServiceProvider.GetService <IResponseCacheService>();

            var returnType = context.IsAsync() ? methodReturnType.GenericTypeArguments.FirstOrDefault() : methodReturnType;

            var cachedValue = await ResponseCacheService.GetCachedResponseAsync(CacheName, returnType);

            if (cachedValue != null)
            {
                context.SetReturnType(methodReturnType, cachedValue);
                return;
            }

            await next(context);

            await ResponseCacheService
            .SetCacheResponseAsync(
                CacheName,
                await context.GetReturnValueAsync(),
                TimeSpan.FromSeconds(configCache.TimeToLiveSeconds ?? TimeToLiveSeconds)
                );
        }