前言
最近需要使用redis,看了一些文档,也在博客园里面看了很多文章,这里就记录下Redis常用类型的操作。
String
string是redis基本类型,一般通过Get,Set 命令进行操作,这里我用ServiceStack.Redis 进行操作:
using (redis)
{
//setValue 插入数据
redis.SetValue("hello", "world");
string s = redis.GetValue("hello"); redis.SetValue("Id", .ToString());
long id = redis.IncrementValue("Id");
id = redis.IncrementValueBy("Id", ); redis.ExpireEntryIn("hello", new TimeSpan(, , )); }
List
list就是我们提到的链表
using (redis)
{
string listId = "listId"; for (int i = ; i < ; i++)
{
redis.AddItemToList(listId, i.ToString());
} string s = redis.GetItemFromList(listId, );
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 = ; i < ; i++)
{
redis.AddItemToSet(setId1, i.ToString());
} for (int i = ; i < ; i++)
{
redis.AddItemToSet(setId2, (i + ).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, "");
s = redis.PopItemFromSet(setId1); }
SortSet
SortSet和set一样,但sortset顾名思义就是有排序的set
using (redis)
{
string sortSetId = "sortSetId"; for (int i = ; i < ; i++)
{
redis.AddItemToSortedSet(sortSetId, i.ToString(), i);
} bool contains = redis.SortedSetContainsItem(sortSetId, "");
long count = redis.GetSortedSetCount(sortSetId);
count = redis.GetSortedSetCount(sortSetId, , ); List<string> ids = redis.GetAllItemsFromSortedSet(sortSetId);
ids = redis.GetAllItemsFromSortedSetDesc(sortSetId);
ids = redis.GetRangeFromSortedSet(sortSetId, , );
ids = redis.GetRangeFromSortedSetByHighestScore(sortSetId, , );
ids = redis.GetRangeFromSortedSetByLowestScore(sortSetId, , ); double score = redis.GetItemScoreInSortedSet(sortSetId, ""); IDictionary<string, double> valueWithScores = redis.GetRangeWithScoresFromSortedSet(sortSetId, , );
valueWithScores = redis.GetRangeWithScoresFromSortedSetByHighestScore(sortSetId, , );
valueWithScores = redis.GetRangeWithScoresFromSortedSetByLowestScore(sortSetId, , ); bool isSucc = redis.RemoveItemFromSortedSet(sortSetId, ""); count = redis.RemoveRangeFromSortedSet(sortSetId, , );
count = redis.RemoveRangeFromSortedSetByScore(sortSetId, , ); 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 = ; i < ; 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, "", "", ""); isSucc = redis.RemoveEntryFromHash(hashId, "Hello");
long afterInc = redis.IncrementValueInHash(hashId, "", ); var newStringMap = new Dictionary<string, string> {
{"","e"}, {"","f"}, {"","g"}}; redis.SetRangeInHash(hashId, newStringMap);
}
总计
string,list, set, sortset, hash是redis的五种类型,这里只是简单的记录其基本操作