Redis-3- C#
引入Nuget C# for Redis:
https://github.com/ServiceStack/ServiceStack.Redis
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | using ServiceStack.Redis; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RedisConsoleApp { class Program { static RedisClient client = new RedisClient( "127.0.0.1" , 6379); static void Main( string [] args) { StringTest(); HashTest(); QueueTest(); SetTest(); SortedSetTest(); } private static void StringTest() { Console.WriteLine( "*****************************************字符串类型*****************************************" ); client.Set< string >( "name" , "zouqj" ); string userName = client.Get< string >( "name" ); Console.WriteLine(userName); UserInfo userInfo = new UserInfo() { UserName = "张三" , UserPwd = "123" }; client.Set<UserInfo>( "UserInfo" , userInfo); UserInfo user = client.Get<UserInfo>( "UserInfo" ); Console.WriteLine(user.UserName); List<UserInfo> list = new List<UserInfo>() { new UserInfo() { UserName = "李四" , UserPwd = "12345" }, new UserInfo(){UserName= "王五" , UserPwd= "12345" } }; client.Set<List<UserInfo>>( "list" , list); List<UserInfo> userInfoList = client.Get<List<UserInfo>>( "list" ); foreach (UserInfo u in userInfoList) { Console.WriteLine(u.UserName); } Console.ReadLine(); } private static void HashTest() { Console.WriteLine( "***************************************Hash********************************************************" ); client.SetEntryInHash( "userInfoId" , "name" , "zhangsan" ); var lstKeys = client.GetHashKeys( "userInfoId" ); lstKeys.ForEach(k => Console.WriteLine(k)); var lstValues = client.GetHashValues( "userInfoId" ); lstValues.ForEach(v=>Console.WriteLine(v)); client.Remove( "userInfoId" ); Console.ReadLine(); } private static void QueueTest() { Console.WriteLine( "**************************************************队列 先进先出********************************************************" ); client.EnqueueItemOnList( "test" , "饶成龙" ); client.EnqueueItemOnList( "test" , "周文杰" ); long length = client.GetListCount( "test" ); for ( int i = 0; i < length; i++) { Console.WriteLine(client.DequeueItemFromList( "test" )); } Console.WriteLine( "***********************************************栈 先进后出************************************************************" ); client.PushItemToList( "name1" , "邹琼俊" ); client.PushItemToList( "name1" , "周文杰" ); long length1 = client.GetListCount( "name1" ); for ( int i = 0; i < length1; i++) { Console.WriteLine(client.PopItemFromList( "name1" )); } Console.ReadLine(); } private static void SetTest() { client.AddItemToSet( "HighSchool" , "卢沛" ); client.AddItemToSet( "HighSchool" , "卢沛1" ); client.AddItemToSet( "HighSchool" , "卢沛2" ); client.AddItemToSet( "HighSchool" , "卢沛3" ); client.AddItemToSet( "HighSchool" , "卢沛4" ); client.AddItemToSet( "HighSchool" , "卢沛5" ); client.AddItemToSet( "HighSchool" , "卢沛6" ); client.AddItemToSet( "HighSchool" , "卢沛7" ); client.AddItemToSet( "HighSchool" , "卢沛8" ); client.AddItemToSet( "HighSchool" , "卢沛9" ); client.AddItemToSet( "HighSchool" , "卢沛10" ); HashSet< string > hashset1 = client.GetAllItemsFromSet( "HighSchool" ); Console.WriteLine( "*****************************************************卢沛和以下人员是高中同学****************************" ); ConsoleHashSetInfo(hashset1); //求并集 client.AddItemToSet( "College" , "卢沛" ); client.AddItemToSet( "College" , "卢沛10" ); client.AddItemToSet( "College" , "卢沛11" ); client.AddItemToSet( "College" , "卢沛12" ); client.AddItemToSet( "College" , "卢沛13" ); client.AddItemToSet( "College" , "卢沛14" ); HashSet< string > hashset2 = client.GetUnionFromSets( new string [] { "HighSchool" , "College" }); Console.WriteLine( "*****************************************************卢沛和以下人员是高中/大学同学****************************" ); ConsoleHashSetInfo(hashset2); //求交集 HashSet< string > hashset3 = client.GetIntersectFromSets( new string [] { "HighSchool" , "College" }); Console.WriteLine( "*****************************************************卢沛和以下人员是高中和大学同学****************************" ); ConsoleHashSetInfo(hashset3); //求差集 HashSet< string > hashset4 = client.GetDifferencesFromSet( "HighSchool" , new string [] { "College" }); Console.WriteLine( "*****************************************************卢沛和以下人员只是高中同学****************************" ); ConsoleHashSetInfo(hashset4); Console.ReadLine(); } private static void ConsoleHashSetInfo(HashSet< string > hs) { foreach ( string str in hs) { if (str == "卢沛" ) { continue ; } Console.WriteLine(str); } } private static void SortedSetTest() { client.AddItemToSortedSet( "friends" , "熊平" , 1); client.AddItemToSortedSet( "friends" , "熊平3" , 3); client.AddItemToSortedSet( "friends" , "熊平5" , 5); client.AddItemToSortedSet( "friends" , "熊平2" , 3); client.AddItemToSortedSet( "friends" , "熊平" , 1); client.AddItemToSortedSet( "friends" , "熊平4" , 4); List< string > list = client.GetAllItemsFromSortedSet( "friends" ); foreach ( string str in list) { Console.WriteLine(str); } Console.ReadLine(); } class UserInfo { public string UserName { get ; set ; } public string UserPwd { get ; set ; } } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步