当前位置:网站首页>.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"));
边栏推荐
猜你喜欢
随机推荐
hx711 数据波动大的问题
datav漂亮数据屏制作体验
记给esp8266烧录刷固件
【Halcon视觉】编程逻辑
【Halcon视觉】仿射变换
Okaleido ecological core equity Oka, all in fusion mining mode
10 令 operator= 返回一个 reference to *this
C语言计算日期间隔天数
.NET 开源框架在工业生产中的应用
INSTALL_ FAILED_ SHARED_ USER_ Incompatible error resolution
SAP ABAP Netweaver 容器化的一些前沿性研究工作分享
[Halcon vision] morphological corrosion
.NET操作Redis Set无序集合
C语言回调函数
【Halcon视觉】图像滤波
父类对子类的引用(父类引用指向子类对象)
equals与==的区别
并行、并发及对于高并发优化的几个方向
【杂谈】Error loading psycopg2 module :No module named psycopg2
【C#语言】具名类型和匿名类型




![[Halcon vision] programming logic](/img/1a/b6daac946fbefd8337355dc8b7873e.png)

![[leetcode每日一题2021/8/31]1109. 航班预订统计【中等】差分数组](/img/9d/5ce5d4144a9edc3891147290e360d8.png)

