Redis 的数据类型

SET设置key对应的值为value

  语法:SET key value [EX seconds] [PX milliseconds] [NX|XX]  #一个键最多存储512MB,如果key存在,同名进行覆盖#

    EX: seconds:设置键的key的过期时间SET key value EX seconds -- SETEX

    PX: milliseconds:以毫秒的形式设置过期时间SET key value PX milliseconds -- PSETEX

    NX: 只有键不存在的时候才可以设置成功SET key value NX--SETNX

    XX: 只有key已经存在的时候才可以设置

  SET test1 'this is a test1' EX 100  #100秒后过期#

  SET test2 'this is a test2' PX 20000  #20000毫秒后过期#

  SET test3 'this is a test3' NX  #设置成功#

  SET test3 'this is a test33' NX  #设置失败,test3已经存在#

  SET test4 'this is a test4' XX  #设置失败,test4不存在#

  SET test5 'this is a test5' EX 100 NX

127.0.0.1:6379> SET test1 'this is a test1' EX 100
OK

127.0.0.1:6379> SET test2 'this is a test2' PX 20000
OK

127.0.0.1:6379> SET test3 'this is a test3' NX
OK

127.0.0.1:6379> SET test3 'this is a test33' NX
(nil)

127.0.0.1:6379> SET test4 'this is a test4' XX
(nil)

127.0.0.1:6379> SET test5 'this is a test5' EX 100 NX
OK

GET:根据key找到对应的值

  语法:GET key

    GET test1  #如果key不存在,返回nil,如果key不是字符串,会报错#

127.0.0.1:6379> GET test1
(nil)

127.0.0.1:6379> GET test3
"this is a test3"

GETRANGE返回字符串中一部分

  语法:GETRANGE key start end  #不支持从右向左截取#

    GETRANGE test3 0 4  #返回结果为 'this ' #

    GETRANGE test3 0 -3  #从第一个到倒数第三个,返回结果为 'this is a tes' #

    GETRANGE test3 -4 -2  #从第一个到倒数第三个,返回结果为 'est' #

    GETRANGE test3 0 1000  #test2里一共就15个字符,所以返回值只有15个字符 'this is a test3' #

127.0.0.1:6379> GETRANGE test3 0 4
"this "

127.0.0.1:6379> GETRANGE test3 0 -3
"this is a tes"

127.0.0.1:6379> GETRANGE test3 -4 -2
"est"

127.0.0.1:6379> GETRANGE test3 0 1000
"this is a test3"

GETSET设置指定key的值,并且返回旧的值

  语法:GETSET key value

    SET test3 'tom'

    GET test3

    GETSET test3 'jerry'  #设置成功返回原值,当key不存在返回nil,如果key不是字符串,会报错#

127.0.0.1:6379> SET test3 'tom'
OK

127.0.0.1:6379> GET test3
"tom"

127.0.0.1:6379> GETSET test3 'jerry'
"tom"

MSET一次设置多个键值对

  语法:MSET key value [key value...]

    MSET test4 'tom' test5 'jerry' test6 'tom - jerry'

127.0.0.1:6379> MSET test4 'tom' test5 'jerry' test6 'tom - jerry'
OK


MGET:一次得到多个键值

  语法:MGET key key

    MGET test4 test5 test6

    MGET test4 test5 test6 test7  #返回值为 'tom' 'jerry' 'tom - jerry' 'null',如果键不存在,则对应的键值会输出null#

127.0.0.1:6379> MGET test4 test5 test6
1) "tom"
2) "jerry"
3) "tom - jerry"

127.0.0.1:6379> MGET test4 test5 test6 test7
1) "tom"
2) "jerry"
3) "tom - jerry"
4) (nil)

STRLEN获取key的字符串长度

  语法:STRLEN key

    STRLEN test5  #返回值为5,对于不存在key获取其长度返回的0#

127.0.0.1:6379> STRLEN test5
(integer) 5

SETRANGE:相当于字符串替换的效果

  语法:SETRANGE key start value  #如果设置的key原来的字符串长度要比偏移量小,就会以零字节(x00)来填充#

    SET test8 'hello tom'

    SETRANGE test8 6 'jerry'

  对不存在的key使用SETRANGE

    EXISTS test9  #返回(integer) 0 ,先用EXISTS来检测一下test9是否存在,为0表示不存在#

    SETRANGE test9 5 'tom'  #返回 'x00x00x00x00x00tom',这里不存在的地方用了5个零字节来代替#

127.0.0.1:6379> STRLEN test5
(integer) 5

127.0.0.1:6379> SET test8 'hello tom'
OK

127.0.0.1:6379> SETRANGE test8 6 'jerry'
(integer) 11

127.0.0.1:6379> EXISTS test9
(integer) 0

127.0.0.1:6379> SETRANGE test9 5 'tom'
(integer) 8

127.0.0.1:6379> GET test9
"x00x00x00x00x00tom"

SETNX:只有key不存在才能设置成功

  语法:SETNX key value

    EXISTS test10  #返回(integer) 0 #

    SETNX test10 'tom'

    GET test10  #返回 'tom' #

127.0.0.1:6379> EXISTS test10
(integer) 0

127.0.0.1:6379> SETNX test10 'tom'
(integer) 1

127.0.0.1:6379> GET test10
"tom"

SETEX:设置key并且设置其过期时间

  语法:SETEX key seconds value

    SETEX test11 60 'this is a test11'  

    #SETEX是原子性操作,相当于执行了SET key value,又对这个key设置了过期时间EXPIRE key seconds#

127.0.0.1:6379> SETEX test11 60 'this is a test11'
OK

MSETNX:一次设置多个key-value对,只有所有的key都不存在的时候才会成功

  语法:MSETNX key value [key value]

    MSETNX test13 'a' test14 'b' test15 'c'  # 'test13' 'test14' 'test15' 都不存在,设置成功返回 1 #

    MSETNX test15 'aa' test16 'bb' test17 'cc'  #因为 'test15' 已经存在了,所以全部设置失败,返回 0 #

127.0.0.1:6379> MSETNX test13 'a' test14 'b' test15 'c'
(integer) 1

127.0.0.1:6379> MSETNX test15 'aa' test16 'bb' test17 'cc'
(integer) 0

PSETEX:以毫秒为单位设置key的生存周期

  语法:PSETEX key milliseconds value

    PSETEX test16 20000 'hello world'

    PTTL test16  #返回生命周期#

127.0.0.1:6379> PSETEX test16 20000 'hello world'
OK

127.0.0.1:6379> PTTL test16
(integer) 10616

127.0.0.1:6379> PTTL test16
(integer) -2

INCR:对key中存储的数字进行递增操作

  语法:INCR key

    SET count 1

    INCR count  #key如果不存在,则会先初始化为0,在进行INCR操作#

    INCR test1  #如果key存储的不是数字,会报错#

127.0.0.1:6379> SET count 1
OK

127.0.0.1:6379> INCR count
(integer) 2

INCRBY:将key中存储的数字加上指定增量

  语法:INCRBY key INCREMENT

    SET count2 10

    INCRBY count2 5  #返回15#

    INCRBY count2 1.2  #这里增加的数不可以是小数,如果是小数会报错#

127.0.0.1:6379> INCRBY count2 5
(integer) 5

127.0.0.1:6379> INCRBY count2 1.2
(error) ERR value is not an integer or out of range

INCRBYFLOAT:给key中存储的数字加上指定的浮点数

  语法:INCRBYFLOAT key increment

    SET count3 1

    INCRBYFLOAT count3 1.2  #返回4.2#

127.0.0.1:6379> SET count3 1
OK

127.0.0.1:6379> INCRBYFLOAT count3 1.2
"2.2"

DECR:将key中存储的数字进行递减操作

  语法:DECR key

    DECR count3  #递减只能对 int 型进行操作,不是 int 型会报错 #

127.0.0.1:6379> DECR count3
(error) ERR value is not an integer or out of range

127.0.0.1:6379> DECR count1
(integer) -1


DECRBY:将key中存储的数值减去指定的值

  语法:DECRBY key decrement

    DECRBY counter2 3  #返回12,这里减法的操作没有float型,无法减小数#

127.0.0.1:6379> DECRBY counter2 3
(integer) 12

APPEND:通过APPEND将值追加到字符串的末尾

  语法:APPEND key value

    APPEND test2 'tom'  #返回值为 'this is a test2tom ' #

    APPEND test20 'this is a test20'  #返回值为 'this is a test20',如果key不存在,则相当于执行的SET操作#

127.0.0.1:6379> APPEND test2 'tom'
(integer) 4

127.0.0.1:6379> APPEND test20 'this is a test20'
(integer) 16
原文地址:https://www.cnblogs.com/shuo-128/p/7071548.html