标签:部署 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(); }
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 }
redis的配置写在webconfig中了
标签:部署 ring close 连接 context cep isnull this alt
原文地址:http://www.cnblogs.com/sunzgod/p/6438490.html