Inheritance: IHttpResult, IStreamWriter, IPartialWriter
Ejemplo n.º 1
0
        public static async Task <object> ResolveFromCacheAsync(this ICacheClientAsync cache, string cacheKey, IRequest req,
                                                                CancellationToken token = default)
        {
            var checkModifiedSince = GetIfModifiedSince(req);

            if (!req.ResponseContentType.IsBinary())
            {
                string modifiers = null;
                if (req.ResponseContentType == MimeTypes.Json)
                {
                    string jsonp = req.GetJsonpCallback();
                    if (jsonp != null)
                    {
                        modifiers = ".jsonp," + jsonp.SafeVarName();
                    }
                }

                var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers);

                var  compressionType = req.GetCompressionType();
                bool doCompression   = compressionType != null;
                if (doCompression)
                {
                    var cacheKeySerializedZip = GetCacheKeyForCompressed(cacheKeySerialized, compressionType);

                    var validCache = await cache.HasValidCacheAsync(req, cacheKeySerializedZip, checkModifiedSince, token).ConfigAwait();

                    if (validCache.IsValid)
                    {
                        return(HttpResult.NotModified());
                    }

                    DateTime?lastModified = validCache.LastModified;
                    if (req.Response.GetHeader(HttpHeaders.CacheControl) != null)
                    {
                        lastModified = null;
                    }

                    var compressedResult = await cache.GetAsync <byte[]>(cacheKeySerializedZip, token).ConfigAwait();

                    if (compressedResult != null)
                    {
                        return(new CompressedResult(
                                   compressedResult,
                                   compressionType,
                                   req.ResponseContentType)
                        {
                            LastModified = lastModified,
                        });
                    }
                }
                else
                {
                    if ((await cache.HasValidCacheAsync(req, cacheKeySerialized, checkModifiedSince, token).ConfigAwait()).IsValid)
                    {
                        return(HttpResult.NotModified());
                    }

                    var serializedResult = await cache.GetAsync <string>(cacheKeySerialized, token).ConfigAwait();

                    if (serializedResult != null)
                    {
                        return(serializedResult);
                    }
                }
            }
            else
            {
                var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers: null);
                if ((await cache.HasValidCacheAsync(req, cacheKeySerialized, checkModifiedSince, token).ConfigAwait()).IsValid)
                {
                    return(HttpResult.NotModified());
                }

                var serializedResult = await cache.GetAsync <byte[]>(cacheKeySerialized, token).ConfigAwait();

                if (serializedResult != null)
                {
                    return(serializedResult);
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        public static object ResolveFromCache(this ICacheClient cache, string cacheKey, IRequest req)
        {
            var checkModifiedSince = GetIfModifiedSince(req);

            if (!req.ResponseContentType.IsBinary())
            {
                string modifiers = null;
                if (req.ResponseContentType == MimeTypes.Json)
                {
                    string jsonp = req.GetJsonpCallback();
                    if (jsonp != null)
                    {
                        modifiers = ".jsonp," + jsonp.SafeVarName();
                    }
                }

                var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers);

                var  compressionType = req.GetCompressionType();
                bool doCompression   = compressionType != null;
                if (doCompression)
                {
                    var cacheKeySerializedZip = GetCacheKeyForCompressed(cacheKeySerialized, compressionType);

                    if (cache.HasValidCache(req, cacheKeySerializedZip, checkModifiedSince, out var lastModified))
                    {
                        return(HttpResult.NotModified());
                    }

                    if (req.Response.GetHeader(HttpHeaders.CacheControl) != null)
                    {
                        lastModified = null;
                    }

                    var compressedResult = cache.Get <byte[]>(cacheKeySerializedZip);
                    if (compressedResult != null)
                    {
                        return(new CompressedResult(
                                   compressedResult,
                                   compressionType,
                                   req.ResponseContentType)
                        {
                            LastModified = lastModified,
                        });
                    }
                }
                else
                {
                    if (cache.HasValidCache(req, cacheKeySerialized, checkModifiedSince, out _))
                    {
                        return(HttpResult.NotModified());
                    }

                    var serializedResult = cache.Get <string>(cacheKeySerialized);
                    if (serializedResult != null)
                    {
                        return(serializedResult);
                    }
                }
            }
            else
            {
                var cacheKeySerialized = GetCacheKeyForSerialized(cacheKey, req.ResponseContentType, modifiers: null);
                if (cache.HasValidCache(req, cacheKeySerialized, checkModifiedSince, out _))
                {
                    return(HttpResult.NotModified());
                }

                var serializedResult = cache.Get <byte[]>(cacheKeySerialized);
                if (serializedResult != null)
                {
                    return(serializedResult);
                }
            }

            return(null);
        }