在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
马上要用redis来改造现有的o2o项目了,在linux下部署了个redis,顺便研究了下代码操作,分享下代码 using System; using System.Collections.Generic; using ServiceStack.Redis; namespace SysBuild { class Program { //linux服务器地址 static private string host = "182.180.50.168"; //static private string host = "127.0.0.1"; static private int port = 6379; static RedisClient redisClient = new RedisClient(host, port);//redis服务IP和端口 static void Main(string[] args) { //创建一个键a,指定值 redisClient.Set<string>("a", "1"); //获取键a对应的值 var a1 = redisClient.Get<string>("a"); //删除键a redisClient.Remove("a"); //创建一个键a,指定值,并指定10s有效 redisClient.Set<string>("b", "1", new TimeSpan(0, 0, 60)); //队列操作之入队 redisClient.EnqueueItemOnList("list", "haha"); redisClient.EnqueueItemOnList("list", "haha111111111"); redisClient.EnqueueItemOnList("list", "haha22"); redisClient.EnqueueItemOnList("list", "hahset33333333333333333"); //读取队列深度 var a = redisClient.GetListCount("list"); //队列操作之出队 for (int i = 0; i < a; i++) { Console.WriteLine(redisClient.DequeueItemFromList("list")); } redisClient.SetEntryInHash("Hash", "Name", "wujf"); redisClient.SetEntryInHash("Hash", "Age", "99"); redisClient.SetEntryInHash("Hash", "Sex", "男"); redisClient.SetEntryInHash("Hash", "Address", "上海市XX号XX室"); //集合类指定超时的方法 redisClient.ExpireEntryIn("Hash", new TimeSpan(0, 0, 100)); var dic = redisClient.GetAllEntriesFromHash("Hash"); Console.WriteLine("Key-----Value"); foreach (var keyVal in dic) { Console.WriteLine(string.Format("{0}-----{1}", keyVal.Key, keyVal.Value)); } List<string> haskKey = redisClient.GetHashKeys("Hash"); List<string> haskVal = redisClient.GetHashValues("Hash"); foreach (string key in haskKey) { Console.WriteLine("HashID--Key:{0}", key); } foreach (string val in haskVal) { Console.WriteLine("HashID--val:{0}", val); } //栈使用,先进后出 redisClient.PushItemToList("stack", "1"); redisClient.PushItemToList("stack", "2"); redisClient.PushItemToList("stack", "3"); redisClient.PushItemToList("stack", "4"); int count = redisClient.GetListCount("stack"); for (int i = 0; i < count; i++) { Console.WriteLine(redisClient.PopItemFromList("stack")); } //对Set类型进行操作 redisClient.AddItemToSet("set", "ddd"); redisClient.AddItemToSet("set", "ccc"); redisClient.AddItemToSet("set", "tttt"); redisClient.AddItemToSet("set", "sssh"); redisClient.AddItemToSet("set", "hhhh"); HashSet<string> hashset = redisClient.GetAllItemsFromSet("set"); foreach (string str in hashset) { Console.WriteLine(str); } //求并集 redisClient.AddItemToSet("set1", "aa"); redisClient.AddItemToSet("set1", "bb"); redisClient.AddItemToSet("set2", "bb"); redisClient.AddItemToSet("set2", "cc"); hashset = redisClient.GetUnionFromSets(new string[] { "set1", "set2" }); foreach (string str in hashset) { Console.WriteLine(str); } //求交集 hashset = redisClient.GetIntersectFromSets(new string[] { "set1", "set2" }); //求差集. hashset = redisClient.GetDifferencesFromSet("set1", new string[] { "set2" }); //Sorted Set类型排序 redisClient.AddItemToSortedSet("sortList", "1"); redisClient.AddItemToSortedSet("sortList", "9"); redisClient.AddItemToSortedSet("sortList", "3"); redisClient.AddItemToSortedSet("sortList", "8"); List<string> sortList = redisClient.GetAllItemsFromSortedSet("sortList"); foreach (string str in sortList) { Console.WriteLine(str); } Console.ReadKey(); } } } 另:运行过程推荐使用Redis客户端看看数据变化(ps:我用的redisclient) 推荐api说明 http://www.cnblogs.com/kissdodog/p/3572084.html |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论