初识Redis
结构类型 |
结构存储的值 |
结构的读写能力 |
STRING |
可以是字符串、整数或者浮点数 |
对整个字符串或者字符串的其中一部分执行操作;对整数和浮点数执行自增(increment)或者自减(decrement) |
LIST |
一个链表,链表上的每个节点都包含了一个字符串 |
从链表的两端推入或者弹出元素;根据偏移量对链表进行修剪(trim);读取单个或者多个元素;根据值查找或移除元素 |
SET |
包含字符串的无序收集器(unordered collection),并且被包含的每个字符串都是独一无二、各不相同的 |
添加、获取、移除单个元素;检查一个元素是否存在于集合中;计算交集、并集、差集;从集合里面随机获取元素 |
HASH |
包含键值对的无序散列列表 |
添加、获取、移除单个键值对;获取所有键值对 |
ZSET |
字符串成员(member)与浮点数分值(score)之间的有序映射,元素的排列顺序由分值的大小决定 |
添加、获取、删除单个元素;根据分值范围(range)或者成员来获取元素 |
127.0.0.1:6379> set hello world OK 127.0.0.1:6379> set youth hello OK 127.0.0.1:6379> get youth "hello" 127.0.0.1:6379> del youth (integer) 1 127.0.0.1:6379> get youth (nil) 127.0.0.1:6379>
127.0.0.1:6379> rpush list-key item (integer) 1 127.0.0.1:6379> rpush list-key item2 (integer) 2 127.0.0.1:6379> rpush list-key item (integer) 3 127.0.0.1:6379> lrange list-key 0 -1 1) "item" 2) "item2" 3) "item" 127.0.0.1:6379> lindex list-key 1 "item2" 127.0.0.1:6379> lpop list-key "item" 127.0.0.1:6379> lrange list-key 0 -1 1) "item2" 2) "item" 127.0.0.1:6379>
127.0.0.1:6379> sadd set-key item1 (integer) 1 127.0.0.1:6379> sadd set-key item2 (integer) 1 127.0.0.1:6379> sadd set-key item3 (integer) 1 127.0.0.1:6379> sadd set-key item1 (integer) 0 127.0.0.1:6379> smembers set-key 1) "item2" 2) "item3" 3) "item1" 127.0.0.1:6379> sismember set-key item4 (integer) 0 127.0.0.1:6379> sismember set-key item1 (integer) 1 127.0.0.1:6379> srem set-key item3 (integer) 1 127.0.0.1:6379> smembers set-key 1) "item2" 2) "item1" 127.0.0.1:6379>
127.0.0.1:6379> hset hash-key b-key1 value1 (integer) 1 127.0.0.1:6379> hset hash-key b-key2 value2 (integer) 1 127.0.0.1:6379> hset hash-key b-key1 value1 (integer) 0 127.0.0.1:6379> hgetall hash-key 1) "b-key1" 2) "value1" 3) "b-key2" 4) "value2" 127.0.0.1:6379> hdel hash-key b-key2 (integer) 1 127.0.0.1:6379> hdel hash-key b-key2 (integer) 0 127.0.0.1:6379> hget hash-key b-key1 "value1" 127.0.0.1:6379> hget hash-key b-key2 (nil) 127.0.0.1:6379> hgetall hash-key 1) "b-key1" 2) "value1" 127.0.0.1:6379>
127.0.0.1:6379> zadd zset-key 5 student1 (integer) 1 127.0.0.1:6379> zadd zset-key 1 student2 (integer) 1 127.0.0.1:6379> zadd zset-key 3 student3 (integer) 1 127.0.0.1:6379> zadd zset-key 9 student4 (integer) 1 127.0.0.1:6379> zadd zset-key 9 student4 (integer) 0 127.0.0.1:6379> zrange zset-key 0 -1 withscores 1) "student2" 2) "1" 3) "student3" 4) "3" 5) "student1" 6) "5" 7) "student4" 8) "9" 127.0.0.1:6379> zrangebyscore zset-key 0 5 withscores 1) "student2" 2) "1" 3) "student3" 4) "3" 5) "student1" 6) "5" 127.0.0.1:6379> zrem zset-key student3 (integer) 1 127.0.0.1:6379> zrem zset-key student3 (integer) 0 127.0.0.1:6379> zrange zset-key 0 -1 withscores 1) "student2" 2) "1" 3) "student1" 4) "5" 5) "student4" 6) "9" 127.0.0.1:6379>