Redis入门学习
Posted likeheart
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis入门学习相关的知识,希望对你有一定的参考价值。
一、软件安装
Redis下载地址:https://github.com/MSOpenTech/redis/releases
因为官方并不支持windows系统,需要从微软的GitHub上下载。
解压缩后文件夹内容如图所示(版本3.2.100):
最开始会用到的文件有redis-server.exe、redis-cli.exe以及一个配置文件redis.windows.conf。
1)启动redis服务,运行cmd.exe
进入到redis文件夹
cd desttop/redis
启动redis-server.exe 并使用配置文件,出现如下图所示就是启动成功了。
redis-server.exe redis.windows.conf
2)启动客户端,再打开一个cmd.exe
进入到文件夹后启动redis-cli.exe
redis-cli.exe
国际惯例,ping helloworld ,解锁熟练掌握redis的成就。
3)为什么默认使用6379端口
Redis作者antirez同学在twitter上说将在下一篇博文(http://oldblog.antirez.com/post/redis-as-LRU-cache.html)中向大家解释为什么他选择6379作为默认端口号。而现在这篇博文出炉,在解释了Redis的LRU机制之后,如期向大家解释了采用6379作为默认端口的原因。6379在是手机按键上MERZ对应的号码,而MERZ取自意大利歌女Alessia Merz的名字。
二、基本用法
redis是以kev-value形式进行数据存储的,value有字符串、哈希表、列表、集合、有序集合等。
一些命令的使用,可以参考http://doc.redisfans.com/学习。
配置文件的使用,redis.windows-service.conf(以windows服务运行时修改),
1.密码修改
# requirepass foobared //去掉注释#,将foobared替换为你自己的密码
2.文件命名修改
dbfilename dump.rdb // xxxx.rdb
三、C#使用Redis
1)使用到的第三方dll:
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" /> <package id="StackExchange.Redis" version="1.2.6" targetFramework="net45" />
2)搜集到的RedisHelper方法,增加了批量操作
1 /// <summary> 2 /// Redis操作 3 /// </summary> 4 public class RedisHelper 5 { 6 /// <summary> 7 /// 连接redis库的Number 8 /// </summary> 9 private int DbNum { set; get; } 10 private readonly ConnectionMultiplexer _conn; 11 /// <summary> 12 /// 自定义键前缀 13 /// </summary> 14 public string CustomKey; 15 16 #region 构造函数 17 18 public RedisHelper(int dbNum = 0) 19 : this(dbNum, null) 20 { 21 } 22 23 public RedisHelper(int dbNum, string readWriteHosts) 24 { 25 DbNum = dbNum; 26 _conn = 27 string.IsNullOrWhiteSpace(readWriteHosts) ? 28 RedisConnectionHelp.Instance : 29 RedisConnectionHelp.GetConnectionMultiplexer(readWriteHosts); 30 } 31 32 #endregion 构造函数 33 34 #region String 35 36 #region 同步方法 37 38 /// <summary> 39 /// 保存单个key value 40 /// </summary> 41 /// <param name="key">Redis Key</param> 42 /// <param name="value">保存的值</param> 43 /// <param name="expiry">过期时间</param> 44 /// <returns></returns> 45 public bool StringSet(string key, string value, TimeSpan? expiry = default(TimeSpan?)) 46 { 47 key = AddSysCustomKey(key); 48 return Do(db => db.StringSet(key, value, expiry)); 49 } 50 51 /// <summary> 52 /// 保存多个key value 53 /// </summary> 54 /// <param name="keyValues">键值对</param> 55 /// <returns></returns> 56 public bool StringSet(List<KeyValuePair<RedisKey, RedisValue>> keyValues) 57 { 58 List<KeyValuePair<RedisKey, RedisValue>> newkeyValues = 59 keyValues.Select(p => new KeyValuePair<RedisKey, RedisValue>(AddSysCustomKey(p.Key), p.Value)).ToList(); 60 return Do(db => db.StringSet(newkeyValues.ToArray())); 61 } 62 63 /// <summary> 64 /// 保存一个对象 65 /// </summary> 66 /// <typeparam name="T"></typeparam> 67 /// <param name="key"></param> 68 /// <param name="obj"></param> 69 /// <param name="expiry"></param> 70 /// <returns></returns> 71 public bool StringSet<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?)) 72 { 73 key = AddSysCustomKey(key); 74 string json = ConvertJson(obj); 75 return Do(db => db.StringSet(key, json, expiry)); 76 } 77 78 /// <summary> 79 /// 获取单个key的值 80 /// </summary> 81 /// <param name="key">Redis Key</param> 82 /// <returns></returns> 83 public byte[] StringGet(string key) 84 { 85 key = AddSysCustomKey(key); 86 return Do(db => db.StringGet(key)); 87 } 88 89 /// <summary> 90 /// 获取多个Key 91 /// </summary> 92 /// <param name="listKey">Redis Key集合</param> 93 /// <returns></returns> 94 public RedisValue[] StringGet(List<string> listKey) 95 { 96 List<string> newKeys = listKey.Select(AddSysCustomKey).ToList(); 97 return Do(db => db.StringGet(ConvertRedisKeys(newKeys))); 98 } 99 100 /// <summary> 101 /// 获取一个key的对象 102 /// </summary> 103 /// <typeparam name="T"></typeparam> 104 /// <param name="key"></param> 105 /// <returns></returns> 106 public T StringGet<T>(string key) 107 { 108 key = AddSysCustomKey(key); 109 return Do(db => ConvertObj<T>(db.StringGet(key))); 110 } 111 112 public object StringGetObj(string key) 113 { 114 key = AddSysCustomKey(key); 115 var database = _conn.GetDatabase(DbNum); 116 return database.StringGet(key); 117 } 118 119 public static object GetObjFromBytes(byte[] buffer) 120 { 121 using (System.IO.MemoryStream stream = new System.IO.MemoryStream(buffer)) 122 { 123 stream.Position = 0; 124 System.Runtime.Serialization.IFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); 125 Object reobj = bf.Deserialize(stream); 126 return reobj; 127 } 128 } 129 130 /// <summary> 131 /// 为数字增长val 132 /// </summary> 133 /// <param name="key"></param> 134 /// <param name="val">可以为负</param> 135 /// <returns>增长后的值</returns> 136 public double StringIncrement(string key, double val = 1) 137 { 138 key = AddSysCustomKey(key); 139 return Do(db => db.StringIncrement(key, val)); 140 } 141 142 /// <summary> 143 /// 为数字减少val 144 /// </summary> 145 /// <param name="key"></param> 146 /// <param name="val">可以为负</param> 147 /// <returns>减少后的值</returns> 148 public double StringDecrement(string key, double val = 1) 149 { 150 key = AddSysCustomKey(key); 151 return Do(db => db.StringDecrement(key, val)); 152 } 153 154 #endregion 同步方法 155 156 #region 异步方法 157 158 /// <summary> 159 /// 保存单个key value 160 /// </summary> 161 /// <param name="key">Redis Key</param> 162 /// <param name="value">保存的值</param> 163 /// <param name="expiry">过期时间</param> 164 /// <returns></returns> 165 public async Task<bool> StringSetAsync(string key, string value, TimeSpan? expiry = default(TimeSpan?)) 166 { 167 key = AddSysCustomKey(key); 168 return await Do(db => db.StringSetAsync(key, value, expiry)); 169 } 170 171 /// <summary> 172 /// 保存多个key value 173 /// </summary> 174 /// <param name="keyValues">键值对</param> 175 /// <returns></returns> 176 public async Task<bool> StringSetAsync(List<KeyValuePair<RedisKey, RedisValue>> keyValues) 177 { 178 List<KeyValuePair<RedisKey, RedisValue>> newkeyValues = 179 keyValues.Select(p => new KeyValuePair<RedisKey, RedisValue>(AddSysCustomKey(p.Key), p.Value)).ToList(); 180 return await Do(db => db.StringSetAsync(newkeyValues.ToArray())); 181 } 182 183 /// <summary> 184 /// 保存一个对象 185 /// </summary> 186 /// <typeparam name="T"></typeparam> 187 /// <param name="key"></param> 188 /// <param name="obj"></param> 189 /// <param name="expiry"></param> 190 /// <returns></returns> 191 public async Task<bool> StringSetAsync<T>(string key, T obj, TimeSpan? expiry = default(TimeSpan?)) 192 { 193 key = AddSysCustomKey(key); 194 string json = ConvertJson(obj); 195 return await Do(db => db.StringSetAsync(key, json, expiry)); 196 } 197 198 /// <summary> 199 /// 获取单个key的值 200 /// </summary> 201 /// <param name="key">Redis Key</param> 202 /// <returns></returns> 203 public async Task<string> StringGetAsync(string key) 204 { 205 key = AddSysCustomKey(key); 206 return await Do(db => db.StringGetAsync(key)); 207 } 208 209 /// <summary> 210 /// 获取多个Key 211 /// </summary> 212 /// <param name="listKey">Redis Key集合</param> 213 /// <returns></returns> 214 public async Task<RedisValue[]> StringGetAsync(List<string> listKey) 215 { 216 List<string> newKeys = listKey.Select(AddSysCustomKey).ToList(); 217 return await Do(db => db.StringGetAsync(ConvertRedisKeys(newKeys))); 218 } 219 220 /// <summary> 221 /// 获取一个key的对象 222 /// </summary> 223 /// <typeparam name="T"></typeparam> 224 /// <param name="key"></param> 225 /// <returns></returns> 226 public async Task<T> StringGetAsync<T>(string key) 227 { 228 key = AddSysCustomKey(key); 229 string result = await Do(db => db.StringGetAsync(key)); 230 return ConvertObj<T>(result); 231 } 232 233 /// <summary> 234 /// 为数字增长val 235 /// </summary> 236 /// <param name="key"></param> 237 /// <param name="val">可以为负</param> 238 /// <returns>增长后的值</returns> 239 public async Task<double> StringIncrementAsync(string key, double val = 1) 240 { 241 key = AddSysCustomKey(key); 242 return await Do(db => db.StringIncrementAsync(key, val)); 243 } 244 245 /// <summary> 246 /// 为数字减少val 247 /// </summary> 248 /// <param name="key"></param> 249 /// <param name="val">可以为负</param> 250 /// <returns>减少后的值</returns> 251 public async Task<double> StringDecrementAsync(string key, double val = 1) 252 { 253 key = AddSysCustomKey(key); 254 return await Do(db => db.StringDecrementAsync(key, val)); 255 } 256 257 #endregion 异步方法 258 259 #endregion String 260 261 #region Hash 262 263 #region 同步方法 264 265 /// <summary> 266 /// 判断某个数据是否已经被缓存 267 /// </summary> 268 /// <param name="key"></param> 269 /// <param name="dataKey"></param> 270 /// <returns></returns> 271 public bool HashExists(string key, string dataKey) 272 { 273 key = AddSysCustomKey(key); 274 return Do(db => db.HashExists(key, dataKey)); 275 } 276 277 /// <summary> 278 /// 存储数据到hash表 279 /// </summary> 280 /// <typeparam name="T"></typeparam> 281 /// <param name="key"></param> 282 /// <param name="dataKey"></param> 283 /// <param name="t"></param> 284 /// <returns></returns> 285 public bool HashSet<T>(string key, string dataKey, T t) 286 { 287 key = AddSysCustomKey(key); 288 return Do(db => 289 { 290 string json = ConvertJson(t); 291 return db.HashSet(key, dataKey, json); 292 }); 293 } 294 295 /// <summary> 296 /// 移除hash中的某值 297 /// </summary> 298 /// <param name="key"></param> 299 /// <param name="dataKey"></param> 300 /// <returns></returns> 301 public bool HashDelete(string key, string dataKey) 302 { 303 key = AddSysCustomKey(key); 304 return Do(db => db.HashDelete(key, dataKey)); 305 } 306 307 /// <summary> 308 /// 移除hash中的多个值 309 /// </summary> 310 /// <param name="key"></param> 311 /// <param name="dataKeys"></param> 312 /// <returns></returns> 313 public long HashDelete(string key, List<RedisValue> dataKeys) 314 { 315 key = AddSysCustomKey(key); 316 //List<RedisValue> dataKeys1 = new List<RedisValue>() {"1","2"}; 317 return Do(db => db.HashDelete(key, dataKeys.ToArray())); 318 } 319 320 /// <summary> 321 /// 从hash表获取数据 322 /// </summary> 323 /// <typeparam name="T"></typeparam> 324 /// <param name="key"></param> 325 /// <param name="dataKey"></param> 326 /// <returns></returns> 327 public T HashGet<T>(string key, string dataKey) 328 { 329 key = AddSysCustomKey(key); 330 return Do(db => 331 Redis学习 - 入门