.NET 使用StackExchange.Redis 连接Redis,作为cache 遇到的一写问题

项目中用到 StackExchange.Redis 连接Redis 作为cache 

在模糊匹配获取key 时出现错误

        /// <summary>
        /// Removes items by pattern
        /// </summary>
        /// <param name="pattern">pattern</param>
        public virtual void RemoveByPattern(string pattern)
        {
            foreach (var ep in _muxer.GetEndPoints())
            {
                var server = _muxer.GetServer(ep);
                var keys = server.Keys(pattern: "*" + pattern + "*");
                foreach (var key in keys)
                    _db.KeyDelete(key);
            }
        }

 

标颜色的这一样 报错提示 

Redis报错提信息   This operation is not available unless admin mode is enabled: KEYS

管理员模式,什么东东,头大了,上网搜。

从国外网站上找到解决办法

http://stackoverflow.com/questions/24531421/remove-delete-all-one-item-from-stackexchange-redis-cache

 

需要  修改代码

        public RedisCacheManager(System.Web.HttpContext _httpContext)
        {
            string pwd = "111111";
            var options = ConfigurationOptions.Parse("127.0.0.1:6379");
            options.SyncTimeout = int.MaxValue;
            options.AllowAdmin = true;
            if (!string.IsNullOrEmpty(pwd))
            {
                options.Password = pwd;
            }
            this._muxer = ConnectionMultiplexer.Connect(options);

            string connStr = "127.0.0.1:6379,syncTimeout = 2147483647,allowAdmin = true,password = 111111";
            this._muxer = ConnectionMultiplexer.Connect(connStr);
            this._db = _muxer.GetDatabase();
            this._perRequestCacheManager = new PerRequestCacheManager(_httpContext);
        }

两种形式都支持  

问题解决 Ok

posted @ 2016-11-16 17:01  金小鳄  阅读(3069)  评论(1编辑  收藏  举报