Redis .Net 基本类型使用之南
前言
最近需要使用redis,看了一些文档,也在博客园里面看了很多文章,这里就记录下Redis常用类型的操作。
String
string是redis基本类型,一般通过Get,Set 命令进行操作,这里我用ServiceStack.Redis 进行操作:
using (redis) { //setValue 插入数据 redis.SetValue("hello", "world"); string s = redis.GetValue("hello"); redis.SetValue("Id", 1.ToString()); long id = redis.IncrementValue("Id"); id = redis.IncrementValueBy("Id", 100); redis.ExpireEntryIn("hello", new TimeSpan(0, 2, 0)); }
List
list就是我们提到的链表
using (redis) { string listId = "listId"; for (int i = 0; i < 5; i++) { redis.AddItemToList(listId, i.ToString()); } string s = redis.GetItemFromList(listId, 0); List<string> all = redis.GetAllItemsFromList(listId); long count = redis.GetListCount(listId); s = redis.PopItemFromList(listId); s = redis.RemoveStartFromList(listId); s = redis.RemoveEndFromList(listId); }
Set
set也是数据容器,和list不一样,set里面的元素不能重复
using (redis) { string setId1 = "setId1"; string setId2 = "setId2"; for (int i = 0; i < 5; i++) { redis.AddItemToSet(setId1, i.ToString()); } for (int i = 0; i < 5; i++) { redis.AddItemToSet(setId2, (i + 2).ToString()); } long count = redis.GetSetCount(setId1); string s = redis.GetRandomItemFromSet(setId1); HashSet<string> allIds = redis.GetAllItemsFromSet(setId1); HashSet<string> unionIds = redis.GetUnionFromSets(setId1, setId2); redis.StoreIntersectFromSets("intersectId", setId1, setId2); redis.StoreDifferencesFromSet("diffentsecId", setId1, setId2); allIds = redis.GetAllItemsFromSet("intersectId"); allIds = redis.GetAllItemsFromSet("diffentsecId"); redis.RemoveItemFromSet(setId1, "1"); s = redis.PopItemFromSet(setId1); }
SortSet
SortSet和set一样,但sortset顾名思义就是有排序的set
using (redis) { string sortSetId = "sortSetId"; for (int i = 0; i < 10; i++) { redis.AddItemToSortedSet(sortSetId, i.ToString(), i); } bool contains = redis.SortedSetContainsItem(sortSetId, "1"); long count = redis.GetSortedSetCount(sortSetId); count = redis.GetSortedSetCount(sortSetId, 0, 4); List<string> ids = redis.GetAllItemsFromSortedSet(sortSetId); ids = redis.GetAllItemsFromSortedSetDesc(sortSetId); ids = redis.GetRangeFromSortedSet(sortSetId, 0, 1); ids = redis.GetRangeFromSortedSetByHighestScore(sortSetId, 0, 3); ids = redis.GetRangeFromSortedSetByLowestScore(sortSetId, 0, 3); double score = redis.GetItemScoreInSortedSet(sortSetId, "1"); IDictionary<string, double> valueWithScores = redis.GetRangeWithScoresFromSortedSet(sortSetId, 0, 10); valueWithScores = redis.GetRangeWithScoresFromSortedSetByHighestScore(sortSetId, 0, 10); valueWithScores = redis.GetRangeWithScoresFromSortedSetByLowestScore(sortSetId, 0, 10); bool isSucc = redis.RemoveItemFromSortedSet(sortSetId, "1"); count = redis.RemoveRangeFromSortedSet(sortSetId, 0, 2); count = redis.RemoveRangeFromSortedSetByScore(sortSetId, 3, 5); string s = redis.PopItemWithHighestScoreFromSortedSet(sortSetId); s = redis.PopItemWithLowestScoreFromSortedSet(sortSetId); }
Hash
hash一般用来存储我们的对象模型
using (redis) { string hashId = "hashId"; bool isSucc = redis.SetEntryInHash(hashId, "Hello", "world"); for (int i = 0; i < 10; i++) { redis.SetEntryInHash(hashId, i.ToString(), i.ToString()); } long count = redis.GetHashCount(hashId); List<string> ids = redis.GetHashKeys(hashId); ids = redis.GetHashValues(hashId); isSucc = redis.HashContainsEntry(hashId, "Hello"); string s = redis.GetValueFromHash(hashId, "Hello"); ids = redis.GetValuesFromHash(hashId, "1", "2", "3"); isSucc = redis.RemoveEntryFromHash(hashId, "Hello"); long afterInc = redis.IncrementValueInHash(hashId, "1", 2); var newStringMap = new Dictionary<string, string> { {"1","e"}, {"2","f"}, {"3","g"}}; redis.SetRangeInHash(hashId, newStringMap); }
总计
string,list, set, sortset, hash是redis的五种类型,这里只是简单的记录其基本操作