Esempio n. 1
0
        private string GetQueryStringKey(HttpContext context, ApiResponseCacheConfig config)
        {
            var pathStrings = ToList(context.GetRouteData());

            if (config.ExcludeQueryStringFromCacheKey)
            {
                return(string.Join("&", pathStrings));
            }
            else
            {
                var args = context.Request.Query
                           .Where(q => !q.Key.Equals(CallbackKey))
                           .Select(q => $"{q.Key}={q.Value}");

                var argumentsString = string.Join("&", pathStrings.Union(args));

                var jsonpCallback = GetJsonpCallback(context.Request);
                if (!string.IsNullOrWhiteSpace(jsonpCallback))
                {
                    argumentsString = RemoveJsopCallback(argumentsString, jsonpCallback);
                }

                return(argumentsString);
            }
        }
Esempio n. 2
0
 private string GetCachePerUserKey(HttpContext context, ApiResponseCacheConfig config)
 {
     if (config.CachePerUser)
     {
         return(context.User.Identity.IsAuthenticated
                             ? context.User.Identity.Name
                             : AnonymusUserName);
     }
     else
     {
         return("");
     }
 }
        public static void EnableOutputCaching(this HttpContext context,
                                               bool noCache,
                                               int sharedTimeSpan,
                                               int clientTimeSpan,
                                               int serverTimeSpan,
                                               bool excludeQueryStringFromCacheKey,
                                               bool mustRevalidate,
                                               bool anonymousOnly,
                                               Type cacheKeyGenerator,
                                               bool @private,
                                               Type customTimeSpanMethodClassType,
                                               string customTimeSpanMethodName,
                                               bool cachePerUser,
                                               ExpirationMode expirationMode,
                                               string apiName,
                                               bool logEnabled,
                                               string actionName,
                                               string controllerName)
        {
            var feature = context.Features.Get <ApiResponseCacheConfig>();

            if (feature == null)
            {
                feature = new ApiResponseCacheConfig();
                context.Features.Set(feature);
            }

            feature.AnonymousOnly  = anonymousOnly;
            feature.NoCache        = noCache;
            feature.SharedTimeSpan = sharedTimeSpan;
            feature.ClientTimeSpan = clientTimeSpan;
            feature.ServerTimeSpan = serverTimeSpan;
            feature.ExcludeQueryStringFromCacheKey = excludeQueryStringFromCacheKey;
            feature.MustRevalidate                = mustRevalidate;
            feature.CacheKeyGenerator             = cacheKeyGenerator;
            feature.Private                       = @private;
            feature.CustomTimeSpanMethodClassType = customTimeSpanMethodClassType;
            feature.CustomTimeSpanMethodName      = customTimeSpanMethodName;
            feature.CachePerUser                  = cachePerUser;
            feature.ExpirationMode                = expirationMode;
            feature.ApiName                       = apiName;
            feature.LogEnabled                    = logEnabled;
            feature.ActionName                    = actionName;
            feature.ControllerName                = controllerName;
        }
Esempio n. 4
0
        private string GetBodyKey(HttpContext context, ApiResponseCacheConfig config)
        {
            var postBody = "";

            if (context.Request.Method == HttpMethod.Post.Method ||
                context.Request.Method == HttpMethod.Delete.Method ||
                context.Request.Method == HttpMethod.Put.Method)
            {
                context.Request.EnableRewind();

                using (var stream = new StreamReader(context.Request.Body, encoding: Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 1024, leaveOpen: true))
                {
                    postBody = stream.ReadToEnd();
                    context.Request.Body.Seek(0, SeekOrigin.Begin);
                }
            }

            return(postBody);
        }
 public static void EnableOutputCaching(this HttpContext context, ApiResponseCacheConfig config)
 {
     context.EnableOutputCaching(
         config.NoCache,
         config.SharedTimeSpan,
         config.ClientTimeSpan,
         config.ServerTimeSpan,
         config.ExcludeQueryStringFromCacheKey,
         config.MustRevalidate,
         config.AnonymousOnly,
         config.CacheKeyGenerator,
         config.Private,
         config.CustomTimeSpanMethodClassType,
         config.CustomTimeSpanMethodName,
         config.CachePerUser,
         config.ExpirationMode,
         config.ApiName,
         config.LogEnabled,
         null,
         null);
 }
        internal static bool IsOutputCachingEnabled(this HttpContext context, out ApiResponseCacheConfig config)
        {
            config = context.Features.Get <ApiResponseCacheConfig>();

            return(config != null);
        }