概述 Redis是什么? Redis(Remote Dictionary Server ),即远程字典服务 ! 是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。 免费和开源!是当下最热门的 NoSQL 技术之一!也被人们称之为结构化数据库!
Redis能干嘛? 1、内存存储、持久化,内存中是断电即失、所以说持久化很重要(rdb、aof) 2、效率高,可以用于高速缓存 3、发布订阅系统 4、地图信息分析 5、计时器、计数器(浏览量!) 6、…
Redis的基本了解 首先我们可以看下官方文档是如何介绍Redis的: ①、英文文档 点击跳转 .
②、中文文档 点击跳转 .
Redis-Key 简单介绍一下Redis中对Key的操作命令。希望大家可以跟着注释敲一遍,简单记一下,都是最常用的命令! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 127.0.0.1:6379> ping PONG 127.0.0.1:6379> clear 127.0.0.1:6379> keys * 1) "db" 127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> set name dingdada OK 127.0.0.1:6379> get name "dingdada" 127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> set name1 dingdada2 OK 127.0.0.1:6379> get name1 "dingdada2" 127.0.0.1:6379> keys * 1) "name1" 2) "name" 127.0.0.1:6379> EXISTS name (integer ) 1 127.0.0.1:6379> move name 1 (integer ) 1 127.0.0.1:6379> keys * 1) "name1" 127.0.0.1:6379> FLUSHALL OK 127.0.0.1:6379> set name dingdada OK 127.0.0.1:6379> set name1 dingdada1 OK 127.0.0.1:6379> set name2 dingdada2 OK 127.0.0.1:6379> EXPIRE name 15 (integer ) 1 127.0.0.1:6379> ttl name (integer ) 13 127.0.0.1:6379> ttl name (integer ) 12 127.0.0.1:6379> ttl name (integer ) 11 127.0.0.1:6379> ttl name (integer ) 8 127.0.0.1:6379> ttl name (integer ) 6 127.0.0.1:6379> ttl name (integer ) 3 127.0.0.1:6379> ttl name (integer ) 2 127.0.0.1:6379> ttl name (integer ) 1 127.0.0.1:6379> ttl name (integer ) 0 127.0.0.1:6379> ttl name (integer ) -2 127.0.0.1:6379> get name (nil) 127.0.0.1:6379> type name1 string 127.0.0.1:6379>
Redis的五大数据类型 String(字符串) ①添加 、查询 、追加 、获取长度 ,判断是否存在 的操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 127.0.0.1:6379> set name dingdada OK 127.0.0.1:6379> get name "dingdada" 127.0.0.1:6379> get key1 "hello world!" 127.0.0.1:6379> keys * 1) "name" 127.0.0.1:6379> EXISTS name (integer ) 1 127.0.0.1:6379> EXISTS name1 (integer ) 0 127.0.0.1:6379> APPEND name1 dingdada1 (integer ) 9 127.0.0.1:6379> get name1 "dingdada1" 127.0.0.1:6379> STRLEN name1 (integer ) 9 127.0.0.1:6379> APPEND name1 ,dingdada2 (integer ) 19 127.0.0.1:6379> STRLEN name1 (integer ) 19 127.0.0.1:6379> get name1 "dingdada1,dingdada2" 127.0.0.1:6379> set key1 "hello world!" OK 127.0.0.1:6379> set key1 hello world! (error) ERR syntax error 127.0.0.1:6379> set key1 hello,world! OK
②自增 、自减 操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 127.0.0.1:6379> set num 0 OK 127.0.0.1:6379> get num "0" 127.0.0.1:6379> incr num (integer ) 1 127.0.0.1:6379> get num "1" 127.0.0.1:6379> incr num (integer ) 2 127.0.0.1:6379> incr num (integer ) 3 127.0.0.1:6379> get num "3" 127.0.0.1:6379> decr num (integer ) 2 127.0.0.1:6379> decr num (integer ) 1 127.0.0.1:6379> decr num (integer ) 0 127.0.0.1:6379> decr num (integer ) -1 127.0.0.1:6379> decr num (integer ) -2 127.0.0.1:6379> decr num (integer ) -3 127.0.0.1:6379> INCRBY num 10 (integer ) 7 127.0.0.1:6379> INCRBY num 10 (integer ) 17 127.0.0.1:6379> DECRBY num 3 (integer ) 14 127.0.0.1:6379> DECRBY num 3 (integer ) 11
③截取 、替换 字符串操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 127.0.0.1:6379> set key1 "hello world!" OK 127.0.0.1:6379> get key1 "hello world!" 127.0.0.1:6379> GETRANGE key1 0 4 "hello" 127.0.0.1:6379> get key1 "hello world!" 127.0.0.1:6379> GETRANGE key1 0 -1 "hello world!" 127.0.0.1:6379> set key2 "hello,,,world!" OK 127.0.0.1:6379> get key2 "hello,,,world!" 127.0.0.1:6379> SETRANGE key2 5 888 (integer ) 14 127.0.0.1:6379> get key2 "hello888world!" 127.0.0.1:6379> SETRANGE key2 5 67 (integer ) 14 127.0.0.1:6379> get key2 "hello678world!"
④设置过期时间 、不存在设置 操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 127.0.0.1:6379> setex name1 15 dingdada OK 127.0.0.1:6379> ttl name1 (integer ) 6 127.0.0.1:6379> ttl name1 (integer ) 5 127.0.0.1:6379> ttl name1 (integer ) 3 127.0.0.1:6379> ttl name1 (integer ) 1 127.0.0.1:6379> ttl name1 (integer ) 0 127.0.0.1:6379> ttl name1 (integer ) -2 127.0.0.1:6379> setnx name2 dingdada2 (integer ) 1 127.0.0.1:6379> get name2 "dingdada2" 127.0.0.1:6379> keys * 1) "name2" 127.0.0.1:6379> setnx name2 "dingdada3" (integer ) 0 127.0.0.1:6379> get name2 "dingdada2"
⑤mset 、mget 操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 127.0.0.1:6379> mset k1 v1 k2 v2 k3 v3 OK 127.0.0.1:6379> keys * 1) "k2" 2) "k3" 3) "k1" 127.0.0.1:6379> mget k1 k2 k3 1) "v1" 2) "v2" 3) "v3" 127.0.0.1:6379> MSETNX k1 v1 k4 v4 (integer ) 0 127.0.0.1:6379> keys * 1) "k2" 2) "k3" 3) "k1" 127.0.0.1:6379> MSETNX k5 v5 k4 v4 (integer ) 1 127.0.0.1:6379> keys * 1) "k2" 2) "k4" 3) "k3" 4) "k5" 5) "k1"
⑥添加获取对象 、getset 操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 127.0.0.1:6379> mset student:1:name dingdada student:1:age 22 OK 127.0.0.1:6379> keys * 1) "student:1:age" 2) "student:1:name" 127.0.0.1:6379> mget student:1:age student:1:name 1) "22" 2) "dingdada" 127.0.0.1:6379> getset name1 dingdada1 (nil) 127.0.0.1:6379> get name1 "dingdada1" 127.0.0.1:6379> getset name1 dingdada2 "dingdada1" 127.0.0.1:6379> get name1 "dingdada2"
⑦总结 String是Redis中最常用的一种数据类型,也是Redis中最简单的一种数据类型。首先,表面上它是字符串,但其实他可以灵活的表示字符串、整数、浮点数3种值。Redis会自动的识别这3种值。
List (列表)①lpush(左插入)、lrange(查询集合)、rpush(右插入)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 127.0.0.1:6379> lpush list v1 (integer ) 1 127.0.0.1:6379> lpush list v2 (integer ) 2 127.0.0.1:6379> lpush list v3 (integer ) 3 127.0.0.1:6379> LRANGE list 0 -1 1) "v3" 2) "v2" 3) "v1" 127.0.0.1:6379> lpush list1 v1 v2 v3 v4 v5 (integer ) 5 127.0.0.1:6379> LRANGE list1 0 -1 1) "v5" 2) "v4" 3) "v3" 4) "v2" 5) "v1" 127.0.0.1:6379> LRANGE list 0 1 1) "v3" 2) "v2" 127.0.0.1:6379> LRANGE list 0 0 1) "v3" 127.0.0.1:6379> rpush list rv0 (integer ) 4 127.0.0.1:6379> lrange list 0 -1 1) "v3" 2) "v2" 3) "v1" 4) "rv0" 每次都使用lpush,老的数据永远在后面,我们每次获取 0 0 位置的元素,是不是相当于更新了 数据操作,但是数据记录还在?想要查询记录即可获取集合所有元素!,
②lpop(左移除)、rpop(右移除)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 127.0.0.1:6379> LRANGE list 0 -1 1) "v5" 2) "v4" 3) "v3" 4) "v2" 5) "v1" 127.0.0.1:6379> lpop list "v5" 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v2" 4) "v1" 127.0.0.1:6379> rpop list "v1" 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v2"
③lindex(查询指定下标元素)、llen(获取集合长度) 操作
1 2 3 4 5 6 7 8 9 10 11 12 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v2" 127.0.0.1:6379> lindex list 1 "v3" 127.0.0.1:6379> lindex list 0 "v4" 127.0.0.1:6379> llen list (integer ) 3
④lrem(根据value移除指定的值)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v2" 127.0.0.1:6379> lrem list 1 v2 (integer ) 1 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 127.0.0.1:6379> lrem list 0 v3 (integer ) 1 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 127.0.0.1:6379> lpush list v3 v2 v2 v2 (integer ) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "v2" 2) "v2" 3) "v2" 4) "v3" 5) "v4" 127.0.0.1:6379> lrem list 3 v2 (integer ) 3 127.0.0.1:6379> LRANGE list 0 -1 1) "v3" 2) "v4"
⑤ltrim(截取元素)、rpoplpush(移除指定集合中最后一个元素到一个新的集合中)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 127.0.0.1:6379> lpush list v1 v2 v3 v4 (integer ) 4 127.0.0.1:6379> LRANGE list 0 -1 1) "v4" 2) "v3" 3) "v2" 4) "v1" 127.0.0.1:6379> ltrim list 1 2 OK 127.0.0.1:6379> LRANGE list 0 -1 1) "v3" 2) "v2" 127.0.0.1:6379> lpush list v1 v2 v3 v4 v5 (integer ) 5 127.0.0.1:6379> LRANGE list 0 -1 1) "v5" 2) "v4" 3) "v3" 4) "v2" 5) "v1" 127.0.0.1:6379> rpoplpush list newlist "v1" 127.0.0.1:6379> LRANGE list 0 -1 1) "v5" 2) "v4" 3) "v3" 4) "v2" 127.0.0.1:6379> LRANGE newlist 0 -1 1) "v1"
⑥lset(更新)、linsert操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 127.0.0.1:6379> LRANGE list 0 -1 1) "v5" 2) "v4" 3) "v3" 4) "v2" 127.0.0.1:6379> 127.0.0.1:6379> lset list 1 newV5 OK 127.0.0.1:6379> LRANGE list 0 -1 1) "v5" 2) "newV5" 3) "v3" 4) "v2" 127.0.0.1:6379> lset list1 0 vvvv (error) ERR no such key 127.0.0.1:6379> lset list 8 vvv (error) ERR index out of range 127.0.0.1:6379> LRANGE list 0 -1 1) "v5" 2) "newV5" 3) "v3" 4) "v2" 127.0.0.1:6379> LINSERT list after v3 insertv3 (integer ) 5 127.0.0.1:6379> LRANGE list 0 -1 1) "v5" 2) "newV5" 3) "v3" 4) "insertv3" 5) "v2" 127.0.0.1:6379> LINSERT list before v3 insertv3 (integer ) 6 127.0.0.1:6379> LRANGE list 0 -1 1) "v5" 2) "newV5" 3) "insertv3" 4) "v3" 5) "insertv3" 6) "v2"
⑦小结:
实际上是一个链表,before Node after , left,right 都可以插入值 如果key 不存在,创建新的链表 如果key存在,新增内容 如果移除了所有值,空链表,也代表不存在! 在两边插入或者改动值,效率最高! 中间元素,相对来说效率会低一点~ 消息排队!消息队列 (Lpush Rpop), 栈( Lpush Lpop)! Set (集合)元素唯一不重复①sadd(添加)、smembers(查看所有元素)、sismember(判断是否存在)、scard(查看长度)、srem(移除指定元素)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 127.0.0.1:6379> sadd set1 ding da mian tiao (integer ) 4 127.0.0.1:6379> SMEMBERS set1 1) "mian" 2) "da" 3) "tiao" 4) "ding" 127.0.0.1:6379> SISMEMBER set1 da (integer ) 1 127.0.0.1:6379> SISMEMBER set1 da1 (integer ) 0 127.0.0.1:6379> SCARD set1 (integer ) 4 127.0.0.1:6379> srem set1 da (integer ) 1 127.0.0.1:6379> SMEMBERS set1 1) "mian" 2) "tiao" 3) "ding"
②srandmember(抽随机)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 127.0.0.1:6379> sadd myset 1 2 3 4 5 6 7 (integer ) 7 127.0.0.1:6379> SMEMBERS myset 1) "1" 2) "2" 3) "3" 4) "4" 5) "5" 6) "6" 7) "7" 127.0.0.1:6379> SRANDMEMBER myset 1 1) "4" 127.0.0.1:6379> SRANDMEMBER myset 1 1) "1" 127.0.0.1:6379> SRANDMEMBER myset 1 1) "5" 127.0.0.1:6379> SRANDMEMBER myset "3" 127.0.0.1:6379> SRANDMEMBER myset 3 1) "1" 2) "2" 3) "3" 127.0.0.1:6379> SRANDMEMBER myset 3 1) "6" 2) "3" 3) "5"
③spop(随机删除元素)、smove(移动指定元素到新的集合中)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 127.0.0.1:6379> SMEMBERS myset 1) "1" 2) "2" 3) "3" 4) "4" 5) "5" 6) "6" 7) "7" 127.0.0.1:6379> spop myset "2" 127.0.0.1:6379> spop myset 1 1) "7" 127.0.0.1:6379> spop myset 2 1) "3" 2) "5" 127.0.0.1:6379> SMEMBERS myset 1) "1" 2) "4" 3) "6" 127.0.0.1:6379> smove myset myset2 1 (integer ) 1 127.0.0.1:6379> SMEMBERS myset 1) "4" 2) "6" 127.0.0.1:6379> SMEMBERS myset2 1) "1"
④sdiff(差集)、sinter(交集)、sunion(并集)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 127.0.0.1:6379> sadd myset1 1 2 3 4 5 (integer ) 5 127.0.0.1:6379> sadd myset2 3 4 5 6 7 (integer ) 5 127.0.0.1:6379> SMEMBERS myset1 1) "1" 2) "2" 3) "3" 4) "4" 5) "5" 127.0.0.1:6379> SMEMBERS myset2 1) "3" 2) "4" 3) "5" 4) "6" 5) "7" 127.0.0.1:6379> SDIFF myset1 myset2 1) "1" 2) "2" 127.0.0.1:6379> SINTER myset1 myset2 1) "3" 2) "4" 3) "5" 127.0.0.1:6379> sunion myset1 myset2 1) "1" 2) "2" 3) "3" 4) "4" 5) "5" 6) "6" 7) "7"
⑤总结:可实现共同好友、共同关注等需求。
Hash (哈希)①hset(添加hash)、hget(查询)、hgetall(查询所有)、hdel(删除hash中指定的值)、hlen(获取hash的长度)、hexists(判断key是否存在)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 127.0.0.1:6379> hset myhash name dingdada age 23 (integer ) 2 127.0.0.1:6379> hget myhash name "dingdada" 127.0.0.1:6379> hget myhash age "23" 127.0.0.1:6379> hgetall myhash 1) "name" 2) "dingdada" 3) "age" 4) "23" 127.0.0.1:6379> hset myhash del test (integer ) 1 127.0.0.1:6379> hgetall myhash 1) "name" 2) "dingdada" 3) "age" 4) "23" 5) "del" 6) "test" 127.0.0.1:6379> hdel myhash del age (integer ) 2 127.0.0.1:6379> hgetall myhash 1) "name" 2) "dingdada" 127.0.0.1:6379> hlen myhash (integer ) 1 127.0.0.1:6379> HEXISTS myhash name (integer ) 1 127.0.0.1:6379> HEXISTS myhash age (integer ) 0
②hkeys(获取所有key)、hvals(获取所有value)、hincrby(给值加增量)、hsetnx(存在不添加)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 127.0.0.1:6379> hset myhash age 23 high 173 (integer ) 2 127.0.0.1:6379> hgetall myhash 1) "name" 2) "dingdada" 3) "age" 4) "23" 5) "high" 6) "173" 127.0.0.1:6379> hkeys myhash 1) "name" 2) "age" 3) "high" 127.0.0.1:6379> hvals myhash 1) "dingdada" 2) "23" 3) "173" 127.0.0.1:6379> hincrby myhash age 2 (integer ) 25 127.0.0.1:6379> hincrby myhash age -1 (integer ) 24 127.0.0.1:6379> hsetnx myhash nokey novalue (integer ) 1 127.0.0.1:6379> hsetnx myhash name miaotiao (integer ) 0 127.0.0.1:6379> hgetall myhash 1) "name" 2) "dingdada" 3) "age" 4) "24" 5) "high" 6) "173" 7) "nokey" 8) "novalue"
③总结:比String更加适合存对象~
zSet (有序集合)①zadd(添加)、zrange(查询)、zrangebyscore(排序小-大)、zrevrange(排序大-小)、zrangebyscore withscores(查询所有值包含key)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three (integer ) 3 127.0.0.1:6379> ZRANGE myzset 0 -1 1) "one" 2) "two" 3) "three" 127.0.0.1:6379> ZRANGEBYSCORE myzset -inf +inf 1) "one" 2) "two" 3) "three" 127.0.0.1:6379> ZRANGEBYSCORE myzset 0 1 1) "one" 127.0.0.1:6379> ZREVRANGE myzset 1 -1 1) "two" 2) "one" 127.0.0.1:6379> ZRANGEBYSCORE myzset -inf +inf withscores 1) "one" 2) "1" 3) "two" 4) "2" 5) "three" 6) "3"
②zrem(移除元素)、zcard(查看元素个数)、zcount(查询指定区间内的元素个数)操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 127.0.0.1:6379> zadd myset 1 v1 2 v2 3 v3 4 v4 (integer ) 4 127.0.0.1:6379> ZRANGE myset 0 -1 1) "v1" 2) "v2" 3) "v3" 4) "v4" 127.0.0.1:6379> zrem myset v3 (integer ) 1 127.0.0.1:6379> ZRANGE myset 0 -1 1) "v1" 2) "v2" 3) "v4" 127.0.0.1:6379> zcard myset (integer ) 3 127.0.0.1:6379> zcount myset 0 100 (integer ) 3 127.0.0.1:6379> zcount myset 0 2 (integer ) 2
③总结:成绩表排序,工资表排序,年龄排序等需求可以用zset来实现!