在之前的博客中已经提供了Redis的Dll,下面使用Redis在C#中实践下面5种类型
注:这个是using Newtonsoft.Json;
需要加Newtonsoft.Json.dll,自己可以网上下载一下
Redis最为常用的数据类型主要有以下五种:
- String
- Hash
- List
- Set
- Sorted set
using ServiceStack.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; namespace ConsoleApplication2 { class Program { static RedisClient redisClient = new RedisClient("127.0.0.1", 6379); // 设置Redis服务IP和端口 static void Main(string[] args) { #region String类型 Console.WriteLine("String类型======================开始"); //整形数据 redisClient.Set<int>("pwd",123456); int psw = redisClient.Get<int>("pwd"); Console.WriteLine(psw); //实体对象 UserInfo user = new UserInfo() { UserName = "vinkong", UserPwd = "159753" }; redisClient.Set<UserInfo>("userInfo", user); UserInfo a = redisClient.Get<UserInfo>("userInfo"); Console.WriteLine(a.UserName+"==="+a.UserPwd); //List对象 List<UserInfo> list = new List<UserInfo>() { new UserInfo {UserName ="vingkong1",UserPwd="123456"}, new UserInfo{UserName="vinkong2",UserPwd="123789"},new UserInfo{UserName ="vinkong3",UserPwd="159741"}}; redisClient.Set<List<UserInfo>>("list", list); List<UserInfo> b = redisClient.Get<List<UserInfo>>("list"); foreach (var item in b) { Console.WriteLine(item.UserName+"==="+item.UserPwd); } Console.WriteLine("String类型======================结束"); #endregion #region Hash类型 Console.WriteLine("Hash类型======================开始"); //hashtale for (int i = 0; i < 10; i++) { redisClient.SetEntryInHash("hashTable", "test" + i.ToString(), JsonConvert.SerializeObject(new { id = i + 1, name = "wolfy" + i.ToString() })); } //获取hashTabel List<string> woftlist = redisClient.GetHashValues("hashTable"); foreach (var item in woftlist) { Console.WriteLine(item); } Console.WriteLine("Hash类型======================结束"); #endregion #region List类型 Console.WriteLine("List类型======================开始"); //队列的使用 redisClient.EnqueueItemOnList("s", "a"); redisClient.EnqueueItemOnList("s","v"); int count = redisClient.GetListCount("s"); for (int i = 0; i < count; i++) { Console.WriteLine(redisClient.DequeueItemFromList("s")); } //栈的使用 redisClient.PushItemToList("name2", "李四"); redisClient.PushItemToList("name2", "张三"); int count2 = redisClient.GetListCount("name2"); for (int i = 0; i < count2; i++) { Console.WriteLine(redisClient.PopItemFromList("name2")); } Console.WriteLine("List类型======================结束"); #endregion #region Set类型 Console.WriteLine("Set类型======================开始"); //它是string 类型的无序集合。set是通过hash table实现的,添加、删除、和查找、对集合我们可以去并集、交集、差集 redisClient.AddItemToSet("a3", "ddd"); redisClient.AddItemToSet("a3", "ccc"); redisClient.AddItemToSet("a3", "tttt"); redisClient.AddItemToSet("a3", "sssh"); redisClient.AddItemToSet("a3", "hhhh"); HashSet<string> hs = redisClient.GetAllItemsFromSet("a3"); foreach (var item in hs) { Console.WriteLine(item); } Console.WriteLine("============并集"); //求并集 redisClient.AddItemToSet("a4","ddd"); redisClient.AddItemToSet("a4", "ccc"); redisClient.AddItemToSet("a4", "hhhh"); redisClient.AddItemToSet("a4", "4444"); HashSet<string> hs2 = redisClient.GetUnionFromSets(new string[] { "a3", "a4" }); foreach (var item in hs2) { Console.WriteLine(item); } Console.WriteLine("============交集"); HashSet<string> hs3 = redisClient.GetIntersectFromSets(new string[]{"a3","a4"}); foreach (var item in hs3) { Console.WriteLine(item); } //返回存在于第一个集合,但是不存在于其他集合的数据。差集 Console.WriteLine("============差集"); HashSet<string> hs4 = redisClient.GetDifferencesFromSet("a3", new string[] { "a4" }); foreach (var item in hs4) { Console.WriteLine(item); } Console.WriteLine("Set类型======================结束"); #endregion #region Sorted Set类型 Console.WriteLine("Sorted Set类型======================开始"); //当你需要一个有序的并且不重复的集合列表,那么可以选择sorted set数据结构 //默认不设置序号,则会按照插入顺序来展示 redisClient.AddItemToSortedSet("SetSorted","vinkong1"); redisClient.AddItemToSortedSet("SetSorted","vinkong2"); redisClient.AddItemToSortedSet("SetSorted","vinkong3"); List<string> hs5 = redisClient.GetAllItemsFromSortedSet("SetSorted"); foreach (var item in hs5) { Console.WriteLine("SetSorted 不设置序号{0}",item); } //设置序号 redisClient.AddItemToSortedSet("SetSorted", "vinkong1",4); redisClient.AddItemToSortedSet("SetSorted", "vinkong2",1); redisClient.AddItemToSortedSet("SetSorted", "vinkong3",3); List<string> hs6 = redisClient.GetAllItemsFromSortedSet("SetSorted"); //按序号由小到大展示 foreach (var item in hs6) { Console.WriteLine("SetSorted 设置序号{0}",item); } Console.WriteLine("Sorted Set类型======================结束"); #endregion Console.ReadKey(); } } }