标签:des style blog http io ar color os 使用
<Redis系列(1)之安装>中介绍了Redis支持以下几种数据类型,那么本节主要介绍学习下这几种数据类型的基本操作
1 127.0.0.1:6379> set key hello 2 OK 3 127.0.0.1:6379> get key 4 "hello"
1 127.0.0.1:6379> incr num 2 (integer) 1 3 127.0.0.1:6379> incr num 4 (integer) 2 5 127.0.0.1:6379> get num 6 "2" 7 127.0.0.1:6379> incr key 8 (error) ERR value is not an integer or out of range
1 127.0.0.1:6379> incrby num 2 2 (integer) 4 3 127.0.0.1:6379> incrby num 3 4 (integer) 7 5 127.0.0.1:6379> get num 6 "7"
1 127.0.0.1:6379> decr num 2 (integer) 6 3 127.0.0.1:6379> decr num 4 (integer) 5 5 127.0.0.1:6379> get num 6 "5"
1 127.0.0.1:6379> decrby num 2 2 (integer) 3
1 127.0.0.1:6379> incrbyfloat num 2.7 2 "5.7" 3 127.0.0.1:6379> incrbyfloat num 5e+4 4 "50005.69999999999999929"
1 127.0.0.1:6379> append str hello 2 (integer) 5 3 127.0.0.1:6379> get str 4 "hello" 5 127.0.0.1:6379> append str ‘ world‘ 6 (integer) 11 7 127.0.0.1:6379> get str 8 "hello world"
1 127.0.0.1:6379> strlen str 2 (integer) 11 3 127.0.0.1:6379> set hello 你好 4 OK 5 127.0.0.1:6379> strlen hello 6 (integer) 6
1 127.0.0.1:6379> mset a 1 b 2 c 3 d 4 2 OK 3 127.0.0.1:6379> mget a b c d 4 1) "1" 5 2) "2" 6 3) "3" 7 4) "4"
//假设字符串bar的二进制为01100010 01100001 01110010 127.0.0.1:6379> set foo bar OK 127.0.0.1:6379> getbit foo 0 (integer) 0 127.0.0.1:6379> getbit foo 1 (integer) 1 127.0.0.1:6379> getbit foo 1000 (integer) 0
如果要设置的位置超过了键值二进制位的长度,setbit命令会自动将中间的二进制位设置为0,同理设置一个不存在的键的指定二进制位的值会自动将其前面的位赋值为0
1 127.0.0.1:6379> setbit foo 6 0 2 (integer) 1 3 127.0.0.1:6379> setbit foo 7 1 4 (integer) 0 5 127.0.0.1:6379> get foo 6 "aar" 7 127.0.0.1
1 127.0.0.1:6379> bitcount foo 2 (integer) 10 3 127.0.0.1:6379> bitcount foo 0 1 4 (integer) 6
BITOP支持AND OR XOR NOT操作。
1 127.0.0.1:6379> set foo1 bar 2 OK 3 127.0.0.1:6379> set foo2 aar 4 OK 5 127.0.0.1:6379> bitop or res foo1 foo2 6 (integer) 3 7 127.0.0.1:6379> get res 8 "car" 9 127.0.0.1:6379> bitop not res1 foo1 10 (integer) 3 11 127.0.0.1:6379> get res1 12 "\x9d\x9e\x8d" 13 127.0.0.1:6379> bitop and res2 foo1 foo2 14 (integer) 3 15 127.0.0.1:6379> get res2 16 "`ar"
1 127.0.0.1:6379> hset car price 100 2 (integer) 1 3 127.0.0.1:6379> hmset car color blue name audi 4 OK 5 127.0.0.1:6379> hget car price 6 "100" 7 127.0.0.1:6379> hmget car peice color name 8 1) (nil) 9 2) "blue" 10 3) "audi" 11 127.0.0.1:6379> hgetall car 12 1) "price" 13 2) "100" 14 3) "color" 15 4) "blue" 16 5) "name" 17 6) "audi"
1 127.0.0.1:6379> hexists car price 2 (integer) 1 3 127.0.0.1:6379> hexists car model 4 (integer) 0
1 127.0.0.1:6379> hsetnx car model 11 2 (integer) 1 3 127.0.0.1:6379> hsetnx car model 22 4 (integer) 0 5 127.0.0.1:6379> hget car model 6 "11"
1 127.0.0.1:6379> hincrby person score 60 2 (integer) 60 3 127.0.0.1:6379> hincrby person score 15 4 (integer) 75 5 127.0.0.1:6379> get person score 6 (error) ERR wrong number of arguments for ‘get‘ command 7 127.0.0.1:6379> hget person score 8 "75"
1 127.0.0.1:6379> hdel car color model 2 (integer) 2 3 127.0.0.1:6379> hgetall car 4 1) "name" 5 2) "audi"
1 127.0.0.1:6379> hkeys cat 2 1) "name" 3 127.0.0.1:6379> hvals cat 4 1) "audi"
1 127.0.0.1:6379> hlen cat 2 (integer) 1
列表类型可以存储一个有序的字符串列表,常用的操作就是向列表两端添加元素或者获取列表的其中一个片段。列表的内部其实是一个双向链表数据结构。所以向两边添加数据的复杂度是o(1),获取的数据越接近两端,速度越快。但是这种结构的代价就是通过索引访问会很慢,需要从头到尾的遍历。列表的这种数据结构一个很好的用处就是微博的新鲜事,始终把最新鲜的事情放到两端。一个列表最多能容纳232-1个元素。
1 127.0.0.1:6379> lpush key 1 2 (integer) 1 3 127.0.0.1:6379> lpush key 2 3 4 4 (integer) 4 5 127.0.0.1:6379> rpush key 0 6 (integer) 5 7 127.0.0.1:6379> rpush key 0 -1 -2 8 (integer) 8 9 127.0.0.1:6379> lrange key 0 -1 10 1) "4" 11 2) "3" 12 3) "2" 13 4) "1" 14 5) "0" 15 6) "0" 16 7) "-1" 17 8) "-2" 18 127.0.0.1:6379> llen key 19 (integer) 8 20 127.0.0.1:6379> lpop key 21 "4" 22 127.0.0.1:6379> rpop key 23 "-2" 24 127.0.0.1:6379> lrange key 0 -1 25 1) "3" 26 2) "2" 27 3) "1" 28 4) "0" 29 5) "0" 30 6) "-1" 31 127.0.0.1:6379> lrange key -2 -1 32 1) "0" 33 2) "-1" 34 127.0.0.1:6379> llen key 35 (integer) 6 36 127.0.0.1:6379> lrem key 1 2 37 (integer) 1 38 127.0.0.1:6379> lrem key -2 0 39 (integer) 2 40 127.0.0.1:6379> lrange key 0 -1 41 1) "3" 42 2) "1" 43 3) "-1"
1 LPUSH log $newlog 2 LTRIM log 0 99
1 127.0.0.1:6379> lpush key 1 2 3 4 5 6 2 (integer) 6 3 127.0.0.1:6379> rpush key 0 -1 -2 -3 -4 4 (integer) 11 5 127.0.0.1:6379> lrange key 0 -1 6 1) "6" 7 2) "5" 8 3) "4" 9 4) "3" 10 5) "2" 11 6) "1" 12 7) "0" 13 8) "-1" 14 9) "-2" 15 10) "-3" 16 11) "-4" 17 127.0.0.1:6379> lindex key 1 18 "5" 19 127.0.0.1:6379> lindex key -1 20 "-4" 21 127.0.0.1:6379> lset key 1 -1 22 OK 23 127.0.0.1:6379> lset key -1 1 24 OK 25 127.0.0.1:6379> lrange key 0 -1 26 1) "6" 27 2) "-1" 28 3) "4" 29 4) "3" 30 5) "2" 31 6) "1" 32 7) "0" 33 8) "-1" 34 9) "-2" 35 10) "-3" 36 11) "1" 37 127.0.0.1:6379> ltrim key 4 8 38 OK 39 127.0.0.1:6379> lrange key 0 -1 40 1) "2" 41 2) "1" 42 3) "0" 43 4) "-1" 44 5) "-2" 45 127.0.0.1:6379> linsert key before 0 3 46 (integer) 6 47 127.0.0.1:6379> linsert key after 0 -3 48 (integer) 7 49 127.0.0.1:6379> lrange key 0 -1 50 1) "2" 51 2) "1" 52 3) "3" 53 4) "0" 54 5) "-3" 55 6) "-1" 56 7) "-2" 57 127.0.0.1:6379> lrange key 0 -1 58 1) "-2" 59 2) "2" 60 3) "1" 61 4) "3" 62 5) "0" 63 6) "-3" 64 7) "-1"
在集合中每个元素是不同的且没有顺序的,一个集合类型同样可以最多存放232-1个元素。由于集合在redis内部是使用值为空的散列表实现的,所以时间复杂度是0(1)。我们可以使用多个集合类型键之间还可以进行并集,交集,和差集运算。
1 127.0.0.1:6379> sadd key a b c d e f 2 (integer) 6 3 127.0.0.1:6379> smembers key 4 1) "b" 5 2) "a" 6 3) "e" 7 4) "f" 8 5) "c" 9 6) "d" 10 127.0.0.1:6379> sismember key a 11 (integer) 1 12 127.0.0.1:6379> sismember key ag 13 (integer) 0 14 127.0.0.1:6379> sadd key a 15 (integer) 0 16 127.0.0.1:6379> sadd key g 17 (integer) 1
1 127.0.0.1:6379> sadd seta 1 2 3 2 (integer) 3 3 127.0.0.1:6379> sadd setb 2 3 4 4 (integer) 3 5 127.0.0.1:6379> sadd setc 2 3 6 (integer) 2 7 127.0.0.1:6379> sdiff seta setb 8 1) "1" 9 127.0.0.1:6379> sdiff seta setb setc 10 1) "1" 11 127.0.0.1:6379> sinter seta setb 12 1) "2" 13 2) "3" 14 127.0.0.1:6379> sinter seta setb setc 15 1) "2" 16 2) "3" 17 127.0.0.1:6379> sunion seta setb 18 1) "1" 19 2) "2" 20 3) "3" 21 4) "4" 22 127.0.0.1:6379> sunion seta setb setc 23 1) "1" 24 2) "2" 25 3) "3" 26 4) "4"
1 127.0.0.1:6379> sadd seta 1 2 3 2 (integer) 3 3 127.0.0.1:6379> sadd setb 2 3 4 4 (integer) 3 5 127.0.0.1:6379> sadd setc 2 3 6 (integer) 2 7 127.0.0.1:6379> scard seta 8 (integer) 3 9 127.0.0.1:6379> scard setb 10 (integer) 3 11 127.0.0.1:6379> scard setc 12 (integer) 2 13 127.0.0.1:6379> sdiffstore setaa seta setb setc 14 (integer) 1 15 127.0.0.1:6379> smembers setaa 16 1) "1" 17 127.0.0.1:6379> sinterstore setbb seta setb setc 18 (integer) 2 19 127.0.0.1:6379> smembers setbb 20 1) "2" 21 2) "3" 22 127.0.0.1:6379> sunionstore setcc seta setb setc 23 (integer) 4 24 127.0.0.1:6379> smembers setc 25 1) "2" 26 2) "3" 27 127.0.0.1:6379> smembers setcc 28 1) "1" 29 2) "2" 30 3) "3" 31 4) "4" 32 127.0.0.1:6379> srandmember setcc 33 "4" 34 127.0.0.1:6379> srandmember setcc 35 "3" 36 127.0.0.1:6379> srandmember setcc 2 37 1) "1" 38 2) "2" 39 127.0.0.1:6379> srandmember setcc 2 40 1) "1" 41 2) "3" 42 127.0.0.1:6379> srandmember setcc -2 43 1) "2" 44 2) "2" 45 127.0.0.1:6379> srandmember setcc -10 46 1) "3" 47 2) "3" 48 3) "3" 49 4) "4" 50 5) "4" 51 6) "1" 52 7) "4" 53 8) "4" 54 9) "1" 55 10) "3" 56 127.0.0.1:6379> spop setcc 57 "2" 58 127.0.0.1:6379> smembers setcc 59 1) "1" 60 2) "3" 61 3) "4"
最后介绍Redis里面最高级的数据类型有序集合sorted set。有序集合在集合的基础上为每个元素关联了一个分数,这使得我们不仅可以完成插入,删除和判断元素是否存在等集合操作,还能够获取分数最高或者最低的前N个元素,获取指定分数范围内的元素等跟分数有关的相关操作。虽然集合中的元素是不同的,但是分数可以相同。
有序集合类型在某些方面跟列表相似:
但是两者还是有很大的区别的:
1 127.0.0.1:6379> zadd scoreboard 89 tom 67 peter 100 david 2 (integer) 3 3 127.0.0.1:6379> zrange scoreboard 0 -1 4 1) "peter" 5 2) "tom" 6 3) "david" 7 127.0.0.1:6379> zrange scoreboard 0 -1 withscores 8 1) "peter" 9 2) "67" 10 3) "tom" 11 4) "89" 12 5) "david" 13 6) "100" 14 127.0.0.1:6379> zadd scoreboard 76 peter 15 (integer) 0 16 127.0.0.1:6379> zrange scoreboard 0 -1 withscores 17 1) "peter" 18 2) "76" 19 3) "tom" 20 4) "89" 21 5) "david" 22 6) "100" 23 127.0.0.1:6379> zrevrange scoreboard 0 -1 withscores 24 1) "david" 25 2) "100" 26 3) "tom" 27 4) "89" 28 5) "peter" 29 6) "76" 30 127.0.0.1:6379> zscore scoreboard peter 31 "76" 32 127.0.0.1:6379> zadd scoreboard 76.00 peter 33 (integer) 0 34 127.0.0.1:6379> zrange scoreboard 0 -1 withscores 35 1) "peter" 36 2) "76" 37 3) "tom" 38 4) "89" 39 5) "david" 40 6) "100" 41 127.0.0.1:6379> zadd scoreboard 76.01 peter 42 (integer) 0 43 127.0.0.1:6379> zrange scoreboard 0 -1 withscores 44 1) "peter" 45 2) "76.010000000000005" 46 3) "tom" 47 4) "89" 48 5) "david" 49 6) "100" 50 127.0.0.1:6379> zadd scoreboard -inf peter 51 (integer) 0 52 127.0.0.1:6379> zrange scoreboard 0 -1 withscores 53 1) "peter" 54 2) "-inf" 55 3) "tom" 56 4) "89" 57 5) "david" 58 6) "100"
1 127.0.0.1:6379> zrange scoreboard 0 -1 withscores 2 1) "jerry" 3 2) "56" 4 3) "yvonne" 5 4) "67" 6 5) "peter" 7 6) "76" 8 7) "tom" 9 8) "89" 10 9) "wendy" 11 10) "92" 12 11) "david" 13 12) "100" 14 127.0.0.1:6379> zrangebyscore scoreboard 56 92 15 1) "jerry" 16 2) "yvonne" 17 3) "peter" 18 4) "tom" 19 5) "wendy" 20 127.0.0.1:6379> zrangebyscore scoreboard (56 92 21 1) "yvonne" 22 2) "peter" 23 3) "tom" 24 4) "wendy" 25 127.0.0.1:6379> zrangebyscore scoreboard 56 (92 26 1) "jerry" 27 2) "yvonne" 28 3) "peter" 29 4) "tom" 30 127.0.0.1:6379> zrangebyscore scoreboard 56 +inf 31 1) "jerry" 32 2) "yvonne" 33 3) "peter" 34 4) "tom" 35 5) "wendy" 36 6) "david" 37 127.0.0.1:6379> zrangebyscore scoreboard -inf 56 38 1) "jerry" 39 127.0.0.1:6379> zrangebyscore scoreboard -inf 67 withscores 40 1) "jerry" 41 2) "56" 42 3) "yvonne" 43 4) "67" 44 127.0.0.1:6379> zrangebyscore scoreboard (60 +inf limit 1 3 45 1) "peter" 46 2) "tom" 47 3) "wendy" 48 127.0.0.1:6379> zrangebyscore scoreboard (60 +inf limit 1 2 49 1) "peter" 50 2) "tom" 51 127.0.0.1:6379> zrangebyscore scoreboard (60 +inf limit 2 2 52 1) "tom" 53 2) "wendy" 54 127.0.0.1:6379> zrangebyscore scoreboard (60 +inf limit 0 2 55 1) "yvonne" 56 2) "peter" 57 127.0.0.1:6379> zrevrangebyscore scoreboard 100 60 withscores 58 1) "david" 59 2) "100" 60 3) "wendy" 61 4) "92" 62 5) "tom" 63 6) "89" 64 7) "peter" 65 8) "76" 66 9) "yvonne" 67 10) "67" 68 127.0.0.1:6379> zrevrangebyscore scoreboard 100 60 limit 2 2 69 1) "tom" 70 2) "peter"
1 127.0.0.1:6379> zscore scoreboard peter 2 "76" 3 127.0.0.1:6379> zincrby scoreboard 4 peter 4 "80" 5 127.0.0.1:6379> zscore scoreboard peter 6 "80" 7 127.0.0.1:6379> zincrby scoreboard -10 peter 8 "70" 9 127.0.0.1:6379> zscore scoreboard peter 10 "70"
1 127.0.0.1:6379> zcard scoreboard 2 (integer) 6 3 127.0.0.1:6379> zcount scoreboard -inf +inf 4 (integer) 6 5 127.0.0.1:6379> zcount scoreboard 60 +inf 6 (integer) 5 7 127.0.0.1:6379> zrange scoreboard 0 -1 withscores 8 1) "jerry" 9 2) "56" 10 3) "yvonne" 11 4) "67" 12 5) "peter" 13 6) "70" 14 7) "tom" 15 8) "89" 16 9) "wendy" 17 10) "92" 18 11) "david" 19 12) "100" 20 127.0.0.1:6379> zrangebyscore scoreboard -inf +inf withscores 21 1) "jerry" 22 2) "56" 23 3) "yvonne" 24 4) "67" 25 5) "peter" 26 6) "70" 27 7) "tom" 28 8) "89" 29 9) "wendy" 30 10) "92" 31 11) "david" 32 12) "100" 33 127.0.0.1:6379> zrem scoreboard jerry yvonne 34 (integer) 2 35 127.0.0.1:6379> zrangebyscore scoreboard -inf +inf withscores 36 1) "peter" 37 2) "70" 38 3) "tom" 39 4) "89" 40 5) "wendy" 41 6) "92" 42 7) "david" 43 8) "100" 44 127.0.0.1:6379> zcard scoreboard 45 (integer) 4 46 127.0.0.1:6379> zrank scoreboard peter 47 (integer) 0 48 127.0.0.1:6379> zrevrank scoreboard peter 49 (integer) 3 50 127.0.0.1:6379> zremrangebyrank scoreboard 0 1 51 (integer) 2 52 127.0.0.1:6379> zrangebyscore scoreboard -inf +inf withscores 53 1) "wendy" 54 2) "92" 55 3) "david" 56 4) "100" 57 127.0.0.1:6379> zremrangebyscore scoreboard 0 92 58 (integer) 1 59 127.0.0.1:6379> zrangebyscore scoreboard -inf +inf withscores 60 1) "david" 61 2) "100"
127.0.0.1:6379> zadd sortedset1 1 a 2 b (integer) 2 127.0.0.1:6379> zadd sortedset2 10 a 20 b (integer) 2 127.0.0.1:6379> zinterstore sortedsetsresult 2 sortedset1 sortedset2 (integer) 2 127.0.0.1:6379> zrange sortedsetsresult 0 -1 withscores 1) "a" 2) "11" 3) "b" 4) "22" 127.0.0.1:6379> zinterstore sortedsetsresult 2 sortedset1 sortedset2 aggregate min (integer) 2 127.0.0.1:6379> zrange sortedsetsresult 0 -1 withscores 1) "a" 2) "1" 3) "b" 4) "2" 127.0.0.1:6379> zinterstore sortedsetsresult 2 sortedset1 sortedset2 aggregate max (integer) 2 127.0.0.1:6379> zrange sortedsetsresult 0 -1 withscores 1) "a" 2) "10" 3) "b" 4) "20" 127.0.0.1:6379> zinterstore sortedsetsresult 2 sortedset1 sortedset2 aggregate max weights 1 0.1 (integer) 2 127.0.0.1:6379> zrange sortedsetsresult 0 -1 withscores 1) "a" 2) "1" 3) "b" 4) "2" 127.0.0.1:6379> zinterstore sortedsetsresult 2 sortedset1 sortedset2 aggregate max weights 2 0.5 (integer) 2 127.0.0.1:6379> zrange sortedsetsresult 0 -1 withscores 1) "a" 2) "5" 3) "b" 4) "10" 127.0.0.1:6379> zinterstore sortedsetsresult 2 sortedset1 sortedset2 aggregate sum weights 2 0.5 (integer) 2 127.0.0.1:6379> zrange sortedsetsresult 0 -1 withscores 1) "a" 2) "7" 3) "b" 4) "14" 127.0.0.1:6379> zadd sortedset 11 a 12 c (integer) 2 127.0.0.1:6379> zinterstore sortedsetsresult 2 sortedset1 sortedset aggregate sum weights 2 1 (integer) 1 127.0.0.1:6379> zrange sortedsetsresult 0 -1 withscores 1) "a" 2) "13" 127.0.0.1:6379> zunionstore sortedsetsresult 2 sortedset1 sortedset aggregate su(integer) 3 1 127.0.0.1:6379> zrange sortedsetsresult 0 -1 withscores 1) "b" 2) "4" 3) "c" 4) "12" 5) "a" 6) "13" 127.0.0.1:6379> zrevrange sortedsetsresult 0 -1 withscores 1) "a" 2) "13" 3) "c" 4) "12" 5) "b" 6) "4"
本节主要介绍了Redis的5中数据类型,并给出具体的例子。通过学习发现,Redis的数据类型非常丰富,其中有序集合sortedset是比较高级灵活实用的数据类型,当然它需要更多的内存空间。
标签:des style blog http io ar color os 使用
原文地址:http://www.cnblogs.com/rcfeng/p/4136323.html