Redis的初識


簡介

  已經有了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

第一步:下載后解壓本地磁盤上(注:目錄不能包括中文)

 

第二步: 定位到解壓redis目錄下(cmd)

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工具)

 第三步:啟動服務

  我們也可以啟動前配置下Redis,詳細配置請看:https://www.runoob.com/redis/redis-conf.html(配置文件:redis.windows.conf)

  這里為了演示,直接用默認配置啟動即可

 

 配置文件參數

 

 

 

 

 

 

 

 

 

 

 

啟動方式2

 啟動方式3(以Window服務方式)

 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

 第四步:連接Redis

 連上!!

 第五步:SET/GET

以下C#控制台代碼演示

第一步:NuGet下載DLL類庫

 注:安裝最新版本的,項目框架應用高版本的,低版本的框架,可能不兼容高版本Redis

 不會下載的朋友,請到我百度雲盤下載:

鏈接:https://pan.baidu.com/s/1-Wzv0tnoXhi6XMkhv_90gw
提取碼:e9at

第二步:引入類庫

 第三步:引入命名空間

1 using ServiceStack.Redis;

第四步:實現(控制台)

  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 }

 項目源碼:

鏈接:https://pan.baidu.com/s/1LmnGrRHQkZbHxEM3KDfzwA
提取碼:tbkc

覺得對你有幫助的話,幫忙推薦下,有不懂的地方,歡迎下方留言!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM