Redis五种数据类型
127.0.0.1String:key-value(做缓存)
Redis中所有的数据都是字符串。命令不区分大小写,key是区分大小写的。Redis是单线程的。Redis中不适合保存内容大的数据。
get、set、
incr:加一(生成id)
Decr:减一
Hash:key-fields-values(做缓存)
相当于一个key对于一个map,map中还有key-value
使用hash对key进行归类。
Hset:向hash中添加内容
Hget:从hash中取内容
List:有顺序可重复
127.0.0.1:6379> lpush list1 a b c d
(integer) 4
127.0.0.1:6379> lrange list1 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379> rpush list1 1 2 3 4
(integer) 8
127.0.0.1:6379> lrange list1 0 -1
1) "d"
2) "c"
3) "b"
4) "a"
5) "1"
6) "2"
7) "3"
8) "4"
127.0.0.1:6379>
127.0.0.1:6379> lpop list1
"d"
127.0.0.1:6379> lrange list1 0 -1
1) "c"
2) "b"
3) "a"
4) "1"
5) "2"
6) "3"
7) "4"
127.0.0.1:6379> rpop list1
"4"
127.0.0.1:6379> lrange list1 0 -1
1) "c"
2) "b"
3) "a"
4) "1"
5) "2"
6) "3"
127.0.0.1:6379>
Set:元素无顺序,不能重复
127.0.0.1:6379> sadd set1 a b c c c d
(integer) 4
127.0.0.1:6379> smembers set1
1) "b"
2) "c"
3) "d"
4) "a"
127.0.0.1:6379> srem set1 a
(integer) 1
127.0.0.1:6379> smembers set1
1) "b"
2) "c"
3) "d"
127.0.0.1:6379>
还有集合运算命令。
SortedSet(zset):有顺序,不能重复
127.0.0.1:6379> zadd zset1 2 a 5 b 1 c 6 d
(integer) 4
127.0.0.1:6379> zrange zset1 0 -1
1) "c"
2) "a"
3) "b"
4) "d"
127.0.0.1:6379> zrem zset1 a
(integer) 1
127.0.0.1:6379> zrange zset1 0 -1
1) "c"
2) "b"
3) "d"
127.0.0.1:6379> zrevrange zset1 0 -1
1) "d"
2) "b"
3) "c"
127.0.0.1:6379> zrange zset1 0 -1 withscores
1) "c"
2) "1"
3) "b"
4) "5"
5) "d"
6) "6"
127.0.0.1:6379> zrevrange zset1 0 -1 withscores
1) "d"
2) "6"
3) "b"
4) "5"
5) "c
6) "1"