ServiceStack.Redis简单封装
首先创建RedisConfig配置类
#region 单例模式 //定义单例实体 private static RedisConfig _redisConfig = null; /// <summary> /// 获取本逻辑类单例 /// </summary> /// <returns></returns> public static RedisConfig GetInstence() { if (_redisConfig == null) { _redisConfig = new RedisConfig(); } return _redisConfig; } #endregion public RedisConfig() { WriteServerList = ConfigurationManager.AppSettings["WriteServerList"]; ReadServerList = ConfigurationManager.AppSettings["ReadServerList"]; MaxWritePoolSize = ConvertHelper.ToInt0(ConfigurationManager.AppSettings["MaxWritePoolSize"]); MaxReadPoolSize = ConvertHelper.ToInt0(ConfigurationManager.AppSettings["MaxReadPoolSize"]); AutoStart = ConfigurationManager.AppSettings["AutoStart"] == "true"; LocalCacheTime = ConvertHelper.ToInt0(ConfigurationManager.AppSettings["LocalCacheTime"]); RecordeLog = ConfigurationManager.AppSettings["RecordeLog"] == "false"; } /// <summary> /// 可写的Redis链接地址 /// </summary> public string WriteServerList { get; set; } /// <summary> /// 可读的Redis链接地址 /// </summary> public string ReadServerList { get; set; } /// <summary> /// 最大写链接数 /// </summary> public int MaxWritePoolSize { get; set; } /// <summary> /// 最大读链接数 /// </summary> public int MaxReadPoolSize { get; set; } /// <summary> /// 自动重启 /// </summary> public bool AutoStart { get; set; } /// <summary> /// 本地缓存到期时间,单位:秒 /// </summary> public int LocalCacheTime { get; set; } /// <summary> /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项 /// </summary> public bool RecordeLog { get; set; } /// <summary> /// 默认db /// </summary> public int DefaultDb { get; set; } }
再创建一个操作工具类
public class RedisHelper { private int _isSafeModal = 0; #region 单例模式 private RedisHelper() { } public static readonly RedisHelper Instance = new RedisHelper(); private RedisHelper(int isSafeModal = 0) { this._isSafeModal = isSafeModal; } #endregion /// <summary> /// redis配置文件信息 /// </summary> private readonly RedisConfig _redisConfigInfo = RedisConfig.GetInstence(); /// <summary> /// 链接池管理对象 /// </summary> private static PooledRedisClientManager _prcm; private static string[] SplitString(string strSource, string split) { return strSource.Split(split.ToArray()); } /// <summary> /// 客户端缓存操作对象 /// </summary> public IRedisClient GetClient() { if (_prcm == null) _prcm = CreateManager(); return _prcm.GetClient(); } /// <summary> /// 创建链接池管理对象 /// </summary> private PooledRedisClientManager CreateManager() { string[] writeServerList = SplitString(_redisConfigInfo.WriteServerList, ","); string[] readServerList = SplitString(_redisConfigInfo.ReadServerList, ","); return new PooledRedisClientManager(writeServerList, readServerList, new RedisClientManagerConfig { MaxWritePoolSize = _redisConfigInfo.MaxWritePoolSize, MaxReadPoolSize = _redisConfigInfo.MaxReadPoolSize, AutoStart = _redisConfigInfo.AutoStart, //DefaultDb = _redisConfigInfo.DefaultDb, DefaultDb = _isSafeModal == 1 ? 1 : _redisConfigInfo.DefaultDb }); } #region 获取指定key的缓存对象 /// <summary> /// 获取指定key的缓存对象 /// </summary> /// <typeparam name="T">泛型对象</typeparam> /// <param name="key">缓存key</param> /// <returns>返回指定对象</returns> public T Get<T>(string key) { //链接Redis缓存管理对象 using (var redisClient = GetClient()) { return redisClient.Get<T>(key.ToLower()); } } /// <summary> /// 获取指定key的缓存对象 /// </summary> /// <param name="key">缓存key</param> /// <param name="isGetMinDate">缓存key</param> /// <returns>返回指定对象</returns> public DateTime GetDateTime(string key, bool isGetMinDate = true) { //链接Redis缓存管理对象 using (var redisClient = GetClient()) { try { var dt = redisClient.Get<DateTime>(key); if (dt == new DateTime(01, 01, 01)) { if (isGetMinDate) return DateTimeHelper.GetMinDate(); return DateTime.Now; } return dt; } catch { if (isGetMinDate) return DateTimeHelper.GetMinDate(); return DateTime.Now; } } } #endregion #region 设置缓存对象 /// <summary> /// 设置缓存对象——默认一天后超时 /// </summary> /// <param name="key">缓存key</param> /// <param name="value">指定对象值</param> public void Set<T>(string key, T value) { //链接Redis缓存管理对象 using (var redisClient = GetClient()) { redisClient.Set(key.ToLower(), value, DateTime.Now.AddDays(1)); } } /// <summary> /// 设置缓存对象 /// </summary> /// <param name="key">缓存key</param> /// <param name="value">指定对象值</param> /// <param name="expiresAt">有效时间</param> public void Set<T>(string key, T value, DateTime expiresAt) { //链接Redis缓存管理对象 using (var redisClient = GetClient()) { redisClient.Set(key.ToLower(), value, expiresAt); } } /// <summary> /// 设置缓存对象 /// </summary> /// <param name="key">缓存key</param> /// <param name="value">指定对象值</param> /// <param name="expiresIn">有效时间</param> public void Set<T>(string key, T value, TimeSpan expiresIn) { //链接Redis缓存管理对象 using (var redisClient = GetClient()) { redisClient.Set(key.ToLower(), value, expiresIn); } } /// <summary> /// 设置缓存对象 /// </summary> /// <param name="key">缓存key</param> /// <param name="value">指定对象值</param> /// <param name="timeout">有效时间</param> public void Set<T>(string key, T value, int timeout) { Set<T>(key, value, new TimeSpan(0, 0, 0, timeout)); } /// <summary> /// 设置缓存过期 /// </summary> /// <param name="key"></param> /// <param name="timeout"></param> public bool SetExpire(string key, int timeout) { using (var redis = GetClient()) { return redis.ExpireEntryIn(key.ToLower(), new TimeSpan(0, 0, timeout)); } } #endregion #region 删除指定缓存对象 /// <summary> /// 清空所有缓存 /// </summary> public void FlushAll() { //链接Redis缓存管理对象 using (var redisClient = GetClient()) { redisClient.FlushDb(); } } /// <summary> /// 删除指定缓存对象 /// </summary> /// <param name="key">缓存key</param> public void Delete(string key) { //链接Redis缓存管理对象 using (var redisClient = GetClient()) { redisClient.Remove(key.ToLower()); } } #endregion }
把缓存配置放进配置文件:
<!-- Redis缓存配置 --> <add key="WriteServerList" value="xxxxxxxxxx" /> <add key="ReadServerList" value="xxxxxxxxxxxxxx" /> <add key="MaxWritePoolSize" value="60" /> <add key="MaxReadPoolSize" value="60" /> <add key="AutoStart" value="true" /> <add key="LocalCacheTime" value="180" /> <add key="RecordeLog" value="false" /> <!--默认缓存放置位置 --> <add key="DefaultDb" value="0" />
配置思路: 连接池管理对象(PooledRedisClientManager) 、Redis客户端操作对象(使用接口IRedisClient)
1.创建一个创建连接池管理对象(PooledRedisClientManager)
private PooledRedisClientManager CreatePool() { string[] WriteList = config.Write.Split(new char[] { ','}); string[] ReadList = config.Read.Split(new char[] { ',' }); return new PooledRedisClientManager(WriteList, ReadList, new RedisClientManagerConfig { MaxReadPoolSize = config.MaxReadPoolSize, MaxWritePoolSize = config.MaxWritePoolSize, AutoStart = true, DefaultDb= DefalutDb }); }
2.使用PooledRedisClientManager对象创建客户端缓存操作对象
public IRedisClient GetRedis() { if (_prcm == null) { _prcm = CreatePool(); } return _prcm.GetClient(); }
3.创建连接池管理对象 、 客户端缓存操作对象创建完成后就可以用客户端缓存操作对象封装一些方法了0.0
如:
public T Get<T>(string key) { using (var redisClient = GetRedis()) { return redisClient.Get<T>(key); }
测试下
看看Redis
List类型
看下redis
刚搞Redis,理解有限,如有不对请不吝赐教~