static IEnumerable <string> CacheVisibility(CacheProxyAttribute proxy, CacheClientAttribute client)
        {
            if (proxy == null && client == null)
            {
                yield break;
            }

            switch (proxy?.Level)
            {
            case CacheLevel.Everything:
                yield return("public");

                break;

            case CacheLevel.DoNotCache when client?.Level == CacheLevel.DoNotCache:
                yield return("no-cache");

                break;

            case null when client.Level != CacheLevel.DoNotCache:
            case CacheLevel.DoNotCache when client?.Level != CacheLevel.DoNotCache:
                yield return("private");

                break;
            }
        }
        static IEnumerable <string> CacheMaxAge(CacheProxyAttribute proxy, CacheClientAttribute client)
        {
            TimeSpan proxyAge = TimeSpan.MinValue, browserAge = TimeSpan.MinValue;

            if (proxy?.MaxAge != null)
            {
                TimeSpan.TryParse(proxy.MaxAge, out proxyAge);
            }
            if (client?.MaxAge != null)
            {
                TimeSpan.TryParse(client.MaxAge, out browserAge);
            }

            if (proxyAge == TimeSpan.MinValue && browserAge == TimeSpan.MinValue)
            {
                yield break;
            }

            if (proxyAge != TimeSpan.MinValue && browserAge != TimeSpan.MinValue)
            {
                yield return("s-max-age=" + proxyAge.TotalSeconds);
            }

            yield return("max-age=" + (proxyAge != TimeSpan.MinValue
                     ? proxyAge.TotalSeconds
                     : browserAge.TotalSeconds));
        }
 static IEnumerable<string> CacheRevalidation(CacheProxyAttribute proxy, CacheBrowserAttribute browser)
 {
     if (proxy != null && proxy.MustRevalidate && browser != null && browser.MustRevalidate)
         yield return "must-revalidate";
     else if (proxy != null && proxy.MustRevalidate)
         yield return "proxy-revalidate";
 }
 // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
 static void ValidateProxyAttribute(CacheProxyAttribute proxy)
 {
     if (proxy?.MaxAge != null && proxy.Level == CacheLevel.DoNotCache)
     {
         throw new InvalidOperationException("Cannot set MaxAge to a value and have the proxy cache disabled");
     }
 }
Exemple #5
0
 protected void given_attribute(
     CacheClientAttribute client = null,
     CacheProxyAttribute proxy   = null)
 {
     this.client = client;
     _proxy      = proxy;
 }
 static void SetupLocalCaching(CacheProxyAttribute proxy, ResponseCachingState response)
 {
     TimeSpan parsedMaxAge;
     if (proxy.MaxAge != null && TimeSpan.TryParse(proxy.MaxAge, out parsedMaxAge))
     {
         response.LocalCacheEnabled = true;
         response.LocalCacheMaxAge = parsedMaxAge;
     }
 }
        static IEnumerable <string> CacheRevalidation(CacheProxyAttribute proxy, CacheClientAttribute client)
        {
            if (proxy?.MustRevalidateWhenStale == true && client?.MustRevalidateWhenStale == false)
            {
                yield return("proxy-revalidate");
            }

            if (proxy?.MustRevalidateWhenStale == true || proxy?.MustRevalidateWhenStale == true)
            {
                yield return("must-revalidate");
            }
        }
 static IEnumerable<string> CacheVisibility(CacheProxyAttribute proxy, CacheBrowserAttribute browser)
 {
     if (proxy == null && browser == null)
         yield break;
     if (proxy != null && proxy.Level == ProxyCacheLevel.Everything)
         yield return "public";
     else if ((proxy == null || proxy.Level == ProxyCacheLevel.None) &&
         (browser == null || browser.Level == BrowserCacheLevel.Default))
         yield return "private";
     else if (proxy != null && browser != null && proxy.Level == ProxyCacheLevel.None && browser.Level == BrowserCacheLevel.None)
         yield return "no-cache";
 }
        // TODO: Reduce all those horrible enumerator allocations!

        public static ResponseCachingState GetResponseDirective(
            CacheProxyAttribute proxy   = null,
            CacheClientAttribute client = null)
        {
            ValidateProxyAttribute(proxy);

            var instructions =
                CacheVisibility(proxy, client)
                .Concat(CacheRevalidation(proxy, client))
                .Concat(CacheMaxAge(proxy, client));

            return(new ResponseCachingState(instructions));
        }
        public Func <IOperationAsync, Task <IEnumerable <OutputMember> > > Compose(
            Func <IOperationAsync, Task <IEnumerable <OutputMember> > > next)
        {
            return(async operation =>
            {
                var outputMembers = await next(operation);

                _proxy = operation.FindAttribute <CacheProxyAttribute>();
                _client = operation.FindAttribute <CacheClientAttribute>();

                _data[CacheKeys.ResponseCache] = CacheResponse.GetResponseDirective(_proxy, _client);
                return outputMembers;
            });
        }
        public bool BeforeExecute(IOperation operation)
        {
            _proxy = operation.FindAttribute<CacheProxyAttribute>();
            _browser = operation.FindAttribute<CacheBrowserAttribute>();
            _server = operation.FindAttribute<CacheServerAttribute>();

            var cacheEntry = TryGetValidCacheEntry(_request.Uri.AbsolutePath);

            if (cacheEntry != null)
            {
                _cachedValue = cacheEntry.Value;
            }
            return true;
        }
        static IEnumerable<string> CacheMaxAge(CacheProxyAttribute proxy, CacheBrowserAttribute browser)
        {
            TimeSpan proxyAge = TimeSpan.MinValue, browserAge = TimeSpan.MinValue;
            if (proxy != null && proxy.MaxAge != null) TimeSpan.TryParse(proxy.MaxAge, out proxyAge);
            if (browser != null && browser.MaxAge != null) TimeSpan.TryParse(browser.MaxAge, out browserAge);

            if (proxyAge == TimeSpan.MinValue && browserAge == TimeSpan.MinValue)
                yield break;

            if (proxyAge != TimeSpan.MinValue && browserAge != TimeSpan.MinValue)
                yield return "s-max-age=" + proxyAge.TotalSeconds;

            yield return "max-age=" + (proxyAge != TimeSpan.MinValue
                                           ? proxyAge.TotalSeconds
                                           : browserAge.TotalSeconds);
        }
        public static ResponseCachingState GetResponseDirective(
            CacheProxyAttribute proxy,
            CacheBrowserAttribute browser,
            CacheServerAttribute server)
        {
            ValidateProxyAttribute(proxy);

            var instructions = CacheVisibility(proxy, browser)
                .Concat(CacheRevalidation(proxy, browser))
                .Concat(CacheMaxAge(proxy, browser));

            var response = new ResponseCachingState(instructions);

            // SetupLocalCaching(proxy, response);

            return response;
        }
 static void ValidateProxyAttribute(CacheProxyAttribute proxy)
 {
     if (proxy != null && proxy.MaxAge != null && proxy.Level == ProxyCacheLevel.None)
         throw new InvalidOperationException("Cannot set MaxAge to a value and have the proxy cache disabled");
 }