redis的官網地址,是http://www.javashuo.com/tag/redis.io。mysql
Redis 是一個高性能的key-value數據庫。git
和Memcached相似,它支持存儲的value類型相對更多,包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(哈希類型)。github
這些數據類型都支持push/pop、add/remove及取交集並集和差集及更豐富的操做,並且這些操做都是原子性的。在此基礎上,redis支持各類不一樣方式的排序。redis
與memcached同樣,爲了保證效率,數據都是緩存在內存中。區別的是redis會週期性的把更新的數據寫入磁盤或者把修改操做寫入追加的記錄文件,而且在此基礎上實現了master-slave(主從)同步。數據能夠從主服務器向任意數量的從服務器上同步,從服務器能夠是關聯其餘從服務器的主服務器。這使得Redis可執行單層樹複製。sql
全量數據和增量請求。數據庫
全量數據格式是把內存中的數據寫入磁盤,便於下次讀取文件進行加載;緩存
增量請求文件則是把內存中的數據序列化爲操做請求,用於讀取文件進行replay獲得數據,序列化的操做包括SET、RPUSH、SADD、ZADD。服務器
Redis的存儲分爲內存存儲、磁盤存儲和log文件三部分,配置文件中有三個參數對其進行配置。ide
Redis的代碼遵循ANSI-C編寫,能夠在全部POSIX系統(如Linux, *BSD, Mac OS X, Solaris等)上安裝運行。並且Redis並不依賴任何非標準庫,也沒有編譯參數必需添加。memcached
下面介紹一下Windows安裝。
一、安裝包下載:https://github.com/dmajkic/redis/downloads
二、安裝包下載後根據操做系統選擇對應版本文件,裏面會有幾個dll分別爲:
redis-server.exe:服務程序
redis-check-dump.exe:本地數據庫檢查
redis-check-aof.exe:更新日誌檢查
redis-benchmark.exe:性能測試,用以模擬同時由N個客戶端發送M個 SETs/GETs 查詢.
redis-cli.exe: 服務端開啓後,咱們的客戶端就能夠輸入各類命令測試了
三、使用
打開cmd (win+R),進入到安裝包下載的位置。輸入:redis-server.exe redis.conf 開啓Redis服務。雙擊redis-server.exe文件也能夠啓動服務。
提示信息沒有報錯表示啓動成功。
此窗口要一直保持打開狀態,若是關閉Redis服務也會被關閉。
再打開一個cmd窗口,使用redis-cli.exe測試一下數據。
Redis客戶端還可下載https://github.com/rgl/redis/downloads,雙擊exe安裝便可
關閉Redis服務命令redis-cli SHUTDOWN
Redis根本是使用內存存儲,持久化的關鍵是這三條指令:SAVE BGSAVE LASTSAVE …
TYPE key — 用來獲取某key的類型
KEYS pattern — 匹配全部符合模式的key,好比KEYS * 就列出全部的key了,固然,複雜度O(n)
RANDOMKEY - 返回隨機的一個key
RENAME oldkeynewkey— key也能夠更名
列表操做,精華
RPUSH key string — 將某個值加入到一個key列表末尾
LPUSH key string — 將某個值加入到一個key列表頭部
LLEN key — 列表長度
LRANGE key start end — 返回列表中某個範圍的值,至關於mysql裏面的分頁查詢那樣
LTRIM key start end — 只保留列表中某個範圍的值
LINDEX key index — 獲取列表中特定索引號的值,要注意是O(n)複雜度
LSET key index value — 設置列表中某個位置的值
LPOP key
RPOP key — 和上面的LPOP同樣,就是相似棧或隊列的那種取頭取尾指令,能夠當成消息隊列來使用了
集合操做
SADD key member — 增長元素
SREM key member — 刪除元素
SCARD key — 返回集合大小
SISMEMBER key member — 判斷某個值是否在集合中
SINTER key1 key2 ... keyN — 獲取多個集合的交集元素
SMEMBERS key — 列出集合的全部元素
還有Multiple DB的命令,能夠更換db,數據能夠隔離開,默認是存放在DB 0。
引用Nuget包ServiceStack.Redis
using ServiceStack.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace RedisConsoleDemo { class Program { static void Main(string[] args) { //在Redis中存儲經常使用的5種數據類型:String,Hash,List,SetSorted set var client = new RedisClient("127.0.0.1", 6379); AddString(client); //AddHash(client); //AddList(client); //AddSet(client); //AddSetSorted(client); Console.ReadLine(); } private static void AddString(RedisClient client) { var timeOut = new TimeSpan(0, 0, 0, 3); client.Add("Test", "Learninghard", timeOut); while (true) { if (client.ContainsKey("Test")) { Console.WriteLine("String Key: Test -Value: {0}, 當前時間: {1}", client.Get<string>("Test"), DateTime.Now); Thread.Sleep(1000); } else { Console.WriteLine("Value 已通過期了,當前時間:{0}", DateTime.Now); break; } } var person = new Person() { Name = "Learninghard", Age = 26 }; client.Add("lh", person); var cachePerson = client.Get<Person>("lh"); Console.WriteLine("Person's Name is : {0}, Age: {1}", cachePerson.Name, cachePerson.Age); } private static void AddHash(RedisClient client) { if (client == null) throw new ArgumentNullException("client"); client.SetEntryInHash("HashId", "Name", "Learninghard"); client.SetEntryInHash("HashId", "Age", "26"); client.SetEntryInHash("HashId", "Sex", "男"); var hashKeys = client.GetHashKeys("HashId"); foreach (var key in hashKeys) { Console.WriteLine("HashId--Key:{0}", key); } var haskValues = client.GetHashValues("HashId"); foreach (var value in haskValues) { Console.WriteLine("HashId--Value:{0}", value); } var allKeys = client.GetAllKeys(); //獲取全部的key。 foreach (var key in allKeys) { Console.WriteLine("AllKey--Key:{0}", key); } } private static void AddList(RedisClient client) { if (client == null) throw new ArgumentNullException("client"); client.EnqueueItemOnList("QueueListId", "1.Learnghard"); //入隊 client.EnqueueItemOnList("QueueListId", "2.張三"); client.EnqueueItemOnList("QueueListId", "3.李四"); client.EnqueueItemOnList("QueueListId", "4.王五"); var queueCount = client.GetListCount("QueueListId"); for (var i = 0; i < queueCount; i++) { Console.WriteLine("QueueListId出隊值:{0}", client.DequeueItemFromList("QueueListId")); //出隊(隊列先進先出) } client.PushItemToList("StackListId", "1.Learninghard"); //入棧 client.PushItemToList("StackListId", "2.張三"); client.PushItemToList("StackListId", "3.李四"); client.PushItemToList("StackListId", "4.王五"); var stackCount = client.GetListCount("StackListId"); for (var i = 0; i < stackCount; i++) { Console.WriteLine("StackListId出棧值:{0}", client.PopItemFromList("StackListId")); //出棧(棧先進後出) } } //它是string類型的無序集合。set是經過hash table實現的,添加,刪除和查找,對集合咱們能夠取並集,交集,差集 private static void AddSet(RedisClient client) { if (client == null) throw new ArgumentNullException("client"); client.AddItemToSet("Set1001", "A"); client.AddItemToSet("Set1001", "B"); client.AddItemToSet("Set1001", "C"); client.AddItemToSet("Set1001", "D"); var hastset1 = client.GetAllItemsFromSet("Set1001"); foreach (var item in hastset1) { Console.WriteLine("Set無序集合Value:{0}", item); //出來的結果是無須的 } client.AddItemToSet("Set1002", "K"); client.AddItemToSet("Set1002", "C"); client.AddItemToSet("Set1002", "A"); client.AddItemToSet("Set1002", "J"); var hastset2 = client.GetAllItemsFromSet("Set1002"); foreach (var item in hastset2) { Console.WriteLine("Set無序集合ValueB:{0}", item); //出來的結果是無須的 } var hashUnion = client.GetUnionFromSets(new string[] { "Set1001", "Set1002" }); foreach (var item in hashUnion) { Console.WriteLine("求Set1001和Set1002的並集:{0}", item); //並集 } var hashG = client.GetIntersectFromSets(new string[] { "Set1001", "Set1002" }); foreach (var item in hashG) { Console.WriteLine("求Set1001和Set1002的交集:{0}", item); //交集 } var hashD = client.GetDifferencesFromSet("Set1001", new string[] { "Set1002" }); //[返回存在於第一個集合,可是不存在於其餘集合的數據。差集] foreach (var item in hashD) { Console.WriteLine("求Set1001和Set1002的差集:{0}", item); //差集 } } /* sorted set 是set的一個升級版本,它在set的基礎上增長了一個順序的屬性,這一屬性在添加修改.元素的時候能夠指定, * 每次指定後,zset(表示有序集合)會自動從新按新的值調整順序。能夠理解爲有列的表,一列存 value,一列存順序。操做中key理解爲zset的名字. */ private static void AddSetSorted(RedisClient client) { if (client == null) throw new ArgumentNullException("client"); client.AddItemToSortedSet("SetSorted1001", "A"); client.AddItemToSortedSet("SetSorted1001", "B"); client.AddItemToSortedSet("SetSorted1001", "C"); var listSetSorted = client.GetAllItemsFromSortedSet("SetSorted1001"); foreach (var item in listSetSorted) { Console.WriteLine("SetSorted有序集合{0}", item); } client.AddItemToSortedSet("SetSorted1002", "A", 400); client.AddItemToSortedSet("SetSorted1002", "D", 200); client.AddItemToSortedSet("SetSorted1002", "B", 300); // 升序獲取第一個值:"D" var list = client.GetRangeFromSortedSet("SetSorted1002", 0, 0); foreach (var item in list) { Console.WriteLine(item); } //降序獲取第一個值:"A" list = client.GetRangeFromSortedSetDesc("SetSorted1002", 0, 0); foreach (var item in list) { Console.WriteLine(item); } } } class Person { public string Name { get; set; } public int Age { get; set; } } }