首页 > 其他 > 详细

Redis的数据类型 - String字符串类型

时间:2017-06-23 21:41:37      阅读:317      评论:0      收藏:0      [点我收藏+]

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  

  SET test6 ‘this is a test6‘ EX 100 PX 300000 NX

  SET testStr1 ‘this is a test‘

 

GET:根据key找到对应的值

  语法:GET key

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

 

GETRANGE返回字符串中一部分

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

    GETRANGE test2 0 4  #返回结果为 ‘this‘ #

    GETRANGE test2 0 -3  #从第一个到倒数第三个,返回结果为 ‘this is a te‘ #

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

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

 

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

  语法:GETSET key value

    SET test3 ‘tom‘

    GET test

    GETSET test ‘jerry‘  #当key不存在返回nil,如果key不是字符串,会报错#

 

MSET一次设置多个键值对

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

    MSET test4 ‘tom‘ test5 ‘jerry‘ test6 ‘tom - jerry‘


MGET:一次得到多个键值

  语法:MGET key key

    MGET test4 test5 test6

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

 

STRLEN获取key的字符串长度

  语法:STRLEN key

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

 

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‘  #返回 ‘\x00\x00\x00\x00\x00tom‘,这里不存在的地方用了5个零字节来代替#

 

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

  语法:SETNX key value

    EXISTS test10  #返回(integer) 0 #

    SETNX test10 ‘tom‘

    GET test10  #返回 ‘tom‘ #

 

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

  语法:SETEX key seconds value

    SETEX test11 60 ‘this is a test11‘  

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

 

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 #

 

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

  语法:PSETEX key milliseconds value

    PSETEX test16 20000 ‘hello world‘

    PTTL test16  #返回生命周期#

 

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

  语法:INCR key

    SET count 1

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

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

 

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

  语法:INCRBY key INCREMENT

    SET count2 10

    INCRBY count2 5  #返回15#

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

 

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

  语法:INCRBYFLOAT key increment

    SET count3 1

    INCRBYFLOAT count3 1.2  #返回4.2#

 

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

  语法:DECR key

    DECR count3  #返回3.2#


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

  语法:DECRBY key decrement

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

 

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

  语法:APPEND key value

    APPEND test2 ‘edu‘

    APPEND noExistsStr ‘this is a test‘  #如果key不存在,则相当于执行的SET操作#

Redis的数据类型 - String字符串类型

原文:http://www.cnblogs.com/shuo-128/p/7071548.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!