.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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM