当前位置:网站首页>.net operation redis string string
.net operation redis string string
2022-07-26 10:33:00 【Miners learn programming】
One 、String String Overview
string Type in the redis Is the most common type , Its data form is key value ,value The maximum amount of data stored is 512M, It can store json data , Image data and so on .
Two 、 Use scenarios
1.session utilize redis do session Shared memory .
2. Self increment and self subtraction -- Do some website requests , Or the number of likes on the Forum , comments . It can be used redis To achieve , After completion, do data disk brushing , Put these statistics into our persistent database .
3、 ... and 、.NET operation
I use ServiceStack.Redis c# client , Very powerful , But the charge . Every hour the request reaches 6000 when , Will be restricted . If you want to use other Redis C# Client library , Can be in Redis Official website Other client libraries found in , among BeetleX.Redis and NewLife.Redis They were developed by the cement guy team and the stone guy team , You can try .
1、 Set up key Of value, Basic operation
client.Set<string>("onekey", " The first string operation ");// write in String
var val = client.Get<string>("onekey");// according to key obtain value
Console.WriteLine(val);2、 Set up multiple key value Key value pair
// Batch write redis key
client.SetAll(new Dictionary<string, string> { { "id", "1" }, { "name", "kgxk" } });
// Batch read multiple in memory key Result If we get key non-existent , The program will return an empty string
var getall = client.GetAll<string>(new string[] { "id", "name", "number" });
foreach (var item in getall)
{
Console.WriteLine(item);
}3、 Set up key Of value And set expiration time
client.Set<string>("name", "kgxk", TimeSpan.FromSeconds(1));
Task.Delay(1 * 1000).Wait();
// The expiration time can be a specific time
//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、 In the original key Of value Value is appended value
client.AppendToValue("name", "K");
client.AppendToValue("name", "G");
client.AppendToValue("name", "XK");
Console.WriteLine(client.Get<string>("name"));5、 Get the old value and assign a new value
client.Set("name", "kgxk");
// Get current key The previous value of , Then replace the new result into
var value = client.GetAndSetValue("name", " Miner's pit ");
Console.WriteLine(" Original value " + value);
Console.WriteLine(" The new value " + client.GetValue("name"));6、 Self increasing , Returns the value after increment
// to key by sid The key of increases automatically 1 , Returns the result after autoincrement
Console.WriteLine(client.Incr("sid"));
Console.WriteLine(client.Incr("sid"));
Console.WriteLine(client.Incr("sid"));
Console.WriteLine(" Default auto increment end ");
Console.WriteLine(client.GetValue("sid"));
// Every time it passes count Cumulative ,count Is the cumulative value
client.IncrBy("sid", 2);
Console.WriteLine(client.Get<string>("sid"));
client.IncrBy("sid", 100);
Console.WriteLine(" The final result ***" + client.GetValue("sid"));7、 Self reduction , Returns the value after subtraction
Console.WriteLine(client.Decr("sid"));
Console.WriteLine(client.Decr("sid"));
Console.WriteLine(client.Decr("sid"));
Console.WriteLine(" The final result " + client.GetValue("sid"));
// Through the introduction of count To lose weight Previous results -count
client.DecrBy("sid", 2);
Console.WriteLine(" Final result " + client.GetValue("sid"));8、add and set
- add You can only add , If there are already key Then it returns failure
- set If there is key Just replace , If not, write
- When using add How to operate redis When , If key exist , Will not operate again return false If the operation is successful, return to true
Console.WriteLine(client.Add("name", "kgxk"));
// use add Help you to judge when there is any, and do not operate , If not, write , It can only write new values , Do not modify
Console.WriteLine(client.Add("name", " You're fine kgxk"));
Console.WriteLine(client.Get<string>("name")); // Use set To operate redis When , If key If it does not exist, write the current value , And back to true, By being , Then the previous value is replaced Returns the result of the operation
Console.WriteLine(client.Set("name", "kgxk"));
Console.WriteLine(client.Set("name", " You're fine kgxk"));
Console.WriteLine(client.Get<string>("name"));
边栏推荐
- Function templates and non template functions with the same name cannot be overloaded (definition of overloads)
- [leetcode每日一题2021/4/23]368. 最大整除子集
- 关于模板函数声明与定义的问题[通俗易懂]
- PTA class a 1002
- js,e.pageX、pageY模态框拖动
- Inheritance method of simplified constructor (I) - combined inheritance
- Application of crosstab in SQL Server
- [Halcon vision] threshold segmentation
- Draco developed by Google and Pixar supports USD format to accelerate 3D object transmission & lt; Forward & gt;
- Cause: could't make a guess for solution
猜你喜欢

Review of database -- 1. Overview

【Halcon视觉】形态学膨胀

【Halcon视觉】软件编程思路

干货likeshop外卖点餐系统开源啦100%开源无加密

【Halcon视觉】算子的结构

Comparison of packet capturing tools fiddler and Wireshark

2022/07/25 ------ arrangement of strings

Okaleido ecological core equity Oka, all in fusion mining mode

Li Kou daily question 917
The software cannot be opened
随机推荐
事务的传播性propagation
函数模板参数(函数参数在哪)
Okaleido生态核心权益OKA,尽在聚变Mining模式
Navicat15 MySQL (centos7) connected to local virtual machine
Review of database -- 1. Overview
MD5加密
【Halcon视觉】图像滤波
The CLOB field cannot be converted when querying Damon database
INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
3.1 leetcode daily question 6
同步方法中不使用asyncTask<T> 修饰和await获取异步返回值(同步方法中调用异步方法)
L2-005 集合相似度(vector、set求并交集)
datav漂亮数据屏制作体验
[leetcode每日一题2021/8/30]528. 按权重随机选择【中等】
What if MySQL can't get in
【Halcon视觉】图像灰度变化
异常的概念与处理
【dectectron2】跟着官方demo一起做
js下载文件,FileSaver.js导出txt、excel文件
上传图片获取宽高