Redis_简单使用
可基于內(nèi)存也可持久化的Key-Value(字典,?Remote?Dictionary?Server,遠(yuǎn)程字典服務(wù)器)數(shù)據(jù)庫(kù)。
客戶端:http://redis.io/clients
命令:http://redis.io/commands?? ??http://redisdoc.com
.NET開(kāi)發(fā)程序配置
- ServiceStack.Common.dll
- ServiceStack.Interfaces.dll
- ServiceStack.Redis.dll
- ServiceStack.Text.dll
- 程序配置Redis服務(wù)IP和端口: static RedisClient Redis = new RedisClient("192.168.100.118", 6379);
- 雙擊運(yùn)行:redis-server.exe
Redis?Desktop?Manager?介紹
Redis?Desktop?Manager(RedisDesktopManager,RDM)是一個(gè)快速、簡(jiǎn)單、支持跨平臺(tái)的?Redis?桌面管理工具
下載地址:http://redisdesktop.com/download
C#操作5種基本數(shù)據(jù)類型
1.?字符串
A: ?存儲(chǔ)普通字符串,并設(shè)置過(guò)期時(shí)間
int?expireTime?=?5000;//?5S?
存儲(chǔ):client.Add<string>("StringKey","StringValue",?DateTime.Now.AddMilliseconds(expireTime));?
獲取:client.Get<string>("StringKey"),?DateTime.Now);
B: ?存儲(chǔ)類對(duì)象
Student?stud?=?new?Student()?{?id?=?"1000",?name?=?"張三"?};
存儲(chǔ):client.Add<Student>("StringEntity",?stud);
獲取:Student?Get_stud?=?client.Get<Student>("StringEntity");
2. 哈希
存儲(chǔ): 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: 隊(duì)列
入隊(duì):client.EnqueueItemOnList("QueueListId", "1");
出隊(duì):long q = client.GetListCount("QueueListId");
? ? ? ? ? client.DequeueItemFromList("QueueListId"));
B: 棧
入棧:client.PushItemToList("StackListId", "1");
出棧:client.PopItemFromList("StackListId")
4.?無(wú)序集合
存儲(chǔ):?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.?有序集合
存儲(chǔ):client.AddItemToSortedSet("SetSorted",?"A");
輸出:List<string>?listSetSorted?=?client.GetAllItemsFromSortedSet("SetSorted");
Redis應(yīng)用場(chǎng)景
A.搶XXX贈(zèng)券、抽獎(jiǎng)系統(tǒng)的獎(jiǎng)品庫(kù)存,使用的Redis中的鏈表
前一天晚上通過(guò)定時(shí)服務(wù)推送獎(jiǎng)品庫(kù)存,使用LPUSH命令將亂序的獎(jiǎng)品推入List中,抽獎(jiǎng)時(shí)則調(diào)用LPOP命令,將最左側(cè)獎(jiǎng)品彈出隊(duì)列,提示用戶中獎(jiǎng)。同時(shí),發(fā)送異步消息,讓消息去處理中獎(jiǎng)紀(jì)錄并插入關(guān)系型數(shù)據(jù)庫(kù)中。
好處:
出隊(duì)操作速度極快,可以滿足多人并發(fā)抽獎(jiǎng)的場(chǎng)景。?
使用了消息隊(duì)列,避免了數(shù)據(jù)庫(kù)并發(fā)操作。
?完全避免了關(guān)系性數(shù)據(jù)庫(kù)的查詢插入操作
Redis的查詢速度非常快,提升了用戶體驗(yàn)
1.?redis持久化RDB和AOF??http://my.oschina.net/davehe/blog/174662
2.?Redis作者談Redis應(yīng)用場(chǎng)景?http://blog.nosqlfan.com/html/2235.html
3.?Redis使用總結(jié)之與Memcached異同?http://www.cnblogs.com/ceecy/p/3279407.html
4.?Redis內(nèi)存使用優(yōu)化與存儲(chǔ)?http://www.infoq.com/cn/articles/tq-redis-memory-usage-optimization-storage
5.?Redis學(xué)習(xí)手冊(cè)(目錄)?http://www.cnblogs.com/stephen-liu74/archive/2012/04/16/2370212.html
?
轉(zhuǎn)載于:https://www.cnblogs.com/ingstyle/p/6655857.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的Redis_简单使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 前端模板Nunjucks简介
- 下一篇: [转]数据库分库分表