2.redis的5种数据类型

redis跟memcached其中一个区别,它支持更多的数据类型,下面就是常见的数据类型

1.string

redis最基本的数据类型,最大支持大小512MB,memcached只支持1MB。你可以用它来存储邮箱,或者json化的图片。

常见的命令操作:set/get/append/strlen/incr/desr/incrby/descrby/getset/setex/setnx/setrange/getrange/mset/mget/msetnx

  eg:

    ①.getset key value

      获取key对应的值,并设置新值,返回的是设置之前的值

      eg:

        192.168.31.182:6379> getset mycounter 3

        "2"

    ②.setex key time value

      设置key/value并添加上失效时间(单位是秒)

      eg:

        192.168.31.182:6379> setex hello 10 hello,redis

        OK

      可以通过ttl key查看失效时间,当值为负数,表示已经失效了

      eg:

        192.168.31.182:6379> ttl hello

        (integer) 4

        192.168.31.182:6379> ttl hello

        (integer) 3

        192.168.31.182:6379> ttl hello
        (integer) 2
        192.168.31.182:6379> ttl hello
        (integer) 1
        192.168.31.182:6379> ttl hello
        (integer) -2

        192.168.31.182:6379> get hello

        (nil)

      已经设置的key可以通过expire 命令添加失效时间

      eg:

        192.168.31.182:6379> set hello hello,redis

        OK

        192.168.31.182:6379> expire hello 10

        (integer) 1

        192.168.31.182:6379> ttl hello

        (integer) -2

        192.168.31.182:6379> get hello
        (nil)        

  其它具体string命令操作参照http://www.redis.cn/commands.html#string

2.hash散列类型

redis的hash类型可以看作是一个string作为key,value是一个string/value的map,适合存储java中的对象类型。

  

  常用的的命令:hset/hget/hdel/hexists/hlen/hsetnx/hincrby/hgetall/hkeys/hvals/hmget/hmset

  eg:

    此处不列举,hset命令比较简单

  其它hash常见的命令参照:http://www.redis.cn/commands.html#hash

3.list列表类型

  redis中的列表类型存储一个有序的字符串链表,可以在头部(left)尾部(right)插入,在插入的时候key不存在,创建新的链表,链表中所有的元素被移除,则此链表也不存在了,链表操作不管是在头部和尾部效率都是极高的,如果操作链表中部,效率极低。list结构相当于一个队列。

常见的命令:lpush/lpushx/lrange/lpop/llen/linsert/rpush/rpushx/rpop/rpoplpush

  eg:

    ①.LPUSH key value 

      从头部压入链表中

      192.168.31.182:6379> lpush list 'a'

      (integer) 1

      192.168.31.182:6379> lpush list 'b' 'c' 'd'

      (integer) 4

      lrange key value 0 -1 是遍历出所有的list中的值

      192.168.31.182:6379> lrange list 0 -1

      1) "d"

      2) "c"

      3) "b"

      4) "a"

      为何不是abcd,因为数据是从顶(left)入压入的,原理如下图

  

  其它list常见命令参照http://www.redis.cn/commands.html#list

  

 

  

 

posted @ 2015-08-12 19:29  云^淡  阅读(49)  评论(0)    收藏  举报