你的位置:首页 > ASP.net教程

[ASP.net教程]高性能文件缓存key


1.前言

 

2.代码描述

 

 1 // 源文件头信息: 2 // <copyright file="RedisHelper.cs"> 3 // Copyright(c)2014-2034 Kencery.All rights reserved. 4 // 个人博客:http://www.cnblogs.com/hanyinglong 5 // 创建人:韩迎龙(kencery) 6 // 创建时间:2015-8-18 7 // </copyright> 8  9  10 using System; 11 using System.Collections.Generic; 12 using System.Configuration; 13 using System.Security.Policy; 14 using NPOI.Open 15 using ServiceStack.Redis; 16  17 namespace KenceryCommonMethod 18 { 19   /// <summary> 20   /// Redis缓存读取设置 封装 21   /// <auther> 22   ///   <name>Kencery</name> 23   ///   <date>2015-8-18</date> 24   /// </auther> 25   /// </summary> 26   public static class RedisHelper 27   { 28     /// <summary> 29     /// 创建Redis连接池管理对象(添加ServiceStack.Interfaces.dll、ServiceStack.Redis.dll) 30     /// </summary> 31     /// <param name="readWriteHosts">只写服务器</param> 32     /// <param name="readOnlyHosts">只读服务器</param> 33     /// <returns></returns> 34     private static PooledRedisClientManager CreateRedisManager(string[] readWriteHosts, string[] readOnlyHosts) 35     { 36       //支持读写分离,均衡负载 37       return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig 38       { 39         MaxWritePoolSize = 5, //“写”链接池数 40         MaxReadPoolSize = 5, //“读”链接池数 41         AutoStart = true, 42       }); 43     } 44  45     /// <summary> 46     /// 调用CreateRedisManager方法,创建连接池管理对象,Redis服务器地址在配置文件中配置 47     /// <add key="RedisHosts" value="127.0.0.1:6379" /> 48     /// </summary> 49     private static PooledRedisClientManager _prcm = CreateRedisManager( 50       ConfigurationManager.AppSettings["Hosts"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries), 51       ConfigurationManager.AppSettings["Hosts"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)); 52  53     /// <summary> 54     /// 给缓存中添加数据,使用:RedisHelper.Set(key,值(需要存放的值)); 55     /// </summary> 56     /// <typeparam name="T">缓存类型</typeparam> 57     /// <param name="key">键</param> 58     /// <param name="val">值</param> 59     public static void Set<T>(string key, T val) 60     { 61       using (IRedisClient rdc = _prcm.GetClient()) 62       { 63         rdc.Set<T>(key, val); 64       } 65     } 66  67     /// <summary> 68     /// 读取缓存中的数据,使用:var result=RedisHelper.Get<T>(key); 69     /// </summary> 70     /// <typeparam name="T">返回读取的数据</typeparam> 71     /// <param name="key">键</param> 72     /// <returns></returns> 73     public static T Get<T>(string key) where T : class 74     { 75       using (IRedisClient rdc = _prcm.GetReadOnlyClient()) 76       { 77         return rdc.Get<T>(key); 78       } 79     } 80  81     /// <summary> 82     /// 删除缓存中的数据,使用 RedisHelper.Remove(key); 83     /// </summary> 84     /// <param name="key">键</param> 85     public static void Remove(string key) 86     { 87       using (IRedisClient rdc = _prcm.GetClient()) 88       { 89         rdc.Remove(key); 90       } 91     } 92  93     /// <summary> 94     /// 缓存中是否包含查询的键数据,使用 var isTrue=RedisHelper.ContainsKey(key); 95     /// </summary> 96     /// <param name="key">键</param> 97     /// <returns>如果包含,返回true,否则返回false</returns> 98     public static bool ContainsKey(string key) 99     {100       using (IRedisClient rdc = _prcm.GetReadOnlyClient())101       {102         return rdc.ContainsKey(key);103       }104     }105 106     /// <summary>107     /// 108     /// </summary>109     /// <param name="key"></param>110     /// <param name="value"></param>111     public static void Add(string key, object value)112     {113       using (IRedisClient rdc = _prcm.GetClient())114       {115         if (!rdc.ContainsKey(key))116         {117           rdc.Add(key, value, DateTime.Now.AddMinutes(30));118         }119         else120         {121           rdc.Set(key, value);122         }123       }124     }125 126     /// <summary>127     /// 128     /// </summary>129     /// <typeparam name="T"></typeparam>130     /// <param name="key"></param>131     /// <returns></returns>132     public static Void RefreshCache<T>(string key) where T : class133     {134       using (IRedisClient rdc = _prcm.GetClient())135       {136         var value = rdc.Get<T>(key);137         rdc.Remove(key);138         rdc.Set<T>(key, value);139       }140     }141 142     /// <summary>143     /// 144     /// </summary>145     /// <param name="keys"></param>146     /// <returns></returns>147     public static Dictionary<string, string> GetList(List<string> keys)148     {149       using (IRedisClient rdc = _prcm.GetReadOnlyClient())150       {151         return rdc.GetValuesMap<string>(keys);152       }153     }154 155     /// <summary>156     /// 157     /// </summary>158     /// <param name="values"></param>159     public static void Set(Dictionary<string, string> values)160     {161       using (IRedisClient rdc = _prcm.GetReadOnlyClient())162       {163         rdc.SetAll(values);164       }165     }166 167   }168 }

3.总结

  a.

  b.

  c.