Пример #1
0
        protected virtual async Task SetContentToCacheAsync(
            ZeusCachingContext context,
            ZeusCachingProfileOptions options,
            string key,
            object content,
            ICachingAdapter cachingAdapter)
        {
            object wrappedContent;

            if (options.WrappingResultHandler != null)
            {
                wrappedContent = options.WrappingResultHandler(_serviceProvider, context.ExecutionContext.HttpContext, content);
            }
            else
            {
                wrappedContent = content;
            }

            if (wrappedContent is string)
            {
                await cachingAdapter.SetContentAsync(key, (string)wrappedContent, context.SlidingExpiration, null, context.AbsoluteExpirationRelativeToNow);
            }
            else
            {
                // Serialize the response
                var json = Serializer(wrappedContent);
                await cachingAdapter.SetContentAsync(key, json, context.SlidingExpiration, null, context.AbsoluteExpirationRelativeToNow);
            }
        }
Пример #2
0
        public AutoDiscoveryCachingAdapter(IServiceProvider serviceProvider)
        {
            var distributedCache = serviceProvider.GetService(typeof(IDistributedCache)) as IDistributedCache;

            if (distributedCache != null)
            {
                _adapter = new DistributedCachingAdapter(distributedCache);
            }
            else
            {
                var inMemoeryCache = serviceProvider.GetService(typeof(IMemoryCache)) as IMemoryCache;
                if (inMemoeryCache != null)
                {
                    _adapter = new MemoryCachingAdapter(inMemoeryCache);
                }
                else
                {
                    throw new InvalidOperationException("No cacing service is registered.");
                }
            }
        }