556   Redis

Redis主要包含5数据结构:

  • String,字符型值
  • Byte,字节型值
  • Hash,哈希集
  • List,列表,值可重复
  • Set,集合,值不可重复
  • Sorted Set,有序的集合

1,String,用于存取简单的key/value键值对
单个操作:set,get

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

批量操作:mset,mget

127.0.0.1:6379> mset key1 one key2 two key3 three
OK
127.0.0.1:6379> mget key1 key2 key3
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> 

追加字符串:append

127.0.0.1:6379> set mykey 'hello'
OK
127.0.0.1:6379> get mykey
"hello"
127.0.0.1:6379> append mykey 'world'
(integer) 10
127.0.0.1:6379> get mykey
"helloworld"
127.0.0.1:6379>

2,Byte,以Bit形式即0或1存取
保存、获取和计算:setbit,getbit,bitcount

127.0.0.1:6379> setbit mybyte 7 1
(integer) 0
127.0.0.1:6379> getbit mybyte 7
(integer) 1
127.0.0.1:6379> get mybyte
"\x01"
127.0.0.1:6379> setbit mybyte 6 1
(integer) 0
127.0.0.1:6379> getbit mybyte 6
(integer) 1
127.0.0.1:6379> get mybyte
"\x03"
127.0.0.1:6379> bitcount mybyte
(integer) 2

3,Hash,以哈希集合保存
单个和批量操作:hset,hget,hmset,hmget

127.0.0.1:6379> hset myhash key1 "hello"
(integer) 1
127.0.0.1:6379> hget myhash key1
"hello"
127.0.0.1:6379> hset myhash key2 "hello"
(integer) 1
127.0.0.1:6379> hset myhash key2 "world"
(integer) 0
127.0.0.1:6379> hget myhash key2
"world"
127.0.0.1:6379> hmget myhash key1 key2
1) "hello"
2) "world"

3,List,可以重复保存值
添加和查看:lpush,lrange

127.0.0.1:6379> lpush mylist 'one'
(integer) 1
127.0.0.1:6379> lpush mylist 'two'
(integer) 2
127.0.0.1:6379> lrange mylist 0 -1
1) "two"
2) "one"
127.0.0.1:6379> lpush mylist 'three'
(integer) 3
127.0.0.1:6379> lpush mylist 'two'
(integer) 4
127.0.0.1:6379> lrange mylist 0 -1
1) "two"
2) "three"
3) "two"
4) "one"
127.0.0.1:6379> 

删除和截取:lpop,ltrim

127.0.0.1:6379> lpush mylist 'one'
(integer) 1
127.0.0.1:6379> lpush mylist 'two'
(integer) 2
127.0.0.1:6379> lpush mylist 'three'
(integer) 3
127.0.0.1:6379> lrange mylist 0 -1
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> lpop mylist
"three"
127.0.0.1:6379> lrange mylist 0 -1
1) "two"
2) "one"

4,Set,跟list相似,但会去重,即重复值只保存一个
保存和查看:sadd,smembers

127.0.0.1:6379> sadd myset "one"
(integer) 1
127.0.0.1:6379> sadd myset "two"
(integer) 1
127.0.0.1:6379> smembers myset
1) "two"
2) "one"
127.0.0.1:6379> sadd myset "one"
(integer) 0
127.0.0.1:6379> smembers myset
1) "two"
2) "one"
127.0.0.1:6379> 

集合运算:sunion,sinter,sdiff

127.0.0.1:6379> sadd set1 a
(integer) 1
127.0.0.1:6379> sadd set1 b
(integer) 1
127.0.0.1:6379> sadd set1 c
(integer) 1
127.0.0.1:6379> sadd set2 b
(integer) 1
127.0.0.1:6379> sadd set2 c
(integer) 1
127.0.0.1:6379> sadd set2 d
(integer) 1
127.0.0.1:6379> sunion set1 set2
1) "c"
2) "a"
3) "b"
4) "d"
127.0.0.1:6379>
127.0.0.1:6379> sinter set1 set2
1) "c"
2) "b"
127.0.0.1:6379> sdiff set1 set2
1) "a"
127.0.0.1:6379> sdiff set2 set1
1) "d"
127.0.0.1:6379> 

5,Sorted Set,是有序的Set集合
保存和排序:zadd

127.0.0.1:6379> zadd mysort 1 one
(integer) 1
127.0.0.1:6379> zadd mysort 2 two1
(integer) 1
127.0.0.1:6379> zadd mysort 2 two2
(integer) 1
127.0.0.1:6379> zadd mysort 3 three
(integer) 1
127.0.0.1:6379> zadd mysort 1 four
(integer) 1
127.0.0.1:6379> zrange mysort 0 -1 withscores
 1) "four"
 2) "1"
 3) "one"
 4) "1"
 5) "two1"
 6) "2"
 7) "two2"
 8) "2"
 9) "three"
10) "3"




Leave a Reply

Your email address will not be published. Required fields are marked *