最近須要使用redis,看了一些文檔,也在博客園裏面看了不少文章,這裏就記錄下Redis經常使用類型的操做。redis
string是redis基本類型,通常經過Get,Set 命令進行操做,這裏我用ServiceStack.Redis 進行操做:dom
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就是咱們提到的鏈表spa
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也是數據容器,和list不同,set裏面的元素不能重複code
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和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通常用來存儲咱們的對象模型排序
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的五種類型,這裏只是簡單的記錄其基本操做rem