Redis初使用
redis是一个key-value存储系统。和Memcached类似,Redis不仅仅支持简单的k/v类型的数据,同时还提供list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)等存储方式。它支持存储的value类型相对更多。这些数据类型都 支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文 件,并且在此基础上实现了master-slave(主从)同步。(来自百度百科)
目前大型企业用到Redis做缓存的主要有新浪微博、Github、StackOverflow等;
下载安装redis-3.2.100-setup-64-bit.exe(根据需要更新版本,我的是64位操作系统),打开CMD编辑命令,找到安装包的根目录运行命令:
redis-server --service-install redis.windows-service.conf --loglevel verbose
NuGet管理包安装:
RedisHelper操作类
1 /// <summary> 2 /// Redis 操作类 3 /// </summary> 4 public class RedisHelper { 5 /// <summary> 6 /// 连接字符串 7 /// </summary> 8 private static readonly string ConnectionString = "172.21.24.104:6379"; 9 10 /// <summary> 11 /// 锁 12 /// </summary> 13 private readonly object _lock = new object(); 14 15 /// <summary> 16 /// 连接对象 17 /// </summary> 18 private volatile IConnectionMultiplexer _connection; 19 20 /// <summary> 21 /// 数据库 22 /// </summary> 23 private IDatabase _db; 24 25 public RedisHelper() { 26 this._connection = ConnectionMultiplexer.Connect(ConnectionString); 27 this._db = this.GetDatabase(); 28 } 29 30 /// <summary> 31 /// 获取连接 32 /// </summary> 33 /// <returns></returns> 34 protected IConnectionMultiplexer GetConnection() { 35 if (this._connection != null && this._connection.IsConnected) { 36 return this._connection; 37 } 38 39 lock(this._lock) { 40 if (this._connection != null && this._connection.IsConnected) { 41 return this._connection; 42 } 43 44 if (this._connection != null) { 45 this._connection.Dispose(); 46 } 47 48 this._connection = ConnectionMultiplexer.Connect(ConnectionString); 49 } 50 51 return this._connection; 52 } 53 54 /// <summary> 55 /// 获取数据库 56 /// </summary> 57 /// <param name="db"></param> 58 /// <returns></returns> 59 public IDatabase GetDatabase(int? db = null) { 60 return this.GetConnection().GetDatabase(db ?? -1); 61 } 62 63 /// <summary> 64 /// 设置 65 /// </summary> 66 /// <param name="key">键</param> 67 /// <param name="data">值</param> 68 /// <param name="cacheTime">时间</param> 69 public virtual void Set(string key, object data, int? cacheTime = null) { 70 if (data == null) { 71 return; 72 } 73 74 var entryBytes = this.Serialize(data); 75 if (cacheTime == null) { 76 cacheTime = 60 * 24 * 30; 77 } 78 79 var expiresIn = TimeSpan.FromMinutes((int)cacheTime); 80 this._db.StringSet(key, entryBytes, expiresIn); 81 } 82 83 /// <summary> 84 /// 根据键获取值 85 /// </summary> 86 /// <typeparam name="T"></typeparam> 87 /// <param name="key"></param> 88 /// <returns></returns> 89 public virtual T Get<T>(string key) { 90 91 var rValue = this._db.StringGet(key); 92 if (!rValue.HasValue) { 93 return default(T); 94 } 95 96 var result = this.Deserialize<T>(rValue); 97 98 return result; 99 } 100 101 /// <summary> 102 /// 反序列化 103 /// </summary> 104 /// <typeparam name="T"></typeparam> 105 /// <param name="serializedObject"></param> 106 /// <returns></returns> 107 protected virtual T Deserialize<T>(byte[] serializedObject) { 108 if (serializedObject == null) { 109 return default(T); 110 } 111 112 var json = Encoding.UTF8.GetString(serializedObject); 113 return JsonConvert.DeserializeObject<T>(json); 114 } 115 116 /// <summary> 117 /// 判断是否已经设置 118 /// </summary> 119 /// <param name="key"></param> 120 /// <returns></returns> 121 public virtual bool IsSet(string key) { 122 return this._db.KeyExists(key); 123 } 124 125 /// <summary> 126 /// 序列化 127 /// </summary> 128 /// <param name="data"></param> 129 /// <returns>byte[]</returns> 130 private byte[] Serialize(object data) { 131 var json = JsonConvert.SerializeObject(data); 132 return Encoding.UTF8.GetBytes(json); 133 } 134 135 public virtual int GetHashCode<T>(T ojb) { 136 StringBuilder r = new StringBuilder(); 137 foreach (var item in ojb.GetType().GetProperties().ToList()) { 138 if (item.PropertyType.IsValueType || item.PropertyType.Name=="String") { 139 r.Append(item.GetValue(ojb)); 140 } 141 else if(!item.PropertyType.IsValueType && item.PropertyType.Name!="String" && !item.PropertyType.Name.StartsWith("ICollection")) { 142 var classItem = item.GetType().GetProperties().ToList(); 143 foreach (var info in classItem) { 144 if (info.PropertyType.IsValueType || info.PropertyType.Name == "String") { 145 r.Append(info.GetValue(ojb)); 146 } 147 } 148 } 149 } 150 return r.ToString().GetHashCode(); 151 } 152 }