当前位置:网站首页>.NET操作Redis String字符串
.NET操作Redis String字符串
2022-07-26 10:29:00 【矿工学编程】
一、String字符串概述
string类型在redis中是最常见的类型,其数据形式为就是 key value ,value存储最大数据量为512M,可以存放json数据,图像数据等等。
二、使用场景
1.session 利用redis做session共享内存。
2.自增和自减法 -- 做一些网站的请求数量,或者论坛的点赞数,评论数。 可以用redis来实现,完成之后做数据刷盘,把这些统计数据放到我们持久化数据库中。
三、.NET 操作
本人采用的是ServiceStack.Redis c#客户端,功能十分强大,但是收费。每小时请求达到6000时,会进行限制。如果想用其他的Redis C#客户端库,可以在Redis官网中找到别的客户端库,其中BeetleX.Redis和NewLife.Redis分别是水泥佬团队和石头佬团队退开发的,大家可以试一试。
1、设置key的value,基础操作
client.Set<string>("onekey", "第一个字符串操作");//写入String
var val = client.Get<string>("onekey");//根据key获取value
Console.WriteLine(val);2、设置多个key value键值对
//批量的写入redis key
client.SetAll(new Dictionary<string, string> { { "id", "1" }, { "name", "kgxk" } });
//批量读取内存中多个key的结果 如果我们获取的key不存在,程序会返回一个空的字符串
var getall = client.GetAll<string>(new string[] { "id", "name", "number" });
foreach (var item in getall)
{
Console.WriteLine(item);
}3、设置key的value并设置过期时间
client.Set<string>("name", "kgxk", TimeSpan.FromSeconds(1));
Task.Delay(1 * 1000).Wait();
//过期时间可以是具体时间
//client.Set<string>("name", "kgxk", DateTime.Now.AddSeconds(1));
//client.Set<string>("name", "kgxk", DateTime.Now.AddMonths(15));
Console.WriteLine(client.Get<string>("name"));
4、在原有key的value值之后追加value
client.AppendToValue("name", "K");
client.AppendToValue("name", "G");
client.AppendToValue("name", "XK");
Console.WriteLine(client.Get<string>("name"));5、获取旧值赋上新值
client.Set("name", "kgxk");
//获取当前key的之前的值,然后把新的结果替换进入
var value = client.GetAndSetValue("name", "矿工小坑");
Console.WriteLine("原先的值" + value);
Console.WriteLine("新值" + client.GetValue("name"));6、自增,返回自增后的值
//给key为sid的键自增1 ,返回了自增之后的结果
Console.WriteLine(client.Incr("sid"));
Console.WriteLine(client.Incr("sid"));
Console.WriteLine(client.Incr("sid"));
Console.WriteLine("默认自增结束");
Console.WriteLine(client.GetValue("sid"));
//每次通过传递的count累计,count就是累加的值
client.IncrBy("sid", 2);
Console.WriteLine(client.Get<string>("sid"));
client.IncrBy("sid", 100);
Console.WriteLine("最后的结果***" + client.GetValue("sid"));7、自减,返回自减后的值
Console.WriteLine(client.Decr("sid"));
Console.WriteLine(client.Decr("sid"));
Console.WriteLine(client.Decr("sid"));
Console.WriteLine("最后的结果" + client.GetValue("sid"));
//通过传入的count去做减肥 之前的结果-count
client.DecrBy("sid", 2);
Console.WriteLine("最终的结果" + client.GetValue("sid"));8、add 和set
- add 只能做新增,如果已经有了key则返回失败
- set 如果有key就替换,如果没有就写入
- 当使用add 方法去操作redis的时候,如果key存在的话,则不会再次进行操作 返回false 如果操作成功返回true
Console.WriteLine(client.Add("name", "kgxk"));
//用add的时候帮你去判断如果有则不进行操作,如果没有则写,它只能写新值,不能修改
Console.WriteLine(client.Add("name", "你很好kgxk"));
Console.WriteLine(client.Get<string>("name")); //使用set去操作 redis的时候,如果key不存在则写入当前值,并且返回true,通过存在,则对之前的值进行了一个替换 返回操作的结果
Console.WriteLine(client.Set("name", "kgxk"));
Console.WriteLine(client.Set("name", "你很好kgxk"));
Console.WriteLine(client.Get<string>("name"));
边栏推荐
猜你喜欢

Unit test, what is unit test and why is it so difficult to write a single test

Okaleido ecological core equity Oka, all in fusion mining mode

Data communication foundation STP principle
![[Halcon vision] image filtering](/img/4b/e73a8d589b49276d96621f0ef02449.png)
[Halcon vision] image filtering

Tradingview 使用教程
![[Halcon vision] morphological expansion](/img/ce/abaca036fce5b67dfe6ac361aecfea.png)
[Halcon vision] morphological expansion

Learning about tensor (III)

The difference between equals and = =

【Halcon视觉】图像滤波

Data communication foundation - layer 2 switching principle
随机推荐
【Halcon视觉】图像的傅里叶变换
一些你不知道的 web API
Perfect / buffer motion framework in sentence parsing JS (for beginners)
RecyclerView最后一条显示不全或显示部分的问题解决
Data communication foundation - layer 2 switching principle
On the compilation of student management system of C language course (simple version)
上传图片获取宽高
函数模板与同名的非模板函数不可以重载(重载的定义)
Redis realizes distributed lock and gets a watchdog
[Qualcomm][Network] qti服务分析
About the declaration and definition of template functions [easy to understand]
Application of crosstab in SQL Server
数据分析入门 | kaggle泰坦尼克任务
L2-005 集合相似度(vector、set求并交集)
Comparison of packet capturing tools fiddler and Wireshark
Some descriptions of DS V2 push down in spark
Review of database -- 3. SQL language
INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
MLX90640 红外热成像仪测温传感器模块开发笔记(六)
并行、并发及对于高并发优化的几个方向