当前位置:网站首页>Redis data type (string)
Redis data type (string)
2022-06-11 21:26:00 【Don't like learning since childhood~】
Redis data type (String)
127.0.0.1:6379> flushall
OK
127.0.0.1:6379> set key1 v1 # Set the value
OK
127.0.0.1:6379> get key1 # Get value
"v1"
127.0.0.1:6379> keys * # Get all key
1) "key1"
127.0.0.1:6379> exists key1 # Judge a certain key Whether there is
(integer) 1
127.0.0.1:6379> APPEND key1 'hello' # Append string , If at present key non-existent , Equivalent to set key
(integer) 7
127.0.0.1:6379> get key1
"v1hello"
127.0.0.1:6379> STRLEN key1 # Get string length
(integer) 7
127.0.0.1:6379> APPEND key1 'Corleone'
(integer) 15
127.0.0.1:6379> STRLEN key1
(integer) 15
127.0.0.1:6379> get key1
"v1helloCorleone"
127.0.0.1:6379> set views 0
OK
127.0.0.1:6379> get views
"0"
127.0.0.1:6379> incr views # Set the gain 1
(integer) 1
127.0.0.1:6379> incr views
(integer) 2
127.0.0.1:6379> incr views
(integer) 3
127.0.0.1:6379> incr views
(integer) 4
127.0.0.1:6379> get views
"4"
127.0.0.1:6379> decr views # Set self subtraction 1
(integer) 3
127.0.0.1:6379> decr views
(integer) 2
127.0.0.1:6379> decr views
(integer) 1
127.0.0.1:6379> decr views
(integer) 0
127.0.0.1:6379> decr views
(integer) -1
127.0.0.1:6379> get views
"-1"
127.0.0.1:6379> INCRBY views 10 # Set step size , Specify increment
(integer) 9
127.0.0.1:6379> INCRBY views 10
(integer) 19
127.0.0.1:6379> DECRBY views 5 # Set step size , Specify increment
(integer) 14
127.0.0.1:6379> DECRBY views 5
(integer) 9
String range
127.0.0.1:6379> set key1 'hello,kuangshen' # Set up key1
OK
127.0.0.1:6379> get key1
"hello,kuangshen"
127.0.0.1:6379> GETRANGE key1 0 3 # Intercepting string [0,3]
"hell"
127.0.0.1:6379> GETRANGE key1 0 -1 # Get all the strings and get key It's the same
"hello,kuangshen"
Replace string
127.0.0.1:6379> set key2 abcdefg
OK
127.0.0.1:6379> get key2
"abcdefg"
127.0.0.1:6379> SETRANGE key2 1 xx # Replace the string starting at the specified position
(integer) 7
127.0.0.1:6379> get key2
"axxdefg"
setex(set with expire) Set expiration time
setnx(set if not exist) # Does not exist in setting ( It is often used in distributed locks !)
127.0.0.1:6379> setex ky3 30 "hello" # Set up key3 The value of is hello 30 Seconds after expired
OK
127.0.0.1:6379> ttl key3
(integer) -2
127.0.0.1:6379> ttl key3
(integer) -2
127.0.0.1:6379> ttl ky3
(integer) -2
127.0.0.1:6379> keys *
1) "key2"
2) "key1"
127.0.0.1:6379> setex key3 30 "hello"
OK
127.0.0.1:6379> ttl key3
(integer) 25
127.0.0.1:6379> ttl key3
(integer) 24
127.0.0.1:6379> ttl key3
(integer) 23
127.0.0.1:6379> get key3
"hello"
127.0.0.1:6379> ttl key3
(integer) 3
127.0.0.1:6379> setnx mykey "redis" # If mykey non-existent , establish mykey
(integer) 1
127.0.0.1:6379> keys *
1) "mykey"
2) "key2"
3) "key1"
127.0.0.1:6379> ttl key3
(integer) -2
127.0.0.1:6379> setnx mykey "MongoDB" # If mykey There is , Create failure
(integer) 0
127.0.0.1:6379> GET mykey
"redis"
mset : Set multiple values at the same time
mget : Get multiple values at the same time
127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 # Set multiple values at the same time
OK
127.0.0.1:6379> keys *
1) "key2"
2) "k2"
3) "key1"
4) "k1"
5) "mykey"
6) "k3"
127.0.0.1:6379> msetnx k1 v1 k4 v4 # msetnx It's an atomic operation , Or make it together , Or fail together !
(integer) 0
127.0.0.1:6379> get k4
(nil)
127.0.0.1:6379> mget k1 k2 k3 # Get multiple values at the same time
1) "v1"
2) "v2"
3) "v3"
object
set user:1 {
name:zhangsan,age:3} # Set up a user:1 object , The value is json Character to save an object
# there key It's a clever design :user:{id}:{filed}, So designed in Redis Chinese is completely OK 了 !
127.0.0.1:6379> mset user:1:name zhangsan user:1:age 2
OK
127.0.0.1:6379> mget user:1:name user:1:age
1) "zhangsan"
2) "2"
getset First get And then again set
127.0.0.1:6379> getset db redis # If there is no value , Then return to nil
(nil)
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> getset db mongodb # If there is value , Get the original value , And set the new value
"redis"
127.0.0.1:6379> get db
"mongodb"
Data structures are interlinked !
String Similar usage scenarios :value In addition to our string, it can also be our number !
- Counter
- Count the number of multiple units
- Number of fans
- Object cache storage !
边栏推荐
猜你喜欢

JVM对象分配策略TLAB

Live broadcast with practice | 30 minutes to build WordPress website with Alibaba cloud container service and container network file system

LabVIEW控制Arduino实现红外测距(进阶篇—6)

LabVIEW控制Arduino实现超声波测距(进阶篇—5)

LeetCode-110-平衡二叉树

ORA-04098: trigger ‘xxx.xxx‘ is invalid and failed re-validation
![[game theory complete information static game] strategic game](/img/d2/743e8d14e4fb27cbe883d1df1bca27.jpg)
[game theory complete information static game] strategic game

Deriving Kalman filter from probability theory

Use float to create a page header, footer, left content, and main content.

How to manually drag nodes in the Obsidian relationship graph
随机推荐
Part I physical layer
JVM之堆区
线性表的链式存储结构
Refresh and upgrade | innovation, starting from cloud store
解决 img 5px 间距的问题
RANSAC提取平面(MATLAB内置函数)
Serval and Rooted Tree(CF1153D)-DP
Object creation process of JVM
flutter系列之:flutter中常用的container layout详解
【C語言進階】整型在內存中的存儲
Diary at 16:29:41 on June 9, 2022
Obsidian关系图谱如何让节点可以手动拖动
JVM runtime constant pool and direct memory
[thinking about life] words and sounds
How to Load Data from CSV (Data Preparation Part)
The official announced the launch of Alibaba's 2023 global school recruitment: Technical Posts account for more than 60%
【生活思考】文字与语音
Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
How to import workflows provided on SAP API hub to sap BTP
俩月没发过博客了,发一篇证明自己的账号还活着