请选择 进入手机版 | 继续访问电脑版

马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

Redis的初识

[复制链接]
查看2356 | 回复0 | 2019-9-12 16:24:08 | 显示全部楼层 |阅读模式

简介

  已经有了Membercache和各种数据库,Redis为什么会产生?Redis纯粹为应用而产生,它是一个高性能的key-value数据库。Redis的出现,很大步伐赔偿了Memcached这类key-value存储的不敷,办理了断电后数据库完全丢失的情况;在部分场合可以对关系数据库起到很好的赔偿作用。性能测试结果体现SET操作每秒钟可达110000,GET操作每秒81000次(当然差别的服务器配置性能差别)。

  Redis是一种面向"键-值"对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存储,顺应高并发的应用场景。和Memcache雷同,它支持存储的value类型相对更多,包罗string(字符串)、list(链表)、set(聚集)和zset(有序聚集)。这些数据类型支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的,支持各种差别方式的排序。Redis与Memcache一样,为了保证服从,数据都是缓存在内存中,区别的是Redis会周期性的把更新的数据写入磁盘或者修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。

  Redis目条件供四种数据类型:string、list、set、zset

  Redis的存储分为内存存储、磁盘存储和log文件三部分,配置文件中有三个参数对其举行配置。

    1、save seconds updates:指出在多长时间内,有多少次更新操作,就将数据同步到数据文件。

    2、appendonly yes/no:是否在每次更新操作后举行日志记录。假如不开启,大概会在断电时导致一段时间内的数据丢失。因为Redis本身数据同步文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在内存中。

    3、appendfsync no/always/everysec:数据缓存同步至磁盘的方式。no体现等操作系统举行数据缓存同步到磁盘,always体现每次更新操作后手动调用fsync()将数据写到磁盘,everysec体现每秒同步一次。

安装及利用

下载地址:https://github.com/microsoftarchive/redis/releases

百度云盘:

链接:https://pan.baidu.com/s/1ObkTyQ5hrCYoVGWkqanfFQ
提取码:d3yo

第一步:下载后解压当地磁盘上(注:目录不能包罗中文)

162418e2plvxx29li5q1yi.png

第二步: 定位到解压redis目录下(cmd)

162418uulcls2caa8rrcpc.png

  1. 1 1、redis-server.exe redis服务器的daemon启动步伐
  2. 2 2、redis.windows.conf redis配置文件
  3. 3 3、redis-cli.exe redis下令行操作工具
  4. 4 4、redis-check-dump.exe 当地数据库查抄
  5. 5 5、redis-check-aof.exe 更新日志查抄
  6. 6 6、redis-benchmark.exe 性能测试,用于模拟同时由N个客户端发送M个 SETs/GETs查询(雷同于Apache ab工具)
复制代码

第三步:启动服务

  我们也可以启动前配置下Redis,具体配置请看:https://www.runoob.com/redis/redis-conf.html(配置文件:redis.windows.conf)

  这里为了演示,直接用默认配置启动即可

162419gq1ds7qzxvzwldag.png

162420m8t5hq51q2ife5jq.png

配置文件参数

162420velill6rldrfazlj.png

162421mxw2xrw9w2ww91qa.png

162422cpcia0anpq1pw5k5.png

162422ct8duudu71j8g901.png

162423gwsjh4mlgzlfbhgg.png

162424te48h2qcz7ch4o17.png

启动方式2

162425nn6xt3egezg2w8r3.png

启动方式3(以Window服务方式)

162425gvfsbppryf1pfcb2.gif
162425r3iz4sslyiiydf6b.gif
  1. 1 安装(redis-install.bat)
  2. 2 echo install redis-server
  3. 3 D:\redis\redis-server.exe --service-install D:\redis\redis.windows.conf --loglevel verbose
  4. 4
  5. 5
  6. 6 卸载(redis-uninstall.bat)
  7. 7 echo uninstall redis-server
  8. 8 D:\redis\redis-server.exe --service-uninstall
  9. 9
  10. 10 启动(start-redis.bat)
  11. 11 echo start redis-server
  12. 12 D:\redis\redis-server.exe D:\redis\redis.windows.conf
复制代码
bat脚本
  1. 1 格式:redis-server --service-install redis.windows.conf
复制代码

162426uqkg6gwwft3eeqbm.png

162426d9cf9aplfacxlfc9.png

第四步:连接Redis

162427z111wx1pdb7b7wgd.png

连上!!

162428ub99vvr9wghrchbn.png

第五步:SET/GET

162428buyfpvdlcfv4azd6.png

以下C#控制台代码演示

第一步:NuGet下载DLL类库

162429zqfm8za69jwzbq98.png

注:安装最新版本的,项目框架应用高版本的,低版本的框架,大概不兼容高版本Redis

162430ifj9zfsalfazarno.png

不会下载的朋侪,请到我百度云盘下载:

链接:https://pan.baidu.com/s/1-Wzv0tnoXhi6XMkhv_90gw
提取码:e9at

第二步:引入类库

162430a8kkxgxkkxu6x46g.png

第三步:引入定名空间

  1. 1 using ServiceStack.Redis;
复制代码

第四步:实现(控制台)

  1. 1 using ServiceStack.Redis;
  2. 2 using System;
  3. 3 using System.Collections.Generic;
  4. 4 using System.Linq;
  5. 5 using System.Text;
  6. 6 using System.Threading;
  7. 7
  8. 8 namespace RedisDemo
  9. 9 {
  10. 10 class Program
  11. 11 {
  12. 12 static void Main(string[] args)
  13. 13 {
  14. 14 //在Redis中存储常用的5种数据类型:String,Hash,List,SetSorted set
  15. 15
  16. 16 RedisClient client = new RedisClient("192.168.1.102", 6379);
  17. 17
  18. 18 client.FlushAll();
  19. 19
  20. 20 //-----string开始----------
  21. 21 client.Add<string>("StringValueTime", "我已设置过期时间噢30秒后会消失", DateTime.Now.AddMilliseconds(30000));
  22. 22 while (true)
  23. 23 {
  24. 24 if (client.ContainsKey("StringValueTime"))
  25. 25 {
  26. 26 Console.WriteLine("String.键:StringValue,值:{0} {1}", client.Get<string>("StringValueTime"), DateTime.Now);
  27. 27 Thread.Sleep(10000);
  28. 28 }
  29. 29 else
  30. 30 {
  31. 31 Console.WriteLine("键:StringValue,值:我已过期 {0}", DateTime.Now);
  32. 32 break;
  33. 33 }
  34. 34 }
  35. 35
  36. 36 client.Add<string>("StringValue", " String和Memcached操作方法差不多");
  37. 37 Console.WriteLine("数据类型为:String.键:StringValue,值:{0}", client.Get<string>("StringValue"));
  38. 38
  39. 39 Student stud = new Student() { id = "1001", name = "李四" };
  40. 40 client.Add<Student>("StringEntity", stud);
  41. 41 Student Get_stud = client.Get<Student>("StringEntity");
  42. 42 Console.WriteLine("数据类型为:String.键:StringEntity,值:{0} {1}", Get_stud.id, Get_stud.name);
  43. 43 //-----string竣事----------
  44. 44
  45. 45 //---------Hash开始---------------
  46. 46 client.SetEntryInHash("HashID", "Name", "张三");
  47. 47 client.SetEntryInHash("HashID", "Age", "24");
  48. 48 client.SetEntryInHash("HashID", "Sex", "男");
  49. 49 client.SetEntryInHash("HashID", "Address", "上海市XX号XX室");
  50. 50
  51. 51 List<string> HaskKey = client.GetHashKeys("HashID");
  52. 52 foreach (string key in HaskKey)
  53. 53 {
  54. 54 Console.WriteLine("HashID--Key:{0}", key);
  55. 55 }
  56. 56
  57. 57 List<string> HaskValue = client.GetHashValues("HashID");
  58. 58 foreach (string value in HaskValue)
  59. 59 {
  60. 60 Console.WriteLine("HashID--Value:{0}", value);
  61. 61 }
  62. 62
  63. 63 List<string> AllKey = client.GetAllKeys(); //获取所有的key。
  64. 64 foreach (string Key in AllKey)
  65. 65 {
  66. 66 Console.WriteLine("AllKey--Key:{0}", Key);
  67. 67 }
  68. 68 //---------Hash竣事---------------
  69. 69
  70. 70 //-----------List开始--------------
  71. 71 /*
  72. 72 * list是一个链表布局,重要功能是push,pop,获取一个范围的所有的值等,操作中key明白为链表名字。
  73. 73 * Redis的list类型其实就是一个每个子元素都是string类型的双向链表。我们可以通过push,pop操作从链表的头部或者尾部添加删除元素,
  74. 74 * 这样list既可以作为栈,又可以作为队列。Redis list的实现为一个双向链表,即可以支持反向查找和遍历,更方便操作,不过带来了部分额外的内存开销,
  75. 75 * Redis内部的许多实现,包罗发送缓冲队列等也都是用的这个数据布局
  76. 76 */
  77. 77 client.EnqueueItemOnList("QueueListId", "1.张三"); //入队
  78. 78 client.EnqueueItemOnList("QueueListId", "2.张四");
  79. 79 client.EnqueueItemOnList("QueueListId", "3.王五");
  80. 80 client.EnqueueItemOnList("QueueListId", "4.王麻子");
  81. 81 long q = client.GetListCount("QueueListId");
  82. 82 for (int i = 0; i < q; i++)
  83. 83 {
  84. 84 Console.WriteLine("QueueListId出队值:{0}", client.DequeueItemFromList("QueueListId")); //出队(队列先辈先出)
  85. 85 }
  86. 86
  87. 87 client.PushItemToList("StackListId", "1.张三"); //入栈
  88. 88 client.PushItemToList("StackListId", "2.张四");
  89. 89 client.PushItemToList("StackListId", "3.王五");
  90. 90 client.PushItemToList("StackListId", "4.王麻子");
  91. 91 long p = client.GetListCount("StackListId");
  92. 92 for (int i = 0; i < p; i++)
  93. 93 {
  94. 94 Console.WriteLine("StackListId出栈值:{0}", client.PopItemFromList("StackListId")); //出栈(栈先辈后出)
  95. 95 }
  96. 96 //-----------List竣事--------------
  97. 97
  98. 98 //----------Set无序聚集开始------------
  99. 99 /*
  100. 100 它是string类型的无序聚集。set是通过hash table实现的,添加,删除和查找,对聚集我们可以取并集,交集,差集
  101. 101 */
  102. 102 client.AddItemToSet("Set1001", "小A");
  103. 103 client.AddItemToSet("Set1001", "小B");
  104. 104 client.AddItemToSet("Set1001", "小C");
  105. 105 client.AddItemToSet("Set1001", "小D");
  106. 106 HashSet<string> hastsetA = client.GetAllItemsFromSet("Set1001");
  107. 107 foreach (string item in hastsetA)
  108. 108 {
  109. 109 Console.WriteLine("Set无序聚集ValueA:{0}", item); //出来的结果是无须的
  110. 110 }
  111. 111
  112. 112 client.AddItemToSet("Set1002", "小K");
  113. 113 client.AddItemToSet("Set1002", "小C");
  114. 114 client.AddItemToSet("Set1002", "小A");
  115. 115 client.AddItemToSet("Set1002", "小J");
  116. 116 HashSet<string> hastsetB = client.GetAllItemsFromSet("Set1002");
  117. 117 foreach (string item in hastsetB)
  118. 118 {
  119. 119 Console.WriteLine("Set无序聚集ValueB:{0}", item); //出来的结果是无须的
  120. 120 }
  121. 121
  122. 122 HashSet<string> hashUnion = client.GetUnionFromSets(new string[] { "Set1001", "Set1002" });
  123. 123 foreach (string item in hashUnion)
  124. 124 {
  125. 125 Console.WriteLine("求Set1001和Set1002的并集:{0}", item); //并集
  126. 126 }
  127. 127
  128. 128 HashSet<string> hashG = client.GetIntersectFromSets(new string[] { "Set1001", "Set1002" });
  129. 129 foreach (string item in hashG)
  130. 130 {
  131. 131 Console.WriteLine("求Set1001和Set1002的交集:{0}", item); //交集
  132. 132 }
  133. 133
  134. 134 HashSet<string> hashD = client.GetDifferencesFromSet("Set1001", new string[] { "Set1002" }); //[返回存在于第一个聚集,但是不存在于其他聚集的数据。差集]
  135. 135 foreach (string item in hashD)
  136. 136 {
  137. 137 Console.WriteLine("求Set1001和Set1002的差集:{0}", item); //差集
  138. 138 }
  139. 139 //----------Set无序聚集开始------------
  140. 140
  141. 141 //--------SetSorted 有序聚集开始-------
  142. 142 /*
  143. 143 sorted set 是set的一个升级版本,它在set的基础上增长了一个序次的属性,这一属性在添加修改.元素的时间可以指定,
  144. 144 * 每次指定后,zset(体现有序聚集)会自动重新按新的值调整序次。可以明白为有列的表,一列存 value,一列存序次。操作中key明白为zset的名字.
  145. 145 */
  146. 146 client.AddItemToSortedSet("SetSorted1001", "1.刘仔");
  147. 147 client.AddItemToSortedSet("SetSorted1001", "2.星仔");
  148. 148 client.AddItemToSortedSet("SetSorted1001", "3.猪仔");
  149. 149 List<string> listSetSorted = client.GetAllItemsFromSortedSet("SetSorted1001");
  150. 150 foreach (string item in listSetSorted)
  151. 151 {
  152. 152 Console.WriteLine("SetSorted有序聚集{0}", item);
  153. 153 }
  154. 154 //--------SetSorted 有序聚集开始-------
  155. 155 Console.ReadKey();
  156. 156 }
  157. 157 public class Student
  158. 158 {
  159. 159 public string id { get; set; }
  160. 160 public string name { get; set; }
  161. 161 }
  162. 162 }
  163. 163 }
复制代码

162431jyvsei8izhg7yyns.png

项目源码:

链接:https://pan.baidu.com/s/1LmnGrRHQkZbHxEM3KDfzwA
提取码:tbkc

觉得对你有资助的话,帮忙保举下,有不懂的地方,欢迎下方留言!!







来源:https://www.cnblogs.com/chenyanbin/p/11443421.html
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则