コード例 #1
0
        /// <summary>Gets the cache item with specified key.</summary>
        /// <typeparam name="T">The item type.</typeparam>
        /// <param name="key">The key.</param>
        /// <returns>The cache item.</returns>
        public override Task <ICacheItem <T> > Get <T>(string key)
        {
            var empty = CacheItem <T> .Empty(key);

            empty.CacheHandler = this.Configuration.Name;

            var item = this.cache.Get(key) as ICacheItem <T>;

            if (item != null)
            {
                item.CacheHandler = this.Configuration.Name;

                // Set expiry date if applicable
                item.MergeExpire();

                if (item.Expires == DateTimeOffset.MinValue)
                {
                    return(Task.FromResult(item));
                }

                if (item.Expires < DateTimeOffset.UtcNow)
                {
                    return(Task.FromResult(empty));
                }

                return(Task.FromResult(item));
            }

            return(Task.FromResult(empty));
        }
コード例 #2
0
        /// <summary>
        /// Gets the cache item with specified key.
        /// </summary>
        /// <typeparam name="T">The item type.</typeparam>
        /// <param name="key">The key.</param>
        /// <returns>The cache item.</returns>
        public override async Task <ICacheItem <T> > Get <T>(string key)
        {
            var empty = CacheItem <T> .Empty(key);

            empty.CacheHandler = this.Configuration.Name;

            try
            {
                var item = this.cache[key] as ICacheItem <T>;

                if (item != null)
                {
                    item.CacheHandler = this.Configuration.Name;

                    // Set expiry date if applicable
                    item.MergeExpire();

                    if (item.Expires == DateTimeOffset.MinValue)
                    {
                        return(item);
                    }

                    if (item.Expires < DateTimeOffset.UtcNow)
                    {
                        return(empty);
                    }

                    return(item);
                }

                return(CacheItem <T> .Empty(key));
            }
            catch (KeyNotFoundException)
            {
                return(empty);
            }
        }
コード例 #3
0
 /// <summary>
 /// Gets the cache item with specified key.
 /// </summary>
 /// <typeparam name="T">The item type.</typeparam>
 /// <param name="key">The key.</param>
 /// <returns>The cache item.</returns>
 public virtual Task <ICacheItem <T> > Get <T>(string key)
 {
     return(Task.FromResult(CacheItem <T> .Empty(key)));
 }
コード例 #4
0
        /// <summary>
        /// Gets the cache item with specified key.
        /// </summary>
        /// <typeparam name="T">The item type.</typeparam>
        /// <param name="key">The key.</param>
        /// <returns>The cache item.</returns>
        public override async Task <ICacheItem <T> > Get <T>(string key)
        {
            var start = DateTimeOffset.UtcNow;

            try
            {
                var db = this.configuration.Connection.GetDatabase();

                this.log.DebugFormat("Trying to get cache item with key '{0}' and type '{1}'", key, typeof(T));

                try
                {
                    RedisValue data;
                    var        expires = DateTimeOffset.MinValue;

                    if (key.Contains("#"))
                    {
                        var h = key.Split('#')[0];
                        var k = key.Split('#')[1];

                        // Get data
                        data = await db.HashGetAsync(h, k);

                        // Get expire time
                        var ttl = await db.KeyTimeToLiveAsync(h);

                        if (ttl.HasValue && ttl < TimeSpan.MaxValue && ttl.Value.TotalSeconds > 0)
                        {
                            expires = DateTimeOffset.UtcNow.Add(ttl.Value);
                        }
                    }
                    else
                    {
                        // Get data
                        data = await db.StringGetAsync(key);

                        // Get expire time
                        var ttl = await db.KeyTimeToLiveAsync(key);

                        if (ttl.HasValue && ttl < TimeSpan.MaxValue && ttl.Value.TotalSeconds > 0)
                        {
                            expires = DateTimeOffset.UtcNow.Add(ttl.Value);
                        }
                    }

                    // If data is empty, return empty result
                    if (!data.HasValue)
                    {
                        this.log.WarnFormat("Couldn't find cache item with key '{0}'", key);

                        return(CacheItem <T> .Empty(key));
                    }

                    // Convert data to typed result
                    var raw = data;
                    T   result;

                    // If compression is specified, decompress result
                    if (this.configuration.Compress)
                    {
                        var serializer = SerializationContext.Default.GetSerializer <T>();

                        result = serializer.UnpackSingleObject(data);
                    }
                    else
                    {
                        var json = Encoding.UTF8.GetString(data);

                        result = JsonConvert.DeserializeObject <T>(json);
                    }

                    if (result == null)
                    {
                        this.log.WarnFormat("Couldn't retrieve cache item with key '{0}' and type '{1}'", key, typeof(T));
                    }
                    else
                    {
                        this.log.DebugFormat("Retrieved cache item with key '{0}' and type '{1}'. It expires at {2}", key, typeof(T), expires);
                        this.log.InfoFormat("RedisCacheHandler.GetCacheItem<{2}>({1}) Time: {0}s", DateTimeOffset.UtcNow.Subtract(start).TotalSeconds, key, typeof(T));

                        var ci = new CacheItem <T>(key, result, raw, expires)
                        {
                            CacheHandler = this.Configuration.Name
                        };
                        ci.MergeExpire();

                        return(ci);
                    }
                }
                catch (Exception ex)
                {
                    this.log.Error(string.Format("Error when getting value '{0}' and type '{1}' from database", key, typeof(T)), ex);
                }
            }
            catch (Exception ex)
            {
                this.log.Error("Error when connecting to database", ex);
            }

            this.log.InfoFormat("(ERROR) RedisCacheHandler.GetCacheItem<{2}>({1}) Time: {0}s", DateTimeOffset.UtcNow.Subtract(start).TotalSeconds, key, typeof(T));

            var empty = CacheItem <T> .Empty(key);

            empty.CacheHandler = this.Configuration.Name;

            return(empty);
        }