.Net Core下 Redis的String Hash List Set和Sorted Set的例子
阿新 • • 發佈:2019-02-15
1.新建一個.Net Core控制檯應用程式,用Nuget匯入驅動
開啟程式包管理控制檯,
執行以下程式碼。
PM> Install-Package ServiceStack.Redis
即可新增Redis的引用。
2.StringDemo
String型別是最常用的資料型別,在Redis中以KKey/Value儲存。
using System; using System.Collections.Generic; using System.Text; using ServiceStack.Redis; using ServiceStack.Text; namespace RedisDotNetDemo { class StringDemo { public static void Start() { var redisMangement = new RedisManagerPool("127.0.0.1:6379"); var client = redisMangement.GetClient(); client.Set<int>("pwd", 111); //普通字串 int pwd = client.Get<int>("pwd"); Console.WriteLine(pwd); var todoTools = client.As<Todo>(); Todo todo=new Todo(){Content = "123",Id =todoTools.GetNextSequence(),Order = 1}; client.Set<Todo>("todo", todo); //object Todo getTodo = client.Get<Todo>("todo"); Console.WriteLine(getTodo.Content); List<Todo> list=new List<Todo>(){new Todo(){Content = "123"},new Todo(){Content = "234"}}; //List<Object> client.Set("list", list); List<Todo> getList = client.Get<List<Todo>>("list"); foreach (var VARIABLE in getList) { Console.WriteLine(VARIABLE.Content); } Console.ReadLine(); } } class Todo { public long Id { get; set; } public string Content { get; set; } public int Order { get; set; } public bool Done { get; set; } } }
3.HashDemo
如何,Hash在Redis採用 (HashId,Key,Value)進行儲存
一個HashId 可以包含多個key,一個key對應著一個value
using System; using System.Collections.Generic; using System.Text; using ServiceStack.Redis; namespace RedisDotNetDemo { class HashDemo { public static void Start() { var redisMangement = new RedisManagerPool("127.0.0.1:6379"); var client = redisMangement.GetClient(); client.SetEntryInHash("test", "123", "aaaaa"); //儲存一次資料 test是hashid,123是key,aaaa是value List<string> listKeys = client.GetHashKeys("test"); //獲取test雜湊下的所有key Console.WriteLine("keys in test"); foreach (var VARIABLE in listKeys) { Console.WriteLine(VARIABLE); } List<string> listValue = client.GetHashValues("test"); //獲取test雜湊下的所有值 Console.WriteLine("test 裡的所有值"); foreach (var VARIABLE in listValue) { Console.WriteLine(VARIABLE); } string value = client.GetValueFromHash("test", listKeys[0]); //獲取test雜湊下,第一個Key對應的值 Console.WriteLine("test 下的key"+listKeys[0]+"對應的值"+value); } } }
4.ListDemo
list是一個連結串列結構,key可以理解為連結串列的名字,然後往這個名字所對應的連結串列里加值。,list可以以隊/棧的形式進行工作。
using System; using System.Collections.Generic; using System.Text; using ServiceStack.Redis; namespace RedisDotNetDemo { class ListDemo { public static void Start() { var redisMangement = new RedisManagerPool("127.0.0.1:6379"); var client = redisMangement.GetClient(); //佇列的使用 //先進先出 client.EnqueueItemOnList("name","zhangsan"); //入列 client.EnqueueItemOnList("name","lisi"); //入列 long count = client.GetListCount("name"); for (int i = 0; i < count; i++) { Console.WriteLine(client.DequeueItemFromList("name")); //出列 } //棧的使用 //先進後出 client.PushItemToList("name2","wangwu"); //推入 client.PushItemToList("name2","maliu"); //推入 long count2 = client.GetListCount("name2"); for (int i = 0; i < count2; i++) { Console.WriteLine(client.PopItemFromList("name2")); //彈出 } } } }
5 SetDemo
它是string型別的無序集合。set是通過hash table實現的,新增,刪除和查詢,對集合我們可以取並集,交集,差集.
using System;
using System.Collections.Generic;
using System.Text;
using ServiceStack.Redis;
namespace RedisDotNetDemo
{
class SetDemo
{//它是string型別的無序集合。set是通過hash table實現的,新增,刪除和查詢,對集合我們可以取並集,交集,差集.
public static void Start()
{
var redisMangement = new RedisManagerPool("127.0.0.1:6379");
var client = redisMangement.GetClient();
//對Set型別進行操作
client.AddItemToSet("a3", "ddd");
client.AddItemToSet("a3", "ccc");
client.AddItemToSet("a3", "tttt");
client.AddItemToSet("a3", "sssh");
client.AddItemToSet("a3", "hhhh");
HashSet<string> hashSet = client.GetAllItemsFromSet("a3");
foreach (var VARIABLE in hashSet)
{
Console.WriteLine(VARIABLE);
}
//求並集
client.AddItemToSet("a4", "hhhh");
client.AddItemToSet("a4", "h777");
HashSet<string> hashSetUnion = client.GetUnionFromSets(new string[] {"a3", "a4"});
Console.WriteLine("並集");
foreach (var VARIABLE in hashSetUnion)
{
Console.WriteLine(VARIABLE);
}
//求交集
HashSet<string> hashsetInter = client.GetIntersectFromSets(new string[] { "a3","a4" });
Console.WriteLine("交集");
foreach (var VARIABLE in hashsetInter)
{
Console.WriteLine(VARIABLE);
}
//求差集
HashSet<string> hashsetDifference = client.GetDifferencesFromSet("a3", new string[] { "a4" });
Console.WriteLine("差集");
foreach (var VARIABLE in hashsetDifference)
{
Console.WriteLine(VARIABLE);
}
}
}
}
6.SortedSetDemo
SortedSet我只知道它相較於Set,它是有序的,而Set是無需的,而且使用者還可以調整SortedSet中value的位置,至於具體怎麼在.Net環境下調整,暫時沒有學會,就不在此班門弄斧,給出一個SortedDemo的存和取得例子。
using System;
using System.Collections.Generic;
using System.Text;
using ServiceStack.Redis;
namespace RedisDotNetDemo
{
//區別是set不是自動有序的,而sorted set可以通過使用者額外提供一個優先順序(score)的引數來為成員排序,並且是插入有序的,即自動排序。
//當你需要一個有序的並且不重複的集合列表,那麼可以選擇sorted set資料結構,
class SortedSetDemo
{
public static void Start()
{
var redisMangement = new RedisManagerPool("127.0.0.1:6379");
var client = redisMangement.GetClient();
client.AddItemToSortedSet("a5", "ffff");
client.AddItemToSortedSet("a5", "bbbb");
client.AddItemToSortedSet("a5", "gggg");
client.AddItemToSortedSet("a5", "cccc");
client.AddItemToSortedSet("a5", "waaa");
System.Collections.Generic.List<string> list = client.GetAllItemsFromSortedSet("a5");
foreach (string str in list)
{
Console.WriteLine(str);
}
}
}
}
以上是我對Redis中幾種資料型別得用法得總結,如有不對得地方,歡迎大家批評指正。
GitHub程式碼地址:https://github.com/liuzhenyulive/RedisDotNetDemo