Redis缓存公共方法类
2018-06-22 07:32:11来源:未知 阅读 ()
Redis缓存公共方法类
Config配置文件:
<!--Redis相关设置--> <!--是否打开缓存1是0否--> <add key="IsOpenCache" value="1" /> <!--redis IP地址--> <add key="RedisIP" value="192.168.1.1" /> <!--redis端口(不填默认6379)--> <add key="RedisPort" value="6379" /> <!--redis密码(不填表示没有密码)--> <add key="RedisPassword" value="@123456" /> <!--redis库索引号(整数,默认有5个库,从0开始,不填表示0)--> <add key="RedisDb" value="2" /> <!--Redis相关设置结束 --> <add key="ClientSettingsProvider.ServiceUri" value="" />
RedisHelper:
using System; using System.Collections.Generic; using System.Text; using ServiceStack.Redis; public class RedisHelper { private static object lockobj = new object(); /// <summary> /// 加入缓存 /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="cacheKey">Key(键值命名规范:RK_字段名_表名_条件字段1_值_条件字段n_值......,键值全部小写,表名不加dbo前缀)</param> /// <param name="obj">对象</param> /// <param name="expiresAt">缓存时间(除了XianZhiAccounts的过期时间为一小时,其余的过期时间都为两天)</param> /// <returns>是否缓存成功</returns> public static bool Set<T>(string cacheKey, T obj, DateTime expiresAt) { bool result = false; lock (lockobj) { try { if (IsOpenCache) { if (!(obj is System.Data.DataTable) || !(obj is System.Data.DataSet) || !(obj is System.Data.DataRow)) { using (IRedisClient Redis = prcm.GetClient()) { result = Redis.Set<T>(cacheKey, obj, expiresAt); } } } } catch { } } return result; } /// <summary> /// 加入缓存(永久缓存) /// </summary> /// <typeparam name="T">对象类型</typeparam> /// <param name="cacheKey">Key(键值命名规范:RK_字段名_表名_条件字段1_值_条件字段n_值......,键值全部小写)</param> /// <param name="obj">对象</param> /// <returns>是否缓存成功</returns> private static bool Set<T>(string cacheKey, T obj) { lock (lockobj) { try { if (!IsOpenCache) return false; return Set<T>(cacheKey, obj, DateTime.Now.AddYears(100)); } catch { return false; } } } /// <summary> /// 获取缓存内容 /// </summary> /// <param name="cacheKey">Key</param> /// <returns></returns> public static T Get<T>(string cacheKey) { lock (lockobj) { try { if (!IsOpenCache) return default(T); using (IRedisClient Redis = prcm.GetClient()) { return Redis.Get<T>(cacheKey); } } catch { string str = string.Empty; return default(T); } } } /// <summary> /// 删除缓存 /// </summary> /// <param name="cacheKey">Key</param> /// <returns></returns> public static bool Del(string cacheKey) { try { if (!IsOpenCache) return false; using (IRedisClient Redis = prcm.GetClient()) { return Redis.Remove(cacheKey); } } catch { return false; } } /// <summary> /// 批量删除缓存 /// </summary> /// <param name="cacheKeys">Key</param> /// <returns></returns> public static void Del(List<string> cacheKeys) { try { if (!IsOpenCache) return; using (IRedisClient Redis = prcm.GetClient()) { Redis.RemoveAll(cacheKeys); } } catch { return; } } /// <summary> /// 清空缓存 /// </summary> public static void ClearCache() { try { if (!IsOpenCache) return; using (IRedisClient Redis = prcm.GetClient()) { Redis.FlushAll(); } } catch { return; } } #region 私有方法 private static string RedisIP = ConfigHelper.GetAppSettingValue("RedisIP"); private static string RedisPassword = ConfigHelper.GetAppSettingValue("RedisPassword"); private static int RedisPort = _51Sole.DJG.Common.DataConvert.ToInt(ConfigHelper.GetAppSettingValue("RedisIP"), 6379); private static int DbIndex = _51Sole.DJG.Common.DataConvert.ToInt(ConfigHelper.GetAppSettingValue("RedisDb"), 2); private static PooledRedisClientManager prcm = CreateManager( new string[] { "" + RedisPassword + "@" + RedisIP + ":" + RedisPort + " " }, new string[] { "" + RedisPassword + "@" + RedisIP + ":" + RedisPort + " " }); private static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts) { // 支持读写分离,均衡负载 RedisClientManagerConfig clientConfig = new RedisClientManagerConfig(); clientConfig.MaxWritePoolSize = 10000; clientConfig.MaxReadPoolSize = 10000; clientConfig.AutoStart = true; clientConfig.DefaultDb = DbIndex; PooledRedisClientManager clientManager = new PooledRedisClientManager(readWriteHosts, readOnlyHosts, clientConfig); return clientManager; } /// <summary> /// 是否使用缓存开关 /// </summary> private static bool IsOpenCache { get { if (ConfigHelper.GetAppSettingValue("IsOpenCache") != "1") return false; return true; } } #endregion }
ConfigHelper:
/// <summary> /// Redis配置文件帮助类 /// </summary> public class ConfigHelper { #region 配置文件读取 public static string GetAppSettingValue(string key) { try { return System.Configuration.ConfigurationManager.AppSettings[key]; } catch { return string.Empty; } } #endregion }
TypeConverter:
using System; using System.Globalization; public class TypeConverter { /// <summary> /// 转换为int类型 /// </summary> /// <param name="obj"></param> /// <param name="defaultValue">返回的默认值</param> /// <param name="numStyle">数字格式</param> /// <returns></returns> public static int ConvertToInt(object obj, NumberStyles numStyle) { int result = 0; if (obj != null && obj != DBNull.Value) { if (!int.TryParse(obj.ToString().Trim(), numStyle, null, out result)) { result = 0; } } return result; } /// <summary> /// 转换为int类型 /// </summary> /// <param name="obj"></param> /// <param name="defaultValue">返回的默认值</param> /// <returns></returns> public static int ConvertToInt(object obj) { return ConvertToInt(obj, NumberStyles.Number); } }
添加缓存方法:
string keyWord = "TestRedis"; string cacheKey_keyWord = "RK_RedisCache_KeyWord_" + keyWord; string cacheKey_Data = "RK_RedisCache_Data_" + keyWord; RedisHelper.Set<string>(cacheKey_keyWord, "此处放缓存值", DateTime.Now.AddDays(5));//有效期
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:扫二维码关注服务号同时发现金红包
下一篇:平台化项目多语言架构实现
- .NET 缓存模块设计实践 2020-01-20
- ASP.NET缓存管理的方法 2019-09-17
- 详解ASP.NET MVC中使用jQuery时的浏览器缓存问题 2019-09-08
- ASP.NET缓存方法和最佳实践 2019-08-23
- asp.net 取消缓存的问题说明 2019-08-09
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash