Example #1
0
        private Task <Result <TData> > ExecuteCacheableQueryAsync <TData>(ICacheableQuery <TData> query)
        {
            var queryHandlerType = typeof(CacheableQueryHandler <,>).MakeGenericType(query.GetType(), typeof(TData));
            var queryHandler     = _serviceProvider.GetService(queryHandlerType);

            if (queryHandler == null)
            {
                _logger.LogError($"Handler not found for type {queryHandlerType.FullName}");
                return(Task.FromResult(new Result <TData>(SystemErrorCodes.SystemError.AsError())));
            }
            var methodInfo = queryHandlerType.GetMethod(nameof(CacheableQueryHandler <ICacheableQuery <TData>, TData> .ExecuteAsync),
                                                        BindingFlags.Public | BindingFlags.Instance);

            Check.NotNull(methodInfo, nameof(CacheableQueryHandler <ICacheableQuery <TData>, TData> .ExecuteAsync));

            return((Task <Result <TData> >)methodInfo.Invoke(queryHandler, new object[] { query }));
        }
        private Task AddToCache(CancellationToken cancellationToken,
                                TResponse response,
                                ICacheableQuery cacheableQuery)
        {
            var cacheOptions = new DistributedCacheEntryOptions
            {
                SlidingExpiration = cacheableQuery.SlidingExpirationMinutes.HasValue
                                        ? TimeSpan.FromMinutes(cacheableQuery.SlidingExpirationMinutes.Value)
                                        : _options.Value.SlidingExpirationMilliseconds.HasValue
                                                ? (TimeSpan?)TimeSpan.FromMilliseconds(_options.Value.SlidingExpirationMilliseconds.Value)
                                                : null,
                AbsoluteExpiration = cacheableQuery.AbsoluteExpirationMinutes.HasValue
                                        ? _dateTimeService.UtcNowOffset.AddMinutes(cacheableQuery.AbsoluteExpirationMinutes
                                                                                   .Value)
                                        : _options.Value.AbsoluteExpirationMilliseconds.HasValue
                                                ? (DateTimeOffset?)_dateTimeService.UtcNowOffset.AddMilliseconds(_options.Value
                                                                                                                 .AbsoluteExpirationMilliseconds
                                                                                                                 .Value)
                                                : null
            };

            return(_cache.SetAsync(cacheableQuery.CacheKey, _byteSerializer.Serialize(response), cacheOptions,
                                   cancellationToken));
        }