redis 专题 (一)字符串操作
这章开始进入主题,讲redis如何存储。以下示例客户端均为 net core版本的,redis客户端为ServiceStack.Redis ,github地址:https://github.com/ServiceStack/ServiceStack.Redis
对于net来说,redis客户端一般使用 ServiceStack.Redis 或 StackExchange.Redis。但总体来说,ServiceStack.Redis性能更优。
以下内容,均基于 ServiceStack.Redis 作为操作的中间件。
客户端连接字符串支持以下几种格式:
localhost 127.0.0.1:6379 redis://localhost:6379 password@localhost:6379 clientid:password@localhost:6379 redis://clientid:password@localhost:6380?ssl=true&db=1
下面开始上示例代码
using (RedisClient client = new RedisClient("127.0.0.1", 6379)) { //删除当前数据库中的所有Key 默认删除的是db0 //client.FlushDb(); //删除所有数据库中的key //client.FlushAll(); //统计网站访问数量、当前在线人数、微博数、粉丝数等,全局递增ID等 #region 设置key的value //client.Set<string>("name", "科易网"); //Console.WriteLine("正确输出如下"); //Console.WriteLine(client.Get<string>("name")); //Console.WriteLine("第二种姿势输出"); //Console.WriteLine(client.GetValue("name")); //Console.WriteLine(JsonConvert.DeserializeObject<string>(client.GetValue("name"))); #endregion #region 设置多个key的value //批量的写入redis key client.SetAll(new Dictionary<string, string> { { "id", "001" }, { "name", "张无忌" } }); //批量读取内存中多个key的结果 如果我们获取的key不存在,程序会返回一个空的字符串 // 来判断当前用户是否是老用户 var getall = client.GetAll<string>(new string[] { "id", "name", "number" }); foreach (var item in getall) { Console.WriteLine(item.Key+":"+item.Value); } #endregion #region 设置key的value并设置过期时间 //client.Set<string>("name", "科易网", TimeSpan.FromSeconds(1)); //Task.Delay(1 * 1000).Wait(); //Console.WriteLine(client.Get<string>("name")); #endregion #region 设置key的value并设置过期时间 //client.Set<string>("name", "科易网", DateTime.Now.AddSeconds(1)); ////client.Set<string>("name", "科易网", DateTime.Now.AddDays(1)); //Console.WriteLine("刚写进去的结果"); //Console.WriteLine(client.Get<string>("name")); //Task.Delay(1 * 1000).Wait(); //Console.WriteLine("1秒钟之后的结果"); //Console.WriteLine(client.Get<string>("name")); //client.Set<string>("class", "优秀班级", TimeSpan.FromSeconds(10)); //Task.Delay(1 * 1000).Wait(); //Console.WriteLine(client.Get<string>("class")); #endregion #region 在原有key的value值之后追加value //client.AppendToValue("name", "I"); //client.AppendToValue("name", " "); //client.AppendToValue("name", "LOVE YOU"); //Console.WriteLine(client.Get<string>("name")); #endregion #region 获取旧值赋上新值 //client.Set("name", "科易网"); ////获取当前key的之前的值,然后把新的结果替换进入 //var value = client.GetAndSetValue("name", "张无忌"); //Console.WriteLine("原先的值"+value); //Console.WriteLine("新值"+client.GetValue("name")); #endregion #region 自增1,返回自增后的值 //给key为sid的键自增1 ,返回了自增之后的结果 //Console.WriteLine(client.Incr("sid")); //Console.WriteLine(client.Incr("sid")); //Console.WriteLine(client.Incr("sid")); //Console.WriteLine("华丽丽的结束"); //Console.WriteLine(client.GetValue("sid")); //每次通过传递的count累计,count就是累加的值 //client.IncrBy("sid", 2); //Console.WriteLine(client.Get<string>("sid")); //client.IncrBy("sid", 100); //Console.WriteLine("最后的结果***"+client.GetValue("sid")); #endregion #region 自减1,返回自减后的值 //Console.WriteLine(client.Decr("sid")); //Console.WriteLine(client.Decr("sid")); //Console.WriteLine(client.Decr("sid")); //Console.WriteLine("最后的结果"+client.GetValue("sid")); ////通过传入的count去做减肥 之前的结果-count //client.DecrBy("sid", 2); //Console.WriteLine("最终的结果"+client.GetValue("sid")); #endregion #region add 和set 的区别? // 当使用add 方法去操作redis的时候,如果key存在的话,则不会再次进行操作 返回false 如果操作成功返回true //Console.WriteLine(client.Add("name", "张无忌")); //Console.WriteLine(client.Add("name", "你很好张无忌")); //Console.WriteLine(client.Get<string>("name")); //使用set去操作 redis的时候,如果key不存在则写入当前值,并且返回true,通过存在,则对之前的值进行了一个替换 返回操作的结果 //Console.WriteLine(client.Set("name", "张无忌")); //Console.WriteLine(client.Set("name", "你很好张无忌")); //Console.WriteLine(client.Get<string>("name")); #endregion }
.Net操作Redis数据类型String
public class DoRedisString : DoRedisBase { #region 赋值 /// <summary> /// 设置key的value /// </summary> public bool Set(string key, string value) { return RedisBase.Core.Set<string>(key, value); } /// <summary> /// 设置key的value并设置过期时间 /// </summary> public bool Set(string key, string value, DateTime dt) { return RedisBase.Core.Set<string>(key, value, dt); } /// <summary> /// 设置key的value并设置过期时间 /// </summary> public bool Set(string key, string value, TimeSpan sp) { return RedisBase.Core.Set<string>(key, value, sp); } /// <summary> /// 设置多个key/value /// </summary> public void Set(Dictionary<string, string> dic) { RedisBase.Core.SetAll(dic); } #endregion #region 追加 /// <summary> /// 在原有key的value值之后追加value /// </summary> public long Append(string key, string value) { return RedisBase.Core.AppendToValue(key, value); } #endregion #region 获取值 /// <summary> /// 获取key的value值 /// </summary> public string Get(string key) { return RedisBase.Core.GetValue(key); } /// <summary> /// 获取多个key的value值 /// </summary> public List<string> Get(List<string> keys) { return RedisBase.Core.GetValues(keys); } /// <summary> /// 获取多个key的value值 /// </summary> public List<T> Get<T>(List<string> keys) { return RedisBase.Core.GetValues<T>(keys); } #endregion #region 获取旧值赋上新值 /// <summary> /// 获取旧值赋上新值 /// </summary> public string GetAndSetValue(string key, string value) { return RedisBase.Core.GetAndSetValue(key, value); } #endregion #region 辅助方法 /// <summary> /// 获取值的长度 /// </summary> public long GetCount(string key) { return RedisBase.Core.GetStringCount(key); } /// <summary> /// 自增1,返回自增后的值 /// </summary> public long Incr(string key) { return RedisBase.Core.IncrementValue(key); } /// <summary> /// 自增count,返回自增后的值 /// </summary> public double IncrBy(string key, double count) { return RedisBase.Core.IncrementValueBy(key, count); } /// <summary> /// 自减1,返回自减后的值 /// </summary> public long Decr(string key) { return RedisBase.Core.DecrementValue(key); } /// <summary> /// 自减count ,返回自减后的值 /// </summary> /// <param name="key"></param> /// <param name="count"></param> /// <returns></returns> public long DecrBy(string key, int count) { return RedisBase.Core.DecrementValueBy(key, count); } #endregion }