.Net Core下 Redis的String Hash List Set和Sorted Set的例子

/ [随笔分类].Net Core跨平台 / 没有评论 / 93 浏览

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 &lt; 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 &lt; 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&lt;string&gt; hashSet = client.GetAllItemsFromSet("a3");
        foreach (var VARIABLE in hashSet)
        {
            Console.WriteLine(VARIABLE);
        }

        //求并集
        client.AddItemToSet("a4", "hhhh");
        client.AddItemToSet("a4", "h777");
        HashSet&lt;string&gt; hashSetUnion = client.GetUnionFromSets(new string[] {"a3", "a4"});
        Console.WriteLine("并集");
        foreach (var VARIABLE in hashSetUnion)
        {
            Console.WriteLine(VARIABLE);
        }

        //求交集
        HashSet&lt;string&gt; hashsetInter = client.GetIntersectFromSets(new string[] { "a3","a4" });
        Console.WriteLine("交集");
        foreach (var VARIABLE in hashsetInter)
        {
            Console.WriteLine(VARIABLE);
        }
        //求差集
        HashSet&lt;string&gt; 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


        </div>