1. 程式人生 > >REDIS演示及使用場景

REDIS演示及使用場景

管理 ict deque top 普通 基本數據類型 stephen 技術 key-value

http://www.cnblogs.com/jys509/p/4721638.html

概述

Redis是一個開源的、使用C語言編寫的、支持網絡交互的、可基於內存也可持久化的Key-Value(字典, Remote Dictionary Server,遠程字典服務器)數據庫。

客戶端:http://redis.io/clients

命令:http://redis.io/commands http://redisdoc.com

.NET開發程序配置

  • ServiceStack.Common.dll
  • ServiceStack.Interfaces.dll
  • ServiceStack.Redis.dll
  • ServiceStack.Text.dll

程序配置Redis服務IP和端口

static RedisClient Redis = new RedisClient("192.168.100.118", 6379); 

雙擊運行:redis-server.exe

技術分享圖片

REDIS DESKTOP MANAGER 介紹

Redis Desktop Manager(RedisDesktopManager,RDM)是一個快速、簡單、支持跨平臺的 Redis 桌面管理工具,基於 Qt 5開發(一個跨平臺的C++圖形用戶界面應用程序框架),支持通過 SSH Tunnel 連接。

下載地址:http://redisdesktop.com/download

配置Redis服務地址:

技術分享圖片

查看可視化keys的值:

技術分享圖片

C#操作5種基本數據類型

1. 字符串

A: 存儲普通字符串,並設置過期時間
int expireTime = 5000;// 5S
存儲:client.Add<string>("StringKey","StringValue", DateTime.Now.AddMilliseconds(expireTime));
獲取:client.Get<string>("StringKey"), DateTime.Now);

B: 存儲類對象
Student stud = new Student() { id = "1000", name = "張三" };

存儲:client.Add<Student>("StringEntity", stud);
獲取:Student Get_stud = client.Get<Student>("StringEntity");

測試用例輸出結果:

技術分享圖片

2. 哈希

存儲: client.SetEntryInHash("HashID", "Name", "張三");

A: 遍歷HashID值為HashID的keys

獲取:List<string> HaskKey = client.GetHashKeys("HashID");

B:遍歷HashID值為HashID的values

獲取:List<string> HaskValue = client.GetHashValues("HashID");

C:遍歷所有keys

獲取:List<string> AllKey = client.GetAllKeys();

測試用例輸出結果:

技術分享圖片

3. 鏈表

A: 隊列
入隊:client.EnqueueItemOnList("QueueListId", "1");
出隊:long q = client.GetListCount("QueueListId");
client.DequeueItemFromList("QueueListId"));

B: 棧
入棧:client.PushItemToList("StackListId", "1");

出棧:client.PopItemFromList("StackListId")

測試用例輸出:

技術分享圖片

4. 無序集合

存儲: client.AddItemToSet("SetA", "1");

獲取:HashSet<string> setA = client.GetAllItemsFromSet("SetA");

A:並集

HashSet<string> hashUnion = client.GetUnionFromSets(new string[] { "SetA", "SetB" });

B:交集

HashSet<string> intersectSet = client.GetIntersectFromSets(new string[] { "SetA", "SetB" });

C:差集

HashSet<string> setOfDiffSetAToSetB = client.GetDifferencesFromSet("SetA", new string[] { "SetB" });

測試用例輸出:

技術分享圖片

5. 有序集合

存儲:client.AddItemToSortedSet("SetSorted", "A");

輸出:List<string> listSetSorted = client.GetAllItemsFromSortedSet("SetSorted");

測試用例輸出:

技術分享圖片

REDIS應用場景

只是介紹我本人在使用Redis時用到的場景,僅個人觀點。

A.搶XXX贈券、抽獎系統的獎品庫存,使用的Redis中的鏈表

前一天晚上通過定時服務推送獎品庫存,使用LPUSH命令將亂序的獎品推入List中,抽獎時則調用LPOP命令,將最左側獎品彈出隊列,提示用戶中獎。同時,發送異步消息,讓消息去處理中獎紀錄並插入關系型數據庫中。


好處:
出隊操作速度極快,可以滿足多人並發抽獎的場景。
使用了消息隊列,避免了數據庫並發操作。


B.某活動累計充值xxx元,獎勵xxx。使用Redis中的string/hash(哈希)結構

技術分享圖片

用戶每次充值,都發送一個充值MQ事件(使用RabbitMQ),另一個程序,消費充值MQ事件,將事件中的用戶ID、充值金額分別存到Redis(string/hash)裏面。
以後,就可以直接匯總用戶總充值金額給滿足條件的客戶贈送獎品。


好處:
完全避免了關系性數據庫的查詢插入操作
Redis的查詢速度非常快,提升了用戶體驗

擴展閱讀

1. redis持久化RDB和AOF http://my.oschina.net/davehe/blog/174662

2. Redis作者談Redis應用場景 http://blog.nosqlfan.com/html/2235.html

3. Redis使用總結之與Memcached異同 http://www.cnblogs.com/ceecy/p/3279407.html

4. Redis內存使用優化與存儲 http://www.infoq.com/cn/articles/tq-redis-memory-usage-optimization-storage

5. Redis學習手冊(目錄) http://www.cnblogs.com/stephen-liu74/archive/2012/04/16/2370212.html

演示代碼下載:http://download.csdn.net/detail/jys1216/8991915

REDIS演示及使用場景