In the previous blog it has been provided Dll Redis, the following use Redis practice the following five types in C #
Note: This is using Newtonsoft.Json;
need to add Newtonsoft.Json.dll, you can download it online
Redis data types most commonly used are the following five categories:
- 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 new RedisClient ( " 127.0.0.1 " , 6379 ); // set the IP and port services Redis static void Main(string[] args) { #region String Type Console.WriteLine ( " String Type ====================== start " ); // integer data redisClient.Set < int > ( " pwd " , 123456 ); int PSW = redisClient.Get < int > ( " pwd " ); Console.WriteLine(psw); // entity object the UserInfo = User new new the UserInfo () = {UserName " vinkong " , UserPwd = " 159 753 " }; 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 type ====================== end " ); #endregion #region the Hash type Console.WriteLine ( " the Hash ====================== Type Start " ); // 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 type ====================== end " ); #endregion #region List type Console.WriteLine ( " List Type ====================== start " ); // Use the queue 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")); } // stack using redisClient.PushItemToList ( " NAME2 " , " John Doe " ); redisClient.PushItemToList("name2", "张三"); int count2 = redisClient.GetListCount("name2"); for (int i = 0; i < count2; i++) { Console.WriteLine(redisClient.PopItemFromList("name2")); } Console.WriteLine ( " List type ====================== end " ); #endregion #region the Set type Console.WriteLine ( " the Set type ==== ================== start " ); // it is an unordered collection of type string. set is implemented by hash table, add, delete, and search, we can go to the set union, intersection, difference 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 ( " ============ union " ); // find and set 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 ( " ============ intersection " ); HashSet<string> hs3 = redisClient.GetIntersectFromSets(new string[]{"a3","a4"}); foreach (var item in hs3) { Console.WriteLine(item); } // returns exist in the first set, but not in other data sets. Difference set Console.WriteLine ( " ============ difference set " ); HashSet<string> hs4 = redisClient.GetDifferencesFromSet("a3", new string[] { "a4" }); foreach (var item in hs4) { Console.WriteLine(item); } Console.WriteLine ( " the Set type ====================== end " ); #endregion #region the Sorted the Set type Console.WriteLine ( " the Sorted type ====================== start the Set " ); // when you need an orderly and does not duplicate list set, can be selected sorted set of data structures // Default is not set number, you insertion order to show 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 not set number 0} { " , Item); } // set number redisClient.AddItemToSortedSet ( " SetSorted " , " vinkong1 " , . 4 ); redisClient.AddItemToSortedSet("SetSorted", "vinkong2",1); redisClient.AddItemToSortedSet("SetSorted", "vinkong3",3); List < String HS6 = redisClient.GetAllItemsFromSortedSet (> " SetSorted " ;) // by ascending sequence number to show the foreach ( var Item in HS6) { Console.WriteLine ( " SetSorted set number 0} { " , Item); } Console.WriteLine ( " the Sorted type ====================== the Set End " ); #endregion Console.ReadKey(); } } }