Ejemplo n.º 1
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);
                }
            }
        }
Ejemplo n.º 2
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);
                }
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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)));
 }
Ejemplo n.º 5
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);
 }