Beispiel #1
0
        /// <summary>
        /// Gets a value indicating whether the value associated with the specified key is cached
        /// </summary>
        /// <param name="key">key</param>
        /// <returns>Result</returns>
        public virtual bool IsSet(string key)
        {
            //little performance workaround here:
            //we use "PerRequestCacheManager" to cache a loaded object in memory for the current HTTP request.
            //this way we won't connect to Redis server 500 times per HTTP request (e.g. each time to load a locale or setting)
            if (_perRequestCacheManager.IsSet(key))
            {
                return(true);
            }

            return(RedisManager.GetDb().KeyExists(key));
        }
Beispiel #2
0
        /// <summary>
        /// Adds the specified key and object to the cache.
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="data">Data</param>
        /// <param name="cacheTime">Cache time</param>
        public virtual void Set(string key, object data, int cacheTime)
        {
            if (data == null)
            {
                return;
            }

            var entryBytes = Serialize(data);
            var expiresIn  = TimeSpan.FromMinutes(cacheTime);

            RedisManager.GetDb().StringSet(key, entryBytes, expiresIn);
        }
Beispiel #3
0
        /// <summary>
        /// Removes items by pattern
        /// </summary>
        /// <param name="pattern">pattern</param>
        public virtual void RemoveByPattern(string pattern)
        {
            var _muxer = RedisManager.GetMuxer();
            var _db    = RedisManager.GetDb();

            foreach (var ep in _muxer.GetEndPoints())
            {
                var server = _muxer.GetServer(ep);
                var keys   = server.Keys(pattern: "*" + pattern + "*", database: _db.Database);
                foreach (var key in keys)
                {
                    _db.KeyDelete(key);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Clear all cache data
        /// </summary>
        public virtual void Clear()
        {
            var _muxer = RedisManager.GetMuxer();
            var _db    = RedisManager.GetDb();

            foreach (var ep in _muxer.GetEndPoints())
            {
                var server = _muxer.GetServer(ep);
                //we can use the code belwo (commented)
                //but it requires administration permission - ",allowAdmin=true"
                //server.FlushDatabase();

                //that's why we simply interate through all elements now
                var keys = server.Keys(_db.Database);
                foreach (var key in keys)
                {
                    _db.KeyDelete(key);
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Gets or sets the value associated with the specified key.
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="key">The key of the value to get.</param>
        /// <returns>The value associated with the specified key.</returns>
        public virtual T Get <T>(string key)
        {
            //little performance workaround here:
            //we use "PerRequestCacheManager" to cache a loaded object in memory for the current HTTP request.
            //this way we won't connect to Redis server 500 times per HTTP request (e.g. each time to load a locale or setting)
            if (_perRequestCacheManager.IsSet(key))
            {
                return(_perRequestCacheManager.Get <T>(key));
            }
            var db = RedisManager.GetDb();

            var rValue = db.StringGet(key);

            if (!rValue.HasValue)
            {
                return(default(T));
            }
            var result = Deserialize <T>(rValue);

            _perRequestCacheManager.Set(key, result, 0);
            return(result);
        }
Beispiel #6
0
 /// <summary>
 /// 基于缓存的分布式锁 加锁(redis、Memcached有效) caodq 20160615
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public bool Lock(string key)
 {
     return(RedisManager.GetDb().LockTake(key, 0, TimeSpan.FromMinutes(5)));
 }
Beispiel #7
0
 /// <summary>
 /// Removes the value with the specified key from the cache
 /// </summary>
 /// <param name="key">/key</param>
 public virtual void Remove(string key)
 {
     RedisManager.GetDb().KeyDelete(key);
 }