NotModified() public static méthode

public static NotModified ( string description = null, CacheControl cacheControl = null, System.TimeSpan maxAge = null, string eTag = null, System.DateTime lastModified = null ) : HttpResult
description string
cacheControl CacheControl
maxAge System.TimeSpan
eTag string
lastModified System.DateTime
Résultat HttpResult
        public static object ResolveFromCache(this ICacheClient cacheClient,
                                              string cacheKey,
                                              IRequest request)
        {
            DateTime?lastModified;
            var      checkModifiedSince = CheckModifiedSince(request);

            string modifiers = null;

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

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

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

                    if (cacheClient.HasValidCache(request, cacheKeySerializedZip, checkModifiedSince, out lastModified))
                    {
                        return(HttpResult.NotModified());
                    }

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

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

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

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

            return(null);
        }
Exemple #2
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);
        }