redis字符串 string

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/wj123446/article/details/90577871
列出了redis常用字符串命令

set key value

设置制定key的值

127.0.0.1:6379> set key value
OK
127.0.0.1:6379> get key
"value"

get key

redis get命令用于获取指定的key值,如果key不存在,返回nil,如果key值存储的不是字符串类型,那么返回一个错误

127.0.0.1:6379> get db
(nil)
127.0.0.1:6379> set db redis
OK
127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> del db
(integer) 1
127.0.0.1:6379> lpush db redis mongodb mysql
(integer) 3
127.0.0.1:6379> get db
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> lrange db 0 -1
1) "mysql"
2) "mongodb"
3) "redis"

getrange key start end

描述:返回key中字符串值的子字符

redis getrange命令用于获取存储在指定key中字符串的子字符串,字符串的截取范围由start和end两个偏移量决定(start和end在内)

127.0.0.1:6379> set mykey "This is my test key"
OK
127.0.0.1:6379> getrange mykey 0 3
"This"
127.0.0.1:6379> getrange mykey 0 -1
"This is my test key"

**getset key value

描述:将给定key的值设为value,并返回key的旧值(old value)

返回值:返回给定key的旧值,当key没有旧值时,即key不存在时,返回nil,当key存在但不是字符串类型时,返回一个错误

127.0.0.1:6379> getset db mongodb
(nil)
127.0.0.1:6379> get db
"mongodb"
127.0.0.1:6379> getset db redis
"mongodb"
127.0.0.1:6379> get db
"redis"

mget key1 key2 …

描述:获取所有(一个或多个)给定key的值

返回值:redis mget命令返回所有(一个或多个)给定key的值,如果给定的key里面,有某个key不存在,那么这个key返回特殊值nil

127.0.0.1:6379> set key1 "hello"
OK
127.0.0.1:6379> set key2 "world"
OK
127.0.0.1:6379> mget key1 key2 someOtherKey
1) "hello"
2) "world"
3) (nil)

setex key seconds value

描述:redis setex命令为指定的key设置值及其过期时间,如果key已经存在,setex命令将会替换旧的值

返回值:设置成功时返回OK

127.0.0.1:6379> setex mykey 20 redis
OK
127.0.0.1:6379> ttl mykey
(integer) 12
127.0.0.1:6379> get mykey
"redis"
127.0.0.1:6379> get mykey
(nil)

setnx key value

描述:在指定的key不存在时,为key设置指定的值

返回值:设置成功,返回1.设置失败,返回0

127.0.0.1:6379> exists job
(integer) 0
127.0.0.1:6379> setnx job "programmer"
(integer) 1
127.0.0.1:6379> get job
"programmer"
127.0.0.1:6379> setnx job "code-farmer"
(integer) 0
127.0.0.1:6379> get job
"programmer"

setrange key offset value

描述:用指定的字符串覆盖给定key所存储的字符串值,覆盖的位置从偏移量offset开始

返回值:被修改后的字符串长度

127.0.0.1:6379> set key1 "hello world"
OK
127.0.0.1:6379> setrange key1 6 redis
(integer) 11
127.0.0.1:6379> get key1
"hello redis"

strlen key

描述:用于获取指定key所储存字符串值的长度,当key存储的不是字符串值时,返回一个错误

返回值:字符串值的长度。当key不存在时,返回0

127.0.0.1:6379> set mykey "hello world"
OK
127.0.0.1:6379> strlen mykey
(integer) 11
127.0.0.1:6379> strlen mykey
(integer) 11
127.0.0.1:6379> strlen nonexisting

**mset key1 value1 key2 value2 … keyN valueN

描述:用于同时设置一个或多个key-value对

127.0.0.1:6379> mset key1 "hello" key2 "world"
OK
127.0.0.1:6379> get key1
"hello"
127.0.0.1:6379> get key2
"world"

msetnx key1 value1 key2 value2

描述:用于所有给定key都不存在时,同时设置一个或多个key-value对

返回值:当所有key都成功设置,返回1.如果所有给定key都设置失败(至少有一个key已经存在),那么返回0

127.0.0.1:6379> msetnx rmdbs "mysql" nosql "mongodb" key-value-store "redis"
(integer) 1
127.0.0.1:6379> mget rmdbs nosql key-value-store
1) "mysql"
2) "mongodb"
3) "redis"
127.0.0.1:6379> msetnx rmdbs "sqlite" language "python"
(integer) 0
127.0.0.1:6379> get rmdbs
"mysql"
127.0.0.1:6379> exists language
(integer) 0

psetex key milliseconds value

描述:以毫秒为单位设置key的生存时间

设置成功返回OK

127.0.0.1:6379> psetex mykey 10000 "hello wj"
OK
127.0.0.1:6379> pttl mykey
(integer) 6965
127.0.0.1:6379> ttl mykey
(integer) 4
127.0.0.1:6379> get mykey
"hello wj"
127.0.0.1:6379> get mykey
(nil)

incr key

描述:将key中储存的数字值增加一

如果key不存在,那么key的值会先被初始化为0,然后在执行incr操作,如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误

127.0.0.1:6379> set page_view 20
OK
127.0.0.1:6379> incr page_view
(integer) 21
127.0.0.1:6379> get page_view
"21"
127.0.0.1:6379> set view wj
OK
127.0.0.1:6379> incr view
(error) ERR value is not an integer or out of range
127.0.0.1:6379> get view
"wj"

incrby key increment

描述:将key中储存的数字加上指定的增量值,其他和incr一样

127.0.0.1:6379> set rank 50
OK
127.0.0.1:6379> incrby rank 20
(integer) 70
127.0.0.1:6379> get rank
"70"
127.0.0.1:6379> exists count
(integer) 0
127.0.0.1:6379> incrby count 30
(integer) 30
127.0.0.1:6379> get count
"30"
127.0.0.1:6379> set book "long long ago"
OK
127.0.0.1:6379> incrby book 200
(error) ERR value is not an integer or out of range

decr key

将key中储存的数字值数字值减1

127.0.0.1:6379> set failure_num 10
OK
127.0.0.1:6379> decr failure_num
(integer) 9
127.0.0.1:6379> get failure_num
"9"

decrby key decrement

将key所存储的值减去指定的减量值

127.0.0.1:6379> set count 100
OK
127.0.0.1:6379> decrby count 20
(integer) 80
127.0.0.1:6379> get count
"80"

append key value

如果key已经存在并且是一个字符串, append命令将指定的value追加到该key原来值(value)的末尾,如果key不存在,append就简单的将key设置为value

127.0.0.1:6379> exists myphone
(integer) 0
127.0.0.1:6379> append myphone "xiaomi"
(integer) 6
127.0.0.1:6379> append myphone "-mi8"
(integer) 10
127.0.0.1:6379> get myphone
"xiaomi-mi8"
展开阅读全文

没有更多推荐了,返回首页