代码实例
namespace RedisTest { class Program { static void Main(string[] args) { Student stu = RedisOperator.Instance.Get<Student>("stu"); RedisOperator.Instance.Set<Student>("stu", new Student { Id = 2, Name = "bb" }, 1); Student stu1 = RedisOperator.Instance.Get<Student>("stu"); Console.ReadKey(); } } class Student { public int Id { get; set; } public string Name { get; set; } } }
using ServiceStack.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace RedisTest { /// <summary> /// Install-Package ServiceStack.Redis /// </summary> public class RedisManager { /// <summary> /// RedisManager类主要是创建链接池管理对象的 /// </summary> private static string RedisPath = System.Configuration.ConfigurationManager.AppSettings["RedisPath"]; private static PooledRedisClientManager _prcm; /// <summary> /// 静态构造方法,初始化链接池管理对象 /// </summary> static RedisManager() { CreateManager(); } /// <summary> /// 创建链接池管理对象 /// </summary> private static void CreateManager() { _prcm = CreateManager(new string[] { RedisPath }, new string[] { RedisPath }); } private static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts) { //WriteServerList:可写的Redis链接地址。 //ReadServerList:可读的Redis链接地址。 //MaxWritePoolSize:最大写链接数。 //MaxReadPoolSize:最大读链接数。 //AutoStart:自动重启。 //LocalCacheTime:本地缓存到期时间,单位:秒。 //RecordeLog:是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项。 //RedisConfigInfo类是记录redis连接信息,此信息和配置文件中的RedisConfig相呼应 // 支持读写分离,均衡负载 return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig { MaxWritePoolSize = 5, // “写”链接池链接数 MaxReadPoolSize = 5, // “读”链接池链接数 AutoStart = true, }); } private static IEnumerable<string> SplitString(string strSource, string split) { return strSource.Split(split.ToArray()); } /// <summary> /// 客户端缓存操作对象 /// </summary> public static IRedisClient GetClient() { if (_prcm == null) { CreateManager(); } return _prcm.GetClient(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ServiceStack.Redis; using ServiceStack.Redis.Support; namespace RedisTest { public class RedisOperator:IDisposable { private IRedisClient _redis; private int _defaultSessionMinute = 20; public RedisOperator() { this._redis = RedisManager.GetClient(); string RedisSessionTimeOutMinute = System.Configuration.ConfigurationManager.AppSettings["RedisSessionTimeOutMinute"]; if (!string.IsNullOrEmpty(RedisSessionTimeOutMinute) && !int.TryParse(RedisSessionTimeOutMinute, out this._defaultSessionMinute)) { this._defaultSessionMinute = 600; } } private static RedisOperator _instance; private static object lockobj = new object(); public static RedisOperator Instance { get { if (_instance == null) { lock (lockobj) { if (_instance == null) { _instance = new RedisOperator(); } } } return _instance; } } public void Set<T>(string key, T obj, double minuteOut = 0) { if (minuteOut >= 0) { if (minuteOut == 0) minuteOut = this._defaultSessionMinute; this._redis.Set<T>(key, obj, TimeSpan.FromMinutes(minuteOut)); } else { this._redis.Set<T>(key, obj); } } public T Get<T>(string key) { return this._redis.Get<T>(key); } public bool Remove(string key) { return this._redis.Remove(key); } public void Dispose() { try { if (this._redis != null) this._redis.Dispose(); } catch (Exception ex) { } } } }
webConfig
<appSettings>
<!--Redis 配置 -->
<add key="RedisPath" value="127.0.0.1:6379" />
<add key="RedisSessionTimeOutMinute" value="20" />
</appSettings>
1.Redis Manager 用于处理Reids 连接池
2.RedisOperator 负责Redis缓存的添加,删除
3.配置文件 RedisPath Redis服务器地址,端口用默认的6379
4.RedisSessionTimeOutMinute 缓存过期时间可以通过配置默认20分钟 代码里边也可以传值设置时间的话就不走配置的默认值了(代码RedisOperator里有判断)
注意:1.由于redis的安全设置,只允许本地访问,
2.若要让其他iP 可访问,那么
a.在安装的redis 文件夹下找到redis.conf 文件 将 所有的bind 127.0.0.1 bind.. 注释掉
b.在电脑防火墙 入站规则里边添加新建规则 redis tcp 端口为 6379