当前位置:网站首页>.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"));
边栏推荐
- About the declaration and definition of template functions [easy to understand]
- The practice of OpenCV -- bank card number recognition
- 结构体操作报错:Segmentation fault (core dumped)
- 移动端H5开发常用技巧总结
- The problem of incomplete or partial display of the last recyclerview is solved
- 数据分析入门 | kaggle泰坦尼克任务(一)—>数据加载和初步观察
- 函数模板参数(函数参数在哪)
- [Halcon vision] array
- 【Halcon视觉】仿射变换
- Basics of data communication - basic knowledge of network
猜你喜欢

canvas上传图片base64-有裁剪功能-Jcrop.js

新建福厦铁路全线贯通 这将给福建沿海带来什么?

我们的Web3创业项目,黄了

【Halcon视觉】形态学膨胀

Learning about tensorflow (I)

Review of database -- 1. Overview

【Halcon视觉】仿射变换

INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决方式

On the compilation of student management system of C language course (simple version)

js下载文件,FileSaver.js导出txt、excel文件
随机推荐
[Halcon vision] image gray change
[gossip] error loading psychopg2 module: no module named psychopg2
2022/07/25 ------ arrangement of strings
PLC overview
数据分析入门 | kaggle泰坦尼克任务(一)—>数据加载和初步观察
Dynamically determine file types through links
C language course design Tetris (Part 1)
INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
简单化构造函数的继承方法(一)- 组合继承
新建福厦铁路全线贯通 这将给福建沿海带来什么?
The difference between equals and = =
C语言回调函数
畅听,网文流量竞争的下一站?
数据库的复习--3.SQL语言
关于函数模板描述错误的是(链接格式错误怎么解决)
Cause: could't make a guess for solution
上传图片获取宽高
[Halcon vision] programming logic
[Halcon vision] threshold segmentation
Review of database -- 3. SQL language