redis学习之路之初识redis

Redis 的数据结构简介

1、redis 中的字符串

 

 

 

127.0.0.1:6379> set hello1 world
OK
127.0.0.1:6379> get hello1
"world"
127.0.0.1:6379> del hello1
(integer) 1
127.0.0.1:6379> get hello1
(nil)
127.0.0.1:6379> type hello # 查看当前key的类型
myRedis:0>set views 0 # 设置浏览量

"OK"
myRedis:0>get views 
"0"
myRedis:0>incr views  # 浏览量自增1
"1"
myRedis:0>INCRBY views  1 # 设置浏览量自增10
 "11"
myRedis:0>

 

2、redis中的列表

 

 

 

 

 

 

 1 127.0.0.1:6379> rpush list-key item
 2 (integer) 1
 3 127.0.0.1:6379> rpush list-key item1
 4 (integer) 2
 5 127.0.0.1:6379> rpush list-key item2
 6 (integer) 3
 7 127.0.0.1:6379> rpush list-key item
 8 (integer) 4
 9 127.0.0.1:6379> get list-key
10 (error) WRONGTYPE Operation against a key holding the wrong kind of value
11 127.0.0.1:6379> lrange list-key
12 (error) ERR wrong number of arguments for 'lrange' command
13 127.0.0.1:6379> lrange list-key 0-1
14 (error) ERR wrong number of arguments for 'lrange' command
15 127.0.0.1:6379> lrange list-key 0 - 1
16 (error) ERR wrong number of arguments for 'lrange' command
17 127.0.0.1:6379> lrange list-key 0 -1
18 1) "item"
19 2) "item1"
20 3) "item2"
21 4) "item"
22 127.0.0.1:6379> lindex list-key 1
23 "item1"
24 127.0.0.1:6379>
25 127.0.0.1:6379> lpop list-key
26 "item"
27 127.0.0.1:6379> lrange list-key 0 -1
28 1) "item1"
29 2) "item2"
30 3) "item"

3、redis的集合(set)

注意:redis存储的集合是无序的

 

 

 

 

 

 

 1 127.0.0.1:6379> sadd set-key item
 2 (integer) 1
 3 127.0.0.1:6379> sadd set-key item1
 4 (integer) 1
 5 127.0.0.1:6379> sadd set-key item2
 6 (integer) 1
 7 127.0.0.1:6379> sadd set-key item3
 8 (integer) 1
 9 127.0.0.1:6379> sadd set-key item
10 //1 表示元素已经添加到集合中
11 //0 表示元素已经存在在集合中
1 127.0.0.1:6379> smembers set-key
2 1) "item"
3 2) "item3"
4 3) "item1"
5 4) "item2"
6 //获取集合包含的所有元素
1 127.0.0.1:6379> sismember set-key item4
2 (integer) 0
3 127.0.0.1:6379> sismember set-key item
4 (integer) 1
5 //0 表示不存在
6 //1 表示存在
1 127.0.0.1:6379> srem set-key item3
2 (integer) 1
3 127.0.0.1:6379> srem set-key item3
4 (integer) 0
5 
6 //使用srem 命令会返回被移除元素的数量
1 127.0.0.1:6379> smembers set-key
2 1) "item2"
3 2) "item"
4 3) "item1"

4、redis 的散列

 

 

 

 1 127.0.0.1:6379> hset hash-key sub-key1  value1
 2 (integer) 1
 3 127.0.0.1:6379> hset hash-key sub-key2  value2
 4 (integer) 1
 5 127.0.0.1:6379> hset hash-key sub-key1  value2
 6 (integer) 0
 7 127.0.0.1:6379> hgetall hash-key
 8 1) "sub-key1"
 9 2) "value2"
10 3) "sub-key2"
11 4) "value2"
12 127.0.0.1:6379> hdel hash-key sub-key2
13 (integer) 1
14 127.0.0.1:6379> hdel hash-key sub-key2
15 (integer) 0
16 127.0.0.1:6379> hget hash-key sub-key1
17 "value2"
18 127.0.0.1:6379> hgetall hash-key
19 1) "sub-key1"
20 2) "value2"
21 127.0.0.1:6379>
22 //注意:key是唯一的,重复set 会覆盖之前的值

 

5、redis的有序集合

 

 

 

 1 127.0.0.1:6379> zadd zset-key 728 membe1
 2 (integer) 1
 3 127.0.0.1:6379> zadd zset-key 899 member0
 4 (integer) 1
 5 127.0.0.1:6379> zadd zset-key 899 member0
 6 (integer) 0
 7 127.0.0.1:6379> zrange zset-key 0 -1e withsorces
 8 (error) ERR value is not an integer or out of range
 9 127.0.0.1:6379> zrange zset-key 0 -1e withscores
10 (error) ERR value is not an integer or out of range
11 127.0.0.1:6379> zrange zset-key 0 -1 withscores
12 1) "membe1"
13 2) "728"
14 3) "member0"
15 4) "899"
16 127.0.0.1:6379> zrange zset-key 0 800 withscores
17 1) "membe1"
18 2) "728"
19 3) "member0"
20 4) "899"
21 127.0.0.1:6379> zrangebyscore zset-key 0 800 withscores
22 1) "membe1"
23 2) "728"
24 127.0.0.1:6379> zrem zset-key member0
25 (integer) 1
26 127.0.0.1:6379> zrem zset-key member0
27 (integer) 0
28 127.0.0.1:6379> zrange zset-key 0 -1 withscores
29 1) "membe1"
30 2) "728"

 

posted @ 2020-08-31 17:48  keepsummer  阅读(164)  评论(0编辑  收藏  举报