03_(B)Redis数据类型
程序员文章站
2022-05-09 17:06:20
...
千呼万唤始出来,呵呵(没有犹抱琵琶半遮面)。Redis数据类型第二弹来了!
这次主要是讲解set,sorted set,hash
ps:希望大家习惯我的博客风格。我主要是通过操作Redis的常用命令来讲解这个数据库。截图我觉得能更好的说明效果。但是有些网速可能不给力,所以这次除了截图,我也会吧代码直接贴出来
老套路,我们先启动Redis服务器
命令在复习一次:redis-server redis.conf
开启客户端:redis-cli
输入密码:AUTH ****
还是先简单的复习一下上次说的三种数据结构。
第一种:key(略)太简单了
第二种:String
存取一对键值对:
redis 127.0.0.1:6379> set s_k01 aaaa OK redis 127.0.0.1:6379> get s_k01 "aaaa" redis 127.0.0.1:6379>
第三种:List。list是有序的可以重复的集合
存取一个list
redis 127.0.0.1:6379> select 1 OK redis 127.0.0.1:6379[1]> DBSIZE (integer) 0 redis 127.0.0.1:6379[1]> lpush l_k01 a b c d a b c d (integer) 8 redis 127.0.0.1:6379[1]> rpush l_k01 haha (integer) 9 redis 127.0.0.1:6379[1]> lrange l_k01 0 -1 1) "d" 2) "c" 3) "b" 4) "a" 5) "d" 6) "c" 7) "b" 8) "a" 9) "haha"
好了,前面三种数据结构就到这。多写几次就熟悉了。
下面开始讲解剩下的三种类型。尤其注意set
第一种:set。set是一种无序的,不能重复的集合
redis 127.0.0.1:6379[1]> sadd sk01 a b c a (integer) 3 redis 127.0.0.1:6379[1]> smemhers sk01 (error) ERR unknown command 'smemhers' redis 127.0.0.1:6379[1]> SMEMBERS sk01 1) "c" 2) "a" 3) "b" redis 127.0.0.1:6379[1]> srem sk01 a (integer) 1 redis 127.0.0.1:6379[1]> smembers sk01 1) "c" 2) "b" redis 127.0.0.1:6379[1]> spop sk01 "b" redis 127.0.0.1:6379[1]> SMEMBERS sk01 1) "c" redis 127.0.0.1:6379[1]> SRANDMEMBER sk01 "c" redis 127.0.0.1:6379[1]> SMEMBERS sk01 1) "c" redis 127.0.0.1:6379[1]> sadd sk01 hah hive pig hadoop (integer) 4 redis 127.0.0.1:6379[1]> SMEMBERS sk01 1) "c" 2) "pig" 3) "hah" 4) "hive" 5) "hadoop" redis 127.0.0.1:6379[1]> smove sk01 sk02 hive (integer) 1 redis 127.0.0.1:6379[1]> SMEMBERS sk01 1) "pig" 2) "hah" 3) "c" 4) "hadoop" redis 127.0.0.1:6379[1]> SMEMBERS sk02 1) "hive" redis 127.0.0.1:6379[1]>
redis 127.0.0.1:6379[1]> SMEMBERS sk01 1) "pig" 2) "hah" 3) "c" 4) "hadoop" redis 127.0.0.1:6379[1]> scard sk01 (integer) 4 redis 127.0.0.1:6379[1]> sismember sk01 hadoop (integer) 1 redis 127.0.0.1:6379[1]> sismember sk01 hadoop1 (integer) 0 redis 127.0.0.1:6379[1]> smembers sk02 1) "hive" redis 127.0.0.1:6379[1]> sadd sk02 hadoop c (integer) 2 redis 127.0.0.1:6379[1]> smembers sk01 1) "hah" 2) "pig" 3) "c" 4) "hadoop" redis 127.0.0.1:6379[1]> smembers sk02 1) "c" 2) "hadoop" 3) "hive" redis 127.0.0.1:6379[1]> sinter sk01 sk02 1) "c" 2) "hadoop" redis 127.0.0.1:6379[1]> sunion sk01 sk01 1) "hah" 2) "pig" 3) "c" 4) "hadoop" redis 127.0.0.1:6379[1]> sdiff sk01 sk02 1) "pig" 2) "hah" redis 127.0.0.1:6379[1]>
关于set的描述就到此请注意最后三个集合的操作:差集,交集,并集这三种比较常用。如果可以的话,我会举个例子。看看Redis是怎么实现关系型数据中的联合查询
第二种:SortedSet这是一种带有排序的set集合,适用性不是很多。简单介绍一下常用的操作
redis 127.0.0.1:6379> Flushdb OK redis 127.0.0.1:6379> DBSIZE (integer) 0 redis 127.0.0.1:6379> zadd ssk01 1 a 2 b 3 c (integer) 3 redis 127.0.0.1:6379> zadd ssk01 4 a (integer) 0 redis 127.0.0.1:6379> zadd ssk01 1 a (integer) 0 redis 127.0.0.1:6379> zrem ssk01 a (integer) 1 redis 127.0.0.1:6379> zrank ssk01 b (integer) 0 redis 127.0.0.1:6379> zrank ssk01 c (integer) 1 redis 127.0.0.1:6379> zadd ssk01 1 a (integer) 1 redis 127.0.0.1:6379> zrank ssk01 a (integer) 0 redis 127.0.0.1:6379> zrank ssk01 c (integer) 2 redis 127.0.0.1:6379> ZREVRANGE ssk01 b (error) ERR wrong number of arguments for 'zrevrange' command redis 127.0.0.1:6379> zcount ssk01 0 -1 (integer) 0 redis 127.0.0.1:6379> zcount ssk01 0 3 (integer) 3 redis 127.0.0.1:6379> zrangebyscore ssk01 0 3 1) "a" 2) "b" 3) "c" redis 127.0.0.1:6379>
第三种数据类型:map这种键值对的结构在实际应用中也是比较多的
//存放一个键值对 redis 127.0.0.1:6379> hset mk01 a A (integer) 1 redis 127.0.0.1:6379> hset mk01 b B (integer) 1 //存放多对键值对 redis 127.0.0.1:6379> hmset mk01 c C d D OK //获得所有的键值对 redis 127.0.0.1:6379> HGETALL mk01 1) "a" 2) "A" 3) "b" 4) "B" 5) "c" 6) "C" 7) "d" 8) "D" //获得mk01中键为a对应的值 redis 127.0.0.1:6379> hget mk01 a "A" //取出多个键值对 redis 127.0.0.1:6379> hmget mk01 a b d 1) "A" 2) "B" 3) "D" //判断a是否存在 redis 127.0.0.1:6379> HEXISTS mk01 a (integer) 1 //删除键为a的数据 redis 127.0.0.1:6379> hdel mk01 a (integer) 1 redis 127.0.0.1:6379> HEXISTS mk01 a (integer) 0 //返回map的长度 redis 127.0.0.1:6379> hlen mk01 (integer) 3 redis 127.0.0.1:6379> hkey mk01 (error) ERR unknown command 'hkey' //获得所有的key redis 127.0.0.1:6379> hkeys mk01 1) "b" 2) "c" 3) "d" //获得所有的value值 redis 127.0.0.1:6379> hvals mk01 1) "B" 2) "C" 3) "D"
以上就是Redis的所有数据类型。是不是很简单!注意多敲几遍,熟悉了才能灵活运用
上一篇: 基于JQuery实现图片轮播效果(焦点图)_jquery
下一篇: 征服 Redis