Example #1
0
        public async Task <QueryResult <TResponse> > Handle(TQuery query, RequestContext context)
        {
            var queryInfo = queryInfoProvider.GetQueryInfo(query.GetType().Name);

            // no way but lets check. no need to break the call.
            if (queryInfo == null || !queryInfo.HasCache)
            {
                //  log critical ?
                return(await decoratedRequestHandler.Handle(query, context));
            }

            string cacheEntryKey = "";

            cacheEntryKey = GetCacheEntryKey(query, context, queryInfo);

            var result = await distributedCache.GetOrAddAsync(cacheEntryKey, async entry =>
            {
                var getResult = await decoratedRequestHandler.Handle(query, context);

                if (getResult.HasError)
                {
                    throw new Exception($"Cache handler {getResult.ErrosToString()}");
                }

                if (queryInfo.CacheOption.ExpireInSeconds > 0)
                {
                    entry.SetAbsoluteExpiration(new DateTimeOffset(DateTime.Now.AddSeconds(queryInfo.CacheOption.ExpireInSeconds)));
                }

                return(getResult.Result);
            });

            return(new QueryResult <TResponse>(result));
        }
        public async Task <QueryResult <TResponse> > Handle(TQuery query, RequestContext context)
        {
            logger.LogDebug("Cache decorator starting");

            var queryInfo = queryInfoProvider.GetQueryInfo(query.GetType().Name);

            logger.LogDebug($"Query info query name is  {queryInfo?.Name}");

            // no way but lets check. no need to break the call.
            if (queryInfo == null || !queryInfo.HasCache)
            {
                logger.LogDebug($"Query info is null or query info has cache is {queryInfo?.HasCache}");

                return(await decoratedRequestHandler.Handle(query, context));
            }

            string cacheEntryKey = "";

            logger.LogDebug($"Getting CacheEntryKey");

            cacheEntryKey = GetCacheEntryKey(query, context, queryInfo);

            logger.LogDebug($"CacheEntryKey getted successfully: {cacheEntryKey}");

            var result = await memoryCache.GetOrAddAsync <TResponse>(cacheEntryKey, async entry =>
            {
                var getResult = await decoratedRequestHandler.Handle(query, context);

                if (getResult.HasError)
                {
                    throw new Exception($"Cache handler error.Error:{getResult.ErrorsToString()}");
                }

                if (getResult.Code == Messages.NotFound)
                {
                    throw new Exception($"No record found to cache");
                }

                if (queryInfo.CacheOption.ExpireInSeconds > 0)
                {
                    entry.SetAbsoluteExpiration(new DateTimeOffset(DateTime.Now.AddSeconds(queryInfo.CacheOption.ExpireInSeconds)));
                }

                return(getResult.Result);
            });

            return(new QueryResult <TResponse>(result));
        }