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

.Net Core下 Redis的String Hash List Set和Sorted Set的例子的更多相关文章

  1. .net core下Redis帮助类

      0.引入.net core环境下Redis的NuGet包,StackExchange.Redis,现目前最新的2.0.519. 帮助类Code: using System; using Syste ...

  2. Redis学习-string数据类型

    Redis 是一个开源的使用 ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志 型.Key-Value 数据库. redis提供五种数据类型string,hash,list,set及sor ...

  3. Redis应用场景 及其数据对象 string hash list set sortedset

    原文地址:http://www.cnblogs.com/shanyou/archive/2012/09/04/2670972.html Redis开创了一种新的数据存储思路,使用Redis,我们不用在 ...

  4. redis数据类型[string 、list 、 set 、sorted set 、hash]

    1. Keys  redis本质上一个key-value db,所以我们首先来看看他的key.  首先key也是字符串类型,但是key中不能包括边界字符:由于key不是binary safe的字符串, ...

  5. 第一节: Redis之String类型和Hash类型的介绍和案例应用

    一. String类型基础 1.类型介绍 典型的Key-Value集合,如果要存实体,需要序列化成字符串,获取的时候需要反序列化一下. 2. 指令Api说明 3.常用Api说明 (1).StringS ...

  6. Redis的String、Hash类型命令

    String是最简单的类型,一个Key对应一个Value,string类型是二进制安全的.Redis的string可以包含任何数据,比如jpg图片或者序列化的对象.最大上限是1G字节.    Hash ...

  7. Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty

    原文:Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty public class NullToEmptyStringResolver : De ...

  8. .net core 下使用StackExchange的Redis库访问超时解决

    原文:.net core 下使用StackExchange的Redis库访问超时解决 目录 问题:并发稍微多的情况下Redis偶尔返回超时 给出了参考网址? 结论 小备注 引用链接 问题:并发稍微多的 ...

  9. Python(Redis 中 String/List/Hash 类型数据操作)

    1.下载 redis 模块 pip install redis 2.redis 数据库两种连接方式 简单连接 decode_responses=True,写入和读取的键值对中的 value 为 str ...

随机推荐

  1. BZOJ 2734: [HNOI2012]集合选数 [DP 状压 转化]

    传送门 题意:对于任意一个正整数 n≤100000,如何求出{1, 2,..., n} 的满足若 x 在该子集中,则 2x 和 3x 不能在该子集中的子集的个数(只需输出对 1,000,000,001 ...

  2. SDN第一次作业

    作业链接 你会选择作 网络编程 方向的程序员吗?为什么? 光凭阅读此篇文章我还无法确定以后是否选择作 网络编程 方向的程序员.出于自身知识的匮乏,文章中提到的很多东西都没有概念,全篇一口气阅读下来,给 ...

  3. js 中的一些小技巧

    js 数字操作: 1.1 取整: 取整有很多方法如: parseInt(a,10); Math.floor(a); a>>0; ~~a; a|0; 前面2种是经常用到的,后面3中算是比较偏 ...

  4. 低版本IE内核浏览器兼容placeholder属性解决办法

    最简便的一个方法,通过js实现. <input type="text" name="username" id="username" v ...

  5. 多个onload事件写法

    window.onload=function(){ function(a); function(b); }

  6. codeforces 940D 比赛总结

    这次比赛总体还行,但是并没发挥到极致 A题 速度正常 题解 B题 这个题先是没注意时间复杂度,tle了,好不容易优化了没多测几组就交了,很开心的wa了,查了一边发现没特判k,改好后有草率地交了,又wa ...

  7. [记录]一则清理MySQL大表以释放磁盘空间的案例

    一则清理MySQL大表以释放磁盘空间的案例 一.基本情况: 1.dbtest库554G,先清理st_online_time_away_ds(37G)表的数据,保留半年的数据: 1)删除的数据:sele ...

  8. 如何让div水平居中呢?

    一百度div居中,多数都是一个答案,但是有时候这种方法并不是万能的...不废话,将我知道的方法都列举一下好了,随时更新. 1.设置width值,指定margin-left和margin-right为a ...

  9. mysql4 - 高级操作

    一.联结(使用 where(早) 和 join(晚) 都可以完成联结) 1.1 从 Teacher 表和 Profession 表中,查询出老师的名字和所属专业的名称. SELECT t.`l_nam ...

  10. iOS程序闪退的原因以及处理办法

    iOS程序闪退是一种比较常见的现象.闪退的情况很多,造成程序闪退的原因也很多. ================================启动时闪退======================= ...