.Net Core下 Redis的String Hash List Set和Sorted Set的例子
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的例子的更多相关文章
- .net core下Redis帮助类
0.引入.net core环境下Redis的NuGet包,StackExchange.Redis,现目前最新的2.0.519. 帮助类Code: using System; using Syste ...
- Redis学习-string数据类型
Redis 是一个开源的使用 ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志 型.Key-Value 数据库. redis提供五种数据类型string,hash,list,set及sor ...
- Redis应用场景 及其数据对象 string hash list set sortedset
原文地址:http://www.cnblogs.com/shanyou/archive/2012/09/04/2670972.html Redis开创了一种新的数据存储思路,使用Redis,我们不用在 ...
- redis数据类型[string 、list 、 set 、sorted set 、hash]
1. Keys redis本质上一个key-value db,所以我们首先来看看他的key. 首先key也是字符串类型,但是key中不能包括边界字符:由于key不是binary safe的字符串, ...
- 第一节: Redis之String类型和Hash类型的介绍和案例应用
一. String类型基础 1.类型介绍 典型的Key-Value集合,如果要存实体,需要序列化成字符串,获取的时候需要反序列化一下. 2. 指令Api说明 3.常用Api说明 (1).StringS ...
- Redis的String、Hash类型命令
String是最简单的类型,一个Key对应一个Value,string类型是二进制安全的.Redis的string可以包含任何数据,比如jpg图片或者序列化的对象.最大上限是1G字节. Hash ...
- Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty
原文:Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty public class NullToEmptyStringResolver : De ...
- .net core 下使用StackExchange的Redis库访问超时解决
原文:.net core 下使用StackExchange的Redis库访问超时解决 目录 问题:并发稍微多的情况下Redis偶尔返回超时 给出了参考网址? 结论 小备注 引用链接 问题:并发稍微多的 ...
- Python(Redis 中 String/List/Hash 类型数据操作)
1.下载 redis 模块 pip install redis 2.redis 数据库两种连接方式 简单连接 decode_responses=True,写入和读取的键值对中的 value 为 str ...
随机推荐
- 使用WinInet实现HTTP站点访问
废话不多说了,直接上代码 HTTP的GET方式代码 void sendGetRequest(LPCTSTR lpszURL) { LPCTSTR lpszAgent = _T("Winine ...
- centos7使用docker部署gitlab-ce-zh应用
1.国内拉取镜像比较慢,所以这里采用DaoCloud源. # curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http ...
- [翻译]编写高性能 .NET 代码 第二章:垃圾回收 基本操作
返回目录 基本操作 垃圾回收的算法细节还在不断完善中,性能还会有进一步的提升.下文介绍的内容在不同的.NET版本里会略有不同,但大方向是不会有变动的. 在.net进程里会管理2个类型的内存堆:托管和非 ...
- Spring中的@scope注解
默认是单例模式,即scope="singleton".另外scope还有prototype.request.session.global session作用域.scope=&quo ...
- No new migrations found. Your system is up-to-date.处理
显然是migrations表中存储的相关操作记录了,删除就好了!!!
- Docker镜像的构成__docker commit
镜像是容器的基础,每次执行docker run的时候都会制定哪个镜像作为容器运行的基础.在之前的例子中,我们所使用的都来自于Docker Hub的镜像.直接使用这些镜像是可以满足一定的需求,而当这些镜 ...
- PAT1119. Pre- and Post-order Traversals
思路:中序遍历–根结点,左子树,右子树:后序遍历–左子树,右子树,根结点. 那么在找到根结点之后就可以开始划分左右子树了.左子树的先序第一个节点是根,左子树的后序最后一个节点是根. 例如 1 2 3 ...
- hihoCoder 403 Forbidden 字典树
题意:给定个规则,个ip,问这些ip是否能和某个规则匹配,如果有多个规则,则匹配第一个.如果没能匹配成功,则认为是"allow",否则根据规则决定是"allow" ...
- [Cake] 1. CI中的Cake
在上一篇C#Make自动化构建-简介中,简单的介绍了下Cake的脚本如何编写以及通过Powershell在本地运行Cake脚本.本篇在此基础上,介绍下如何在CI环境中使用Cake. 1. Cake简介 ...
- RestTemplate 支持服务器内302重定向
Stack Overflow 里找到的代码,可以正常返回服务器302重定向后的响应 final RestTemplate restTemplate = new RestTemplate(); fina ...