当前位置:网站首页>.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"));
边栏推荐
- The CLOB field cannot be converted when querying Damon database
- About the declaration and definition of template functions [easy to understand]
- Nacos custom service change subscription
- Introduction to Phoenix (Level 1: Phoenix installation, level 2: Phoenix basic grammar)
- Google与Pixar开发Draco支持USD格式 加速3D对象传输&lt;转发&gt;
- 父类对子类的引用(父类引用指向子类对象)
- [socket] the three handshakes are completed in listen, and accept only takes out one connection from the queue that completes the connection
- Listening freely, the next stop of online text traffic competition?
- 简单化构造函数的继承方法(二)- ES6中的class继承
- Inheritance method of simplified constructor (II) - class inheritance in ES6
猜你喜欢
随机推荐
构造器、方法重载、对象数组和static
L2-005 集合相似度(vector、set求并交集)
移动端双指缩放事件(原生),e.originalEvent.touches
Closure of go (cumulative sum)
干货likeshop外卖点餐系统开源啦100%开源无加密
Okaleido生态核心权益OKA,尽在聚变Mining模式
What if MySQL can't get in
数据分析入门 | kaggle泰坦尼克任务
[leetcode每日一题2021/5/8]1723. 完成所有工作的最短时间
[Halcon vision] image filtering
Interview questions and answers for the second company (2)
.NET操作Redis Set无序集合
头歌 Phoenix 入门(第1关:Phoenix 安装、第2关:Phoenix 基础语法)
mysql 进不去了怎么办
【Halcon视觉】形态学膨胀
记给esp8266烧录刷固件
链式方法调用的事务问题剖析
Comparison of packet capturing tools fiddler and Wireshark
Review of database -- 1. Overview
2022pta usual training questions (1-10 string processing questions)








