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

简介: 1.新建一个.Net Core控制台应用程序,用Nuget导入驱动 打开程序包管理控制台, 执行以下代码。   PM> Install-Package ServiceStack.Redis 即可添加Redis的引用。

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

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

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


 

相关文章
|
6月前
|
存储 消息中间件 NoSQL
【Redis】常用数据结构之List篇:从常用命令到典型使用场景
本文将系统探讨 Redis List 的核心特性、完整命令体系、底层存储实现以及典型实践场景,为读者构建从理论到应用的完整认知框架,助力开发者在实际业务中高效运用这一数据结构解决问题。
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
215 18
你对Collection中Set、List、Map理解?
|
存储 缓存 安全
只会“有序无序”?面试官嫌弃的List、Set、Map回答!
小米,一位热衷于技术分享的程序员,通过与朋友小林的对话,详细解析了Java面试中常见的List、Set、Map三者之间的区别,不仅涵盖了它们的基本特性,还深入探讨了各自的实现原理及应用场景,帮助面试者更好地准备相关问题。
404 20
|
JSON Java 关系型数据库
Java更新数据库报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
在Java中,使用mybatis-plus更新实体类对象到mysql,其中一个字段对应数据库中json数据类型,更新时报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
1512 4
Java更新数据库报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
|
存储 消息中间件 NoSQL
Redis数据结构:List类型全面解析
Redis数据结构——List类型全面解析:存储多个有序的字符串,列表中每个字符串成为元素 Eelement,最多可以存储 2^32-1 个元素。可对列表两端插入(push)和弹出(pop)、获取指定范围的元素列表等,常见命令。 底层数据结构:3.2版本之前,底层采用**压缩链表ZipList**和**双向链表LinkedList**;3.2版本之后,底层数据结构为**快速链表QuickList** 列表是一种比较灵活的数据结构,可以充当栈、队列、阻塞队列,在实际开发中有很多应用场景。
|
NoSQL Redis
Redis 字符串(String)
10月更文挑战第16天
193 4
|
NoSQL 关系型数据库 MySQL
Redis 列表(List)
10月更文挑战第16天
203 2
|
消息中间件 存储 监控
redis 的List类型 实现 排行榜
【10月更文挑战第8天】
224 2
|
存储 分布式计算 NoSQL
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
大数据-40 Redis 类型集合 string list set sorted hash 指令列表 执行结果 附截图
191 3
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
160 5

热门文章

最新文章