鱼喃

听!布鲁布鲁,大鱼又在那叨叨了

redis入门(2)---5种数据类型

types

commands

  • KEYS pattern
  • EXISTS key
  • DEL key
  • TYPE key
pattern
  • ?
  • []
  • \x

string

  • SET key value
  • GET key
  • MSET key1 value1 key2 value2 …
  • MGET key1 key2 …
  • INCR key
  • INCRBY key increment
  • DECR key
  • DECRBY key increment
  • INCRBYFLOAT key increment
  • APPEND key value
  • SRELEN key
  • GETBIT key offset
  • SETBIT key offset value
  • BITCOUNT key [start end]
  • BITOP operation destkey key1 [key2 …]

hash

  • HSET key field value
  • HGET key field
  • HMSET key field1 value1 [field2 value2 …]
  • HMGET key field1 [field2…]
  • HGETALL key
  • HEXISTS key field
  • HSETNX key field value (set if field not exists)
  • HINCRBY key field increment
  • HDEL key field [field…]
  • HKEYS key
  • HVALS key
  • HLEN key (count of fields)

list

  • LPUSH key value [value…]
  • RPUSH key value [value…]
  • LPOP key
  • RPOP key
  • LLEN key
  • LRANGE key start end (-n means the n-th number from right)
  • LREM key count value (remove the first [count] elements from the left who’s value is [value])
  • LINDEX key index
  • LSET key index value
  • LTRIM key start end (remove all other elements out of list[start:end])
  • LINSERT key BEFORE|AFTER value1 value2 (insert value2 before/after value1)
  • RPOPLPUSH srckey dstkey (RPOP + LPUSH , 1 element each time)

set

  • SADD key member [member…]
  • SREM key member [member…]
  • SMEMBERS key
  • SISMEMBER key member
  • SDIFF key1 [key2 …] (belong to key1 but not belong to key2, and …)
  • SINTER key [key…] (belong to key1 and key2 and …)
  • SUNION key [key…] (belong to key1 or key2 or …)
  • SCARD key (count elements of set)
  • SDIFFSTORE dstkey key1 [key2…] (do operations and store results to dstkey)
  • SINTERSTORE dstkey key1 [key2…]
  • SUNIONSTORE dstkey key1 [key2…]
  • SRANDMEMBER key [count] (get random member(s) in set, unique) (note: this is not absolutely random. redis use hashtable to store set. it take 2 steps to get a random member, firstly, choose a bucket randomly, and then choose one element in that bucket randomly. this means those elements in buckets that has less elements will have more opportunity to by choosen)
  • SPOP key ( = SRANDMEMBER key 1)

sorted set (from small score to large)

  • ZADD key score member [score member …]
  • ZSCORE key member
  • ZRANGE key start end (s[start:end])
  • ZREVRANGE key start end (reverse of s[start:end])
  • ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] (get elements whose score>=min && score<=max)
  • ZINCRBY key increment member (increse score of member by increment)
  • ZCARD key (get count of members)
  • ZCOUNT key min max (count members whose score>=min and <=max)
  • ZREM key member [member…]
  • ZREMRANGEBYRANK key start end
  • ZREMRANGEBYSCORE key min max
  • ZRANK key member (get rank of member, start from 0, smallest is 0)
  • ZREVRANK key menber (get rank of member, start from 0, largest is 0)
  • ZINTERSTORE dstkey countOfKeys key1 [key2…] [WEIGHTS weight1 [weight2…]] [AGGREGATE SUM|MIN|MAX] (get members which belong to key1,key2,… and store into dstkey) (WEIGHTS: set weight of each set) (AGGREGATE: determine how to handle scores of dstkey, default is to summarize each socre)

store methods

typeencoding methodOBJECT ENCODING result
stringREDIS_ENCODING_RAW"raw"
REDIS_ENCODING_INT"int"
hashREDIS_ENCODING_HT"hashtable"
REDIS_ENCODING_ZIPLIST"zirlist"
listREDIS_ENCODING_LINKEDLIST"linkedlist"
REDIS_ENCODING_ZIPLIST"ziplist"
setREDIS_ENCODING_HT"hashtable"
REDIS_ENCODING_INTSET"intset"
sorted listREDIS_ENCODING_SKIPLIST"skiplist"
REDIS_ENCODING_ZIPLIST"ziplist"