redis in c#

來源:互聯網
上載者:User

標籤:部署   ring   close   串連   context   cep   isnull   this   alt   

redis伺服器部署:講的很明白??

代碼使用部分

需要引用的程式集:StackExchange.Redis.StrongName.dll

redis串連的介面

    public interface  IRedisConnectionWrapper:IDisposable    {        IDatabase Database(int? db = null);        IServer Server(EndPoint endPoint);        EndPoint[] GetEndPoints();        void FlushDb(int? db = null);    }
//緩衝具體介面
    public interface ICacheManager:IDisposable    {        /// <summary>        /// 根據鍵擷取值        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="key"></param>        /// <returns></returns>        T Get<T>(string key);        /// <summary>        /// 添加一個索引值對緩衝        /// </summary>        /// <param name="key"></param>        /// <param name="data"></param>        /// <param name="cacheTime"></param>        void Set(string key, object data, int cacheTime);        /// <summary>        /// 擷取鍵對應的值是否存在        /// </summary>        /// <param name="key"></param>        /// <returns></returns>        bool IsSet(string key);        /// <summary>        /// 移除索引值對緩衝        /// </summary>        /// <param name="key"></param>        void Remove(string key);        void RemoveByPattern(string pattern);        /// <summary>        /// 清空所有的快取資料        /// </summary>        void Clear();    }
View Code//實現
    public partial class RedisCacheManager:ICacheManager    {        #region Fileds        private readonly IRedisConnectionWrapper _connectionWrapper;        private readonly IDatabase _db;        private readonly ICacheManager _perRequestCacheManager;        #endregion        #region Ctor        public RedisCacheManager(CalabashConfig config, IRedisConnectionWrapper connectionWrapper)        {            if(String.IsNullOrEmpty(config.RedisCachingConnectionString))                throw new Exception("Redis connection string is empty");            this._connectionWrapper = connectionWrapper;            this._db = _connectionWrapper.Database();            this._perRequestCacheManager = EngineContext.Current.Resolve<ICacheManager>();        }        #endregion        #region Utilities        /// <summary>        /// 序列化        /// </summary>        /// <param name="item"></param>        /// <returns></returns>        protected virtual byte[] Serialize(object item)        {            var jsonString = JsonConvert.SerializeObject(item);            return Encoding.UTF8.GetBytes(jsonString);        }        /// <summary>        /// 還原序列化        /// </summary>        /// <typeparam name="T"></typeparam>        /// <param name="serializedObject"></param>        /// <returns></returns>        protected virtual T Deserialize<T>(byte[] serializedObject)        {            if (serializedObject == null)                return default(T);            var jsonString = Encoding.UTF8.GetString(serializedObject);            return JsonConvert.DeserializeObject<T>(jsonString);        }        #endregion        #region Methods        public T Get<T>(string key)        {            if (_perRequestCacheManager.IsSet(key))                return _perRequestCacheManager.Get<T>(key);            var rValue = _db.StringGet(key);            if (!rValue.HasValue)                return default(T);            var result = Deserialize<T>(rValue);            _perRequestCacheManager.Set(key,result,0);            return result;        }        public void Set(string key, object data, int cacheTime)        {            if(data==null)                return;            var entryBytes = Serialize(data);            var expiresIn = TimeSpan.FromMinutes(cacheTime);            _db.StringSet(key, entryBytes, expiresIn);        }        public bool IsSet(string key)        {            if (_perRequestCacheManager.IsSet(key))                return true;            return _db.KeyExists(key);        }        public void Remove(string key)        {            _db.KeyDelete(key);            _perRequestCacheManager.Remove(key);        }        public void RemoveByPattern(string pattern)        {            foreach (var ep in _connectionWrapper.GetEndPoints())            {                var server = _connectionWrapper.Server(ep);                var keys = server.Keys(pattern: "*" + pattern + "*");                foreach (var key in keys)                {                    _db.KeyDelete(key);                }            }        }        public void Clear()        {            foreach (var key in _connectionWrapper.GetEndPoints().Select(ep => _connectionWrapper.Server(ep)).Select(server => server.Keys()).SelectMany(keys => keys))            {                _db.KeyDelete(key);            }        }        public void Dispose()        {        }        #endregion    }
View Code

redis的配置寫在webconfig中了

redis in c#

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.