ibcadmin 发表于 2019-9-12 16:24:08

Redis的初识

<h1>简介</h1>
<p>  已经有了Membercache和各种数据库,Redis为什么会产生?Redis纯粹为应用而产生,它是一个高性能的key-value数据库。Redis的出现,很大步伐赔偿了Memcached这类key-value存储的不敷,办理了断电后数据库完全丢失的情况;在部分场合可以对关系数据库起到很好的赔偿作用。性能测试结果体现SET操作每秒钟可达110000,GET操作每秒81000次(当然差别的服务器配置性能差别)。</p>
<p>  Redis是一种面向"键-值"对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存储,顺应高并发的应用场景。和Memcache雷同,它支持存储的value类型相对更多,包罗string(字符串)、list(链表)、set(聚集)和zset(有序聚集)。这些数据类型支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的,支持各种差别方式的排序。<strong>Redis与Memcache一样,为了保证服从,数据都是缓存在内存中,区别的是Redis会周期性的把更新的数据写入磁盘或者修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。</strong></p>
<p>  Redis目条件供四种数据类型:string、list、set、zset</p>
<p>  Redis的存储分为内存存储、磁盘存储和log文件三部分,配置文件中有三个参数对其举行配置。</p>
<p>    1、save seconds updates:指出在多长时间内,有多少次更新操作,就将数据同步到数据文件。</p>
<p>    2、appendonly yes/no:是否在每次更新操作后举行日志记录。假如不开启,大概会在断电时导致一段时间内的数据丢失。因为Redis本身数据同步文件是按上面save条件来同步的,所以有的数据会在一段时间内只存在内存中。</p>
<p>    3、appendfsync no/always/everysec:数据缓存同步至磁盘的方式。no体现等操作系统举行数据缓存同步到磁盘,always体现每次更新操作后手动调用fsync()将数据写到磁盘,everysec体现每秒同步一次。</p>
<h1> 安装及利用</h1>
<p>下载地址:https://github.com/microsoftarchive/redis/releases</p>
<p>百度云盘:</p>
<p>链接:https://pan.baidu.com/s/1ObkTyQ5hrCYoVGWkqanfFQ <br />提取码:d3yo</p>
<h2>第一步:下载后解压当地磁盘上(注:目录不能包罗中文)</h2>
<p> <div align="center"></div></p>
<h2>第二步: 定位到解压redis目录下(cmd)</h2>
<p><div align="center"></div></p>

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

<h2> 第三步:启动服务</h2>
<p>  我们也可以启动前配置下Redis,具体配置请看:https://www.runoob.com/redis/redis-conf.html(<strong>配置文件:redis.windows.conf</strong>)</p>
<p>  这里为了演示,直接用默认配置启动即可</p>
<p><div align="center"></div></p>
<p> <div align="center"></div></p>
<p><strong> 配置文件参数</strong></p>
<p><div align="center"></div></p>
<p> </p>
<p> <div align="center"></div></p>
<p> </p>
<p> <div align="center"></div></p>
<p> </p>
<p> <div align="center"></div></p>
<p> </p>
<p> <div align="center"></div></p>
<p> </p>
<p> <div align="center"></div></p>
<p> </p>
<h2>启动方式2</h2>
<p><div align="center"></div></p>
<h2> 启动方式3(以Window服务方式)</h2>
<div align="center"></div><div align="center"></div>

1 安装(redis-install.bat)
2 echo install redis-server
3 D:\redis\redis-server.exe --service-install D:\redis\redis.windows.conf --loglevel verbose
4
5
6 卸载(redis-uninstall.bat)
7 echo uninstall redis-server
8 D:\redis\redis-server.exe --service-uninstall
9
10 启动(start-redis.bat)
11 echo start redis-server
12 D:\redis\redis-server.exe D:\redis\redis.windows.conf

bat脚本

1 格式:redis-server --service-install redis.windows.conf

<p><div align="center"></div></p>
<p><div align="center"></div></p>
<h2> 第四步:连接Redis</h2>
<p><div align="center"></div></p>
<p><strong> 连上!!</strong></p>
<p><div align="center"></div></p>
<h2> 第五步:SET/GET</h2>
<p><div align="center"></div></p>
<p><strong>以下C#控制台代码演示</strong></p>
<h3>第一步:NuGet下载DLL类库</h3>
<p><div align="center"></div></p>
<p> 注:安装最新版本的,项目框架应用高版本的,低版本的框架,大概不兼容高版本Redis<div align="center"></div></p>
<p> 不会下载的朋侪,请到我百度云盘下载:</p>
<p>链接:https://pan.baidu.com/s/1-Wzv0tnoXhi6XMkhv_90gw <br />提取码:e9at</p>
<h3>第二步:引入类库</h3>
<p><div align="center"></div></p>
<h3> 第三步:引入定名空间</h3>

1 using ServiceStack.Redis;

<h3>第四步:实现(<strong>控制台</strong>)</h3>

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

<p><div align="center"></div></p>
<p> 项目源码:</p>
<p>链接:https://pan.baidu.com/s/1LmnGrRHQkZbHxEM3KDfzwA <br />提取码:tbkc </p>
<p>觉得对你有资助的话,帮忙保举下,有不懂的地方,欢迎下方留言!!</p>
<p> </p><br><br/><br/><br/><br/><br/>来源:<a href="https://www.cnblogs.com/chenyanbin/p/11443421.html" target="_blank">https://www.cnblogs.com/chenyanbin/p/11443421.html</a>
页: [1]
查看完整版本: Redis的初识