当前位置:网站首页>.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"));
边栏推荐
- About the declaration and definition of template functions [easy to understand]
- 微信公众号发布提醒(微信公众号模板消息接口)
- 卸载魅族应用商店
- Cause: could't make a guess for solution
- 图片随手机水平移动-陀螺仪。360度设置条件
- PTA class a 1002
- Structure of [Halcon vision] operator
- 2022pta平时训练题(1~10题字符串处理问题)
- MLX90640 红外热成像仪测温传感器模块开发笔记(六)红外图像伪彩色编码
- Agenda express | list of sub forum agenda on July 27
猜你喜欢
随机推荐
[Halcon vision] image filtering
[Halcon vision] morphological corrosion
videojs转canvas暂停、播放、切换视频
Our Web3 entrepreneurship project is yellow
并行、并发及对于高并发优化的几个方向
The reason why go language is particularly slow to develop run and build commands
Navicat15 MySQL (centos7) connected to local virtual machine
Inheritance method of simplified constructor (I) - combined inheritance
移动端双指缩放事件(原生),e.originalEvent.touches
关于模板函数声明与定义的问题[通俗易懂]
Tradingview tutorial
Dynamically determine file types through links
Kaptcha image verification code integration
软件打不开了
QRcode二维码(C语言)遇到的问题
Inheritance method of simplified constructor (II) - class inheritance in ES6
【C#语言】LINQ概述
Summary of common skills in H5 development of mobile terminal
结构体操作报错:Segmentation fault (core dumped)
【Halcon视觉】极坐标变换