当前位置:网站首页>Redis learning notes - data type: string (string)
Redis learning notes - data type: string (string)
2022-06-23 09:10:00 【Love Guoba】
List of articles
- Set the value
- set command
- ex seconds: Sets a second - level expiration time for the key
- px milliseconds: Sets the millisecond expiration time for the key .
- nx: The key must not exist , Can be set successfully , Used to add .
- xx: And nx contrary , The bond has to exist , Can be set successfully , Used to update the .
- setex command ( Set expiration time )
- setnx command ( If key There is no operation , If not, create )
- Get value
- Batch setting value
- Get values in batch and return them in sequence
- Count
- Additional value
- Get string length
- Set and return the value ( If there is an original value, the original value will be returned )
- Sets the character of the specified position
- Get part string
- Check the internal code
- Typical usage scenarios ( Three examples )
The string is Redis The most basic data structure . First, the key is a string type , And several other types are built on the basis of string types . A value of type string can actually be a string ( Simple string 、 Complex string ( for example JSON、XML))、 Numbers ( Integers 、 Floating point numbers ), Even binary ( picture 、 Audio 、 video ), But the maximum value cannot exceed 512MB.
Set the value
set command
set key value [ex seconds] [px milliseconds] [nx|xx]
ex seconds: Sets a second - level expiration time for the key
127.0.0.1:6379> set hello world ex 50
OK
px milliseconds: Sets the millisecond expiration time for the key .
127.0.0.1:6379> set hello world px 50000
OK
nx: The key must not exist , Can be set successfully , Used to add .
127.0.0.1:6379> set hello world nx
OK
127.0.0.1:6379> set hello world nx
(nil)
xx: And nx contrary , The bond has to exist , Can be set successfully , Used to update the .
127.0.0.1:6379> del hello
(integer) 1
127.0.0.1:6379> set hello world xx
(nil)
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> set hello world1 xx
OK
setex command ( Set expiration time )
setex key seconds value
The following four examples are all settings key by hello Cache expiration time 50 second
127.0.0.1:6379> setex hello 50 world
OK
127.0.0.1:6379> set hello world ex 50
OK
127.0.0.1:6379> set hello world px 50000
OK
127.0.0.1:6379> expire java 50
(integer) 1
setnx command ( If key There is no operation , If not, create )
setnx key value
because Redis The single thread command processing mechanism , If multiple clients execute at the same time setnx key value, according to setnx Only one client can be set successfully ,setnx It can be used as an implementation of distributed lock ,Redis The use of setnx How to implement distributed lock :http://redis.io/topics/distlock.
Get value
get key
Batch setting value
mset key value [key value ...]
Batch settings a,b,c Three key
127.0.0.1:6379> mset a 1 b 2 c 3
OK
Get values in batch and return them in sequence
mget key [key ...]
Batch acquisition a,b,c Three key value
127.0.0.1:6379> mget a b c
1) "1"
2) "2"
3) "3"
Batch operations can reduce items and redis Network request for database , If you loop through each , Then each one should go through the network request , Compared with memory speed , The network is absolutely the bottleneck of performance , So the batch operation is entrusted to redis Internal processing , One request , One return , It helps to improve the efficiency of business processing ; But it should be noted that the number of commands sent by each batch operation is not unlimited , If the quantity is too large, it may cause Redis Congestion or network congestion .
Count
Self increasing
incr key
Self reduction
decr key
Self increment specified number
incr key increment
Self subtract the specified number
decrby key decrement
Self-incremented floating point number
incrbyfloat key increment
Many storage systems and programming languages are used internally CAS Mechanism to achieve the counting function , There will be a certain amount of CPU expenses , But in Redis There is no such problem in , because Redis It's a single thread architecture , Any order arrived Redis The server should execute in sequence .
Additional value
append key value
Illustrate with examples :
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> append hello is
(integer) 7
127.0.0.1:6379> get hello
"worldis"
Get string length
strlen key
Illustrate with examples :
127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> strlen hello
(integer) 5
Set and return the value ( If there is an original value, the original value will be returned )
getset key value
Illustrate with examples :
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> getset hello world1
"world"
127.0.0.1:6379> get hello
"world1"
Sets the character of the specified position
setrange key offeset value
Illustrate with examples :
127.0.0.1:6379> set java redis
OK
127.0.0.1:6379> setrange java 0 j
(integer) 5
127.0.0.1:6379> get java
"jedis"
Get part string
getrange key start end
Illustrate with examples :
127.0.0.1:6379> get java
"jedis"
127.0.0.1:6379> getrange java 1 3
"edi"
Check the internal code
127.0.0.1:6379> object encoding hello
"embstr"
The internal encoding of a string type is 3 Kind of :
- int:8 A long integer of bytes .
- embstr: Less than or equal to 39 A byte string .
- raw: Greater than 39 A byte string .
Redis Depending on the type and length of the current value, which internal encoding is used to implement .
Typical usage scenarios ( Three examples )
- Do things that storage tiers don't change very often , But a cache layer of data that is often accessed , Reduce database pressure
- Be a user session The memory of , Solve the problem of distributed service load balancing session Out of sync problem
- Count , For example, the number of page views , Video playback volume , If you call the interface to save the database every time, it will cause a large number of requests , Greatly affects performance , Can exist first redis, And then persistence
边栏推荐
- Structure binary tree from inorder and postorder traversal for leetcode topic analysis
- Combination sum II of leetcode topic analysis
- [QNX Hypervisor 2.2用户手册]6.1 使用QNX Hypervisor系统
- 670. Maximum Swap
- RGB与CMYK颜色模式
- Detailed explanation of srl16e in xilinxffpga
- [QNX Hypervisor 2.2用户手册]6.2 网络
- [advanced Android] kotlin notes
- 学习SCI论文绘制技巧(F)
- Batch generation of code128- C barcode
猜你喜欢
随机推荐
Best time to buy and sell stock
玩转NanoPi 2 裸机教程编程-01点亮User LED难点解析
Isomorphic strings for leetcode topic resolution
Fraction to recursing decimal
【NanoPi2试用体验】裸机第一步
自定义标签——jsp标签基础
How postman does interface testing 1: how to import swagger interface documents
In depth interpretation of poca smart contract platform gear: the road to parallel architecture public chain
Le rapport d'analyse de l'industrie chinoise des bases de données a été publié en juin. Le vent intelligent se lève, les colonnes se régénèrent
通用分页(1)
MySQL fault case | mysqldump: couldn't execute 'select column_ NAME
Redis学习笔记—redis-cli详解
栈(Stack)的链式实现详解----线性结构
2022.6.22-----leetcode. five hundred and thirteen
简易学生管理
An idea of using keep alive to cache data in vue3 form pages
Tencent cloud arm server evaluation practice
如何在 FlowUs、Notion 等笔记软件中使用「番茄工作法」?
[qnx hypervisor 2.2 user manual]6.1 using the QNX hypervisor system
Redis学习笔记—客户端通讯协议RESP







