当前位置:网站首页>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
边栏推荐
- H-index of leetcode topic analysis
- [qnx hypervisor 2.2 user manual]6.2 network
- 线性表(LinkList)的链式表示和实现----线性结构
- 嵌入式系统概述(学习笔记)
- 文件的打开新建与存储
- Detailed explanation of srl16e in xilinxffpga
- Isomorphic strings for leetcode topic resolution
- 简易学生管理
- 一元函数求极限三大方法---洛必达法则,泰勒公式
- Mqtt+flink to subscribe and publish real-time messages
猜你喜欢
Redis学习笔记—数据类型:哈希(hash)

297. Serialize and Deserialize Binary Tree

JSP入门总结

一元函数求极限三大方法---洛必达法则,泰勒公式

Custom tag - JSP tag Foundation

Which is better, semrush or ahrefs? Which is more suitable for GoogleSEO keyword analysis
![[event registration] sofastack × CSDN jointly held the open source series meetup, which was launched on June 24](/img/e1/97c92290a2a5e68f05cdbd5bf525e8.png)
[event registration] sofastack × CSDN jointly held the open source series meetup, which was launched on June 24

Typora设置图片上传服务

636. Exclusive Time of Functions

Simple student management
随机推荐
A method of realizing video call and interactive live broadcast in small programs
【学习资源】理解数学和热爱数学
The sliding window of the force button "step by step" (209. sub array with the smallest length, 904. fruit basket)
ARM处理器与51单片机程序编写的区别
MySQL fault case | error 1071 (42000): specified key was too long
[learning resources] understand and love mathematics
Simple student management
Redis学习笔记—数据类型:字符串(string)
In depth interpretation of poca smart contract platform gear: the road to parallel architecture public chain
[QNX Hypervisor 2.2用户手册]5.6.1 Guest关机时静默设备
636. Exclusive Time of Functions
Redis学习笔记—发布订阅
Quartz Crystal Drive Level Calculation
65. Valid Number
JS mask important data of ID card and mobile phone number with * *
4sum of leetcode topic analysis
[qnx hypervisor 2.2 user manual]5.6.1 silent device during guest shutdown
披萨订购设计----简单工厂模式
扫码登录基本流程
JSP入门总结