Ejemplo n.º 1
0
        public async Task <T> GetAsync <T>(string key)
        {
            if (PreGet != null)
            {
                PreGet(this, new CacheEventArgs(key));
            }
            var value = await targetCache.GetAsync <T>(key);

            if (PostGet != null)
            {
                PostGet(this, new GetEventArgs(key));
            }
            return(value);
        }
        public async Task <AsyncCacheResult> GetAsync(string key)
        {
            AsyncCacheResult res;

            try
            {
                res = await _cache.GetAsync(key);
            }
            catch (Exception e)
            {
                _logger?.LogError(e, "An error occured getting key '{key}' from distributed cache", key);
                throw;
            }

            bool shouldLog = _logger != null && _logger.IsEnabled(LogLevel.Trace);

            switch (res.Status)
            {
            case AsyncCacheStatus.Hit:
                _metrics.IncrementDistributedCacheGetHits();
                if (shouldLog)
                {
                    _logger.Log(LogLevel.Trace, "IAsyncCache: GET {0} -> HIT {1}B", key, res.Value !.Length);
                }
                break;

            case AsyncCacheStatus.Miss:
                _metrics.IncrementDistributedCacheGetMisses();
                if (shouldLog)
                {
                    _logger.Log(LogLevel.Trace, "IAsyncCache: GET  {0} -> MISS", key);
                }
                break;

            case AsyncCacheStatus.Error:
                _metrics.IncrementDistributedCacheGetErrors();
                if (shouldLog)
                {
                    _logger.Log(LogLevel.Trace, "IAsyncCache: GET  {0} -> ERROR", key);
                }
                break;
            }

            return(res);
        }
Ejemplo n.º 3
0
        public override async Task OnActionExecutingAsync(HttpActionContext actionContext,
                                                          CancellationToken cancellationToken)
        {
            var cacheKey = actionContext.ActionArguments.ToCacheKey();
            var value    = await cache.GetAsync <string>(cacheKey);

            if (value != null)
            {
                actionContext.Response = actionContext.Request.CreateResponse();
                if (cacheOnClient)
                {
                    actionContext.Response.StatusCode = HttpStatusCode.NotModified;
                }
                else
                {
                    actionContext.Response.Content = new StringContent(value);
                    actionContext.Response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
                }
            }
        }
        public override SessionStateStoreData GetItem(HttpContext context, string id, out bool locked,
                                                      out TimeSpan lockAge, out object lockId,
                                                      out SessionStateActions actions)
        {
            // set default out parameters
            locked  = false;
            lockAge = new TimeSpan();
            lockId  = null;
            actions = SessionStateActions.None;
            // try to get the session from cache.
            string sessionString = cache.GetAsync <string>(id).Result;

            if (string.IsNullOrEmpty(sessionString))
            {
                return(null);
            }
            var sessionItems = JsonConvert.DeserializeObject <SessionStateItemCollection>(sessionString);
            var data         = new SessionStateStoreData(sessionItems, null, 60); // todo: set timeout.

            return(data);
        }
Ejemplo n.º 5
0
 /// <summary>
 ///   Gets the value with default partition and specified key. If it is a "sliding" or
 ///   "static" value, its lifetime will be increased by corresponding interval.
 /// </summary>
 /// <param name="cache">The async cache.</param>
 /// <param name="key">The key.</param>
 /// <param name="cancellationToken">An optional cancellation token.</param>
 /// <typeparam name="TVal">The type of the expected value.</typeparam>
 /// <returns>The value with default partition and specified key.</returns>
 /// <remarks>
 ///   If you are uncertain of which type the value should have, you can always pass
 ///   <see cref="object"/> as type parameter; that will work whether the required value is a
 ///   class or not.
 /// </remarks>
 /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>
 public static Task <CacheResult <TVal> > GetAsync <TVal>(this IAsyncCache cache, string key, CancellationToken cancellationToken = default(CancellationToken))
 => cache.GetAsync <TVal>(cache.Settings.DefaultPartition, key, cancellationToken);