Redis基础知识
Redis默认有16个数据库,默认使用第一个
127.0.0.1:6379> select 3 #可以使用select 进行数据库切换
OK
127.0.0.1:6379[3]>
127.0.0.1:6379[3]> DBSIZE #数据库大小
(integer) 0
127.0.0.1:6379[3]> set my name
OK
127.0.0.1:6379[3]> DBSIZE
(integer) 1
127.0.0.1:6379[3]> flushdb #清空数据库
OK
127.0.0.1:6379[3]>FLUSHALL #清空所有数据库
OK
127.0.0.1:6379[3]> set name zhang
OK
127.0.0.1:6379[3]> EXPIRE name 50 #设置过期时间
(integer) 1
127.0.0.1:6379[3]> TTL name # 查看当前key的剩余时间
(integer) 35
127.0.0.1:6379[3]> TTL name
(integer) 29
127.0.0.1:6379> set name fengjun
OK
127.0.0.1:6379> move name
(error) ERR wrong number of arguments for 'move' command
127.0.0.1:6379> move name 1 #移除当前的key
(integer) 1
127.0.0.1:6379> get name
(nil)
127.0.0.1:6379> set name 1
OK
127.0.0.1:6379> type name #判断key的类型
string
127.0.0.1:6379> LPUSH list 1 2 3
(integer) 3
127.0.0.1:6379> type list
list
五大数据类型
1) String(字符串)
String类似的使用场景:value除了是字符串还可以是数字!!!
- 计数器
- 统计多单位的数量 set uid:858345:follow 0
- 粉丝数
- 对象缓存存储
127.0.0.1:6379> set name fengjun #设置key的value
OK
127.0.0.1:6379> get name #获得值
"fengjun"
127.0.0.1:6379> set age 22
OK
127.0.0.1:6379> keys * #查看所有的key
1) "age"
2) "name"
127.0.0.1:6379> EXISTS name #判断当前key是否存在
(integer) 1
127.0.0.1:6379> EXISTS sex
(integer) 0
127.0.0.1:6379> APPEND name "zi" #当前key后面追加字符串,如果key不存在相当于set key
(integer) 9
127.0.0.1:6379> get name
"fengjunzi"
127.0.0.1:6379> STRLEN name #查看key的长度
(integer) 9
127.0.0.1:6379> set view 0
OK
127.0.0.1:6379> get view
"0"
127.0.0.1:6379> incr view #自增1
(integer) 1
127.0.0.1:6379> DECR view #自减1
(integer) 0
127.0.0.1:6379> INCRBY view 10
(integer) 10
127.0.0.1:6379> DECRBY view 4
(integer) 6
127.0.0.1:6379> set user zhangsan
OK
127.0.0.1:6379> GETRANGE user 0 2 #截取字符串
"zha"
127.0.0.1:6379> get user
"zhangsan"
127.0.0.1:6379> SETRANGE user 0 hello #替换指定位置的字符串
(integer) 8
127.0.0.1:6379> get user
"hellosan"
###setex (set with expire) 设置过期时间
127.0.0.1:6379> setex key3 30 "hello" #设置key的值为hello,30秒后过期
OK
###setnx (set if not exist) 不存在设置(在分布式锁使用)
127.0.0.1:6379> setnx mykey "redis" #如果mykey不存在,创建mykey/如果mykey存在,创建失败!
(integer) 1
127.0.0.1:6379> mset k1 v1 k2 v2 l3 v3 #同时设置多个值
OK
127.0.0.1:6379> mget k1 k2 k3 #同时获取多个值
1) "v1"
2) "v2"
3) (nil)
127.0.0.1:6379> msetnx k1 v1 k4 v4 #msetnx 是一个原子操作,要么一起成功,要么一起失败!
(integer) 0
127.0.0.1:6379> keys * #查看所有的key
1) "k1"
2) "k2"
3) "user"
4) "name"
5) "l3"
6) "age"
7) "view"
8) "mykey"
127.0.0.1:6379> set user:1 {name:zhang,age:11,sex:1} #设置一个user:1 对象 值为 json字符串保存对象!!
OK
127.0.0.1:6379> get user:1
"{name:zhang,age:11,sex:1}"
#########这里的key是一个巧妙的设计 user:{id}:{filed}
127.0.0.1:6379> mset user:1:name zhang user:1:age 11 user:1:sex 1
OK
127.0.0.1:6379> mget user:1:name user:1:age user:1:sex
1) "zhang"
2) "11"
3) "1"
2) List(列表)
List可以完成栈、队列、阻塞队列
127.0.0.1:6379> LPUSH list one #将一个或多个值插入列表的头部
(integer) 1
127.0.0.1:6379> LPUSH list two
(integer) 2
127.0.0.1:6379> LPUSH list three
(integer) 3
127.0.0.1:6379> LRANGE list 0 -1 #获取list的值
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> LRANGE list 0 1 #通过区间获取list的值
1) "three"
2) "two"
127.0.0.1:6379> RPUSH list right #将一个或多个值插入列表的尾部
(integer) 4
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379>
127.0.0.1:6379> LRANGE list 0 -1
1) "three"
2) "two"
3) "one"
4) "right"
127.0.0.1:6379> LPOP list #移除list第一个元素
"three"
127.0.0.1:6379> RPOP list #移除list的最后一个元素
"right"
127.0.0.1:6379> LINDEX list 0 #通过下标获取lsit的某个值
"two"
127.0.0.1:6379> LINDEX list 1
"one"
127.0.0.1:6379> lpush list 1
(integer) 1
127.0.0.1:6379> lpush list 2
(integer) 2
127.0.0.1:6379> lpush list 3
(integer) 3
127.0.0.1:6379> lpush list 4
(integer) 4
127.0.0.1:6379> LLEN list #返回列表的长度
(integer) 4
127.0.0.1:6379> LREM list 1 1 #移除list集合中指定个数的value
(integer) 1
127.0.0.1:6379> LRANGE list 0 -1
1) "4"
2) "3"
3) "2"
127.0.0.1:6379> RPUSH mylist hello1 hello2 hello3 hello4 hello5
(integer) 5
127.0.0.1:6379> LTRIM mylist 0 1 #通过下标截取指定的长度,这个list改变了
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello1"
2) "hello2"
127.0.0.1:6379> RPUSH mylist hello1
(integer) 1
127.0.0.1:6379> RPUSH mylist hello2 hello3
(integer) 3
127.0.0.1:6379> RPOPLPUSH mylist otherlist #移除列表最后一个元素,将它移到新的列表
"hello3"
127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello1"
2) "hello2"
127.0.0.1:6379> LRANGE otherlist 0 -1
1) "hello3"
127.0.0.1:6379> EXISTS list
(integer) 0
127.0.0.1:6379> EXISTS mylist
(integer) 1
127.0.0.1:6379> LSET mylist 0 newvalue #将列表指定下标值更新为另一个值|存在就替换
OK
127.0.0.1:6379> LRANGE mylist 0 -1
1) "newvalue"
2) "hello2"
127.0.0.1:6379> LSET mylist 3 qq #如果不存在,报错
(error) ERR index out of range
127.0.0.1:6379> LRANGE mylist 0 -1
1) "newvalue"
2) "hello2"
#LINSERT 将某个具体的值插入到列中某个元素的前面或后面
127.0.0.1:6379> LINSERT mylist before "hello2" "hello1"
(integer) 3
127.0.0.1:6379> LRANGE mylist 0 -1
1) "newvalue"
2) "hello1"
3) "hello2"
127.0.0.1:6379> LINSERT mylist after "hello2" "hello2"
(integer) 4
127.0.0.1:6379> LRANGE mylist 0 -1
1) "newvalue"
2) "hello1"
3) "hello2"
4) "hello2"
3) Set(集合)
Set中值不能重复的
127.0.0.1:6379> sadd myset hello1 #set集合添加值
(integer) 1
127.0.0.1:6379> sadd myset hello2 hello3 hello4
(integer) 3
127.0.0.1:6379> SMEMBERS myset #查看指定set所有值
1) "hello3"
2) "hello2"
3) "hello1"
4) "hello4"
127.0.0.1:6379> SISMEMBER myset hello #判断某个值是否在sset集合中
(integer) 0
127.0.0.1:6379> SISMEMBER myset hello2
(integer) 1
127.0.0.1:6379> SADD hello1
(error) ERR wrong number of arguments for 'sadd' command
127.0.0.1:6379> SCARD myset #获取set集合元素的个数
(integer) 4
127.0.0.1:6379> SREM myset hello #移除set中指定元素
(integer) 0
127.0.0.1:6379> SREM myset hello1
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "hello3"
2) "hello2"
3) "hello4"
127.0.0.1:6379> SRANDMEMBER myset 2 #随机抽选出指定个数的元素
1) "hello2"
2) "hello3"
127.0.0.1:6379> SRANDMEMBER myset #随机抽选出1个元素
1) "hello4"
127.0.0.1:6379> SADD myset qwe asd zxv fff
(integer) 4
127.0.0.1:6379> SMEMBERS myset
1) "hello4"
2) "qwe"
3) "fff"
4) "zxv"
5) "hello2"
6) "hello3"
7) "asd"
127.0.0.1:6379> SPOP myset #随机删除set集合中元素
"zxv"
127.0.0.1:6379> SPOP myset 2
1) "hello2"
2) "qwe"
127.0.0.1:6379> SMEMBERS myset
1) "fff"
2) "hello4"
3) "hello3"
4) "asd"
127.0.0.1:6379> SMEMBERS myset
1) "fff"
2) "hello4"
3) "hello3"
4) "asd"
127.0.0.1:6379> sadd set hello
(integer) 1
127.0.0.1:6379> SMOVE myset set asd #讲一个指定的值移动到另一个set集合
(integer) 1
127.0.0.1:6379> SMEMBERS myset
1) "fff"
2) "hello4"
3) "hello3"
127.0.0.1:6379> SMEMBERS set
1) "asd"
2) "hello"
# 数字集合:
- 差集
- 交集
- 并集
127.0.0.1:6379> SADD k1 a
(integer) 1
127.0.0.1:6379> SADD k1 b
(integer) 1
127.0.0.1:6379> SADD k1 c
(integer) 1
127.0.0.1:6379> SADD k2 c
(integer) 1
127.0.0.1:6379> SADD k2 d
(integer) 1
127.0.0.1:6379> SDIFF k1 k2 #差集
1) "a"
2) "b"
127.0.0.1:6379> SINTER k1 k2 #交集 共同好友可以这样实现
1) "c"
127.0.0.1:6379> SUNION k1 k2 #并集
1) "d"
2) "a"
3) "c"
4) "b"
4) Hash(哈希)
map集合,key-map! 本质和String类型没有太大区别,还是简单的一个key-value
127.0.0.1:6379> HSET myhash field1 java #set一个具体的key-value
(integer) 1
127.0.0.1:6379> HGET myhash field1 #获取一个字段值
"java"
127.0.0.1:6379> HMSET myhash field1 redis field2 mysql field3 python #set多个具体的key-value
OK
127.0.0.1:6379> HMGET myhash field1 field2 #获取多个字段值
1) "redis"
2) "mysql"
127.0.0.1:6379> HGETALL myhash #获取全部的数据
1) "field1"
2) "redis"
3) "field2"
4) "mysql"
5) "field3"
6) "python"
127.0.0.1:6379> HDEL myhash field3 #删除hash指定的key字段,对应的value删掉
(integer) 1
127.0.0.1:6379> HGETALL myhash
1) "field1"
2) "redis"
3) "field2"
4) "mysql"
127.0.0.1:6379> HLEN myhash
(integer) 2
127.0.0.1:6379> HSET myhash field3 java
(integer) 1
127.0.0.1:6379> HLEN myhash #获取hash表的字段数量
(integer) 3
127.0.0.1:6379> HEXISTS myhash field3 #判断hash指定字段是否存在
(integer) 1
127.0.0.1:6379> HEXISTS myhash field5
(integer) 0
127.0.0.1:6379> HKEYS myhash #只获取所有的field
1) "field1"
2) "field2"
3) "field3"
127.0.0.1:6379> HVALS myhash #只获取所有的value
1) "redis"
2) "mysql"
3) "java"
127.0.0.1:6379> HSET myhash field 3
(integer) 1
127.0.0.1:6379> HINCRBY myhash field 4 #指定增量
(integer) 7
127.0.0.1:6379> HSETNX myhash field 4 #如果存在,不以设置
(integer) 0
127.0.0.1:6379> HSETNX myhash field10 4 #如果不存在,则可以设置
(integer) 1
#场景
- 变更经常变动的数据 hash更适合对象的存储,string更适合字符串存储
127.0.0.1:6379> hset user1 name zhang age 23 sex 1
(integer) 3
127.0.0.1:6379> hget user1 name
"zhang"
127.0.0.1:6379> hget user1 age
"23"
5) Zset(有序集合)
在set基础上增加了一个值,set k1 v1 zset k1 score1 v1
127.0.0.1:6379> zadd salary 2000 zhang #添加一个值
(integer) 1
127.0.0.1:6379> zadd salary 8000 wang 4000 huang #添加多个值
(integer) 2
127.0.0.1:6379> zadd salary 1000 fang
(integer) 1
127.0.0.1:6379> ZRANGE salary 0 -1
1) "fang"
2) "zhang"
3) "huang"
4) "wang"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf +inf #从小到大显示
1) "fang"
2) "zhang"
3) "huang"
4) "wang"
127.0.0.1:6379> ZREVRANGE salary 0 -1 #从大到小
1) "wang"
2) "huang"
3) "zhang"
4) "fang"
127.0.0.1:6379> ZRANGEBYSCORE salary -inf 5000 withscores
1) "fang"
2) "1000"
3) "zhang"
4) "2000"
5) "huang"
6) "4000"
127.0.0.1:6379> ZCARD salary #读取有序集合个数
(integer) 4
127.0.0.1:6379> ZREM salary wang #删除
(integer) 1
127.0.0.1:6379> ZREVRANGE salary 0 -1
1) "huang"
2) "zhang"
3) "fang"
三种特殊类型
geospatial地理位置
朋友的定位,附近的人,打车距离的计算
参数(维度,经度,名称)
127.0.0.1:6379> GEOADD china:city 116.40 39.30 beijing
(integer) 1
127.0.0.1:6379> GEOADD china:city 121.47 31.23 shanghai
(integer) 1
127.0.0.1:6379> GEOADD china:city 106.50 29.53 chongqing
(integer) 1
127.0.0.1:6379> GEOADD china:city 114.05 22.52 shenzhen 120.16 30.24 hangzhou 108.96 34.26 xian
(integer) 3
127.0.0.1:6379> GEOPOS china:city beijing xian #获取指定城市的经度和纬度
1) 1) "116.39999896287918091"
2) "39.30000117660147652"
2) 1) "108.96000176668167114"
2) "34.25999964418929977"
127.0.0.1:6379> GEOPOS china:city beijing
1) 1) "116.39999896287918091"
2) "39.30000117660147652"
# GEODIST 单位
- m 表示单位为米
- km 表示单位为千米
- mi 表示单位为英里
- ft 表示单位为英尺
127.0.0.1:6379> GEODIST china:city shanghai beijing #两个之间的距离
"1008342.6601"
127.0.0.1:6379> GEODIST china:city shanghai beijing km
"1008.3427"
# GEORADIUS 找附近的人?(给定的经纬度为中心,找出某一半径内的元素)
127.0.0.1:6379> GEORADIUS china:city 121.47 31.23 500 km
1) "hangzhou"
2) "shanghai"
127.0.0.1:6379> GEORADIUS china:city 121.47 31.23 5000 km count 2
1) "shanghai"
2) "hangzhou"
127.0.0.1:6379> GEORADIUS china:city 121.47 31.23 5000 km count 3
1) "shanghai"
2) "hangzhou"
3) "beijing"
127.0.0.1:6379> GEORADIUSBYMEMBER china:city shanghai 500 km #找出位于指定元素周围的其他元素!
1) "hangzhou"
2) "shanghai"
#### GEO底层实现原理就是Zset,可以通过Zset命令操作GEO
127.0.0.1:6379> ZREM china:city beijing #移除指定元素
(integer) 1
127.0.0.1:6379> ZRANGE china:city 0 -1 #查看
1) "chongqing"
2) "xian"
3) "shenzhen"
4) "hangzhou"
5) "shanghai"
Hyperloglog
Hyperloglog:基数统计算法 网页的UV(一个人访问一个网站多次,但是还算是一个人)
优点:占用内存固定,2^64 不同的元素的技术,只需要废12kb内存!
- A(1,3,5,7,8,7)
- B(1,3,5,7,8)
什么是基数? 不重复的元素=5 可以接受误差
127.0.0.1:6379> PFADD list a b c d e f g h i j #创建第一组元素
(integer) 1
127.0.0.1:6379> PFCOUNT list #统计元素的基数数量
(integer) 10
127.0.0.1:6379> PFADD mylist i j z x c v b
(integer) 1
127.0.0.1:6379> PFCOUNT mylist
(integer) 7
127.0.0.1:6379> PFMERGE result list mylist #合并两组 list mylist => result 并集
OK
127.0.0.1:6379> PFCOUNT result #查看并集数量
(integer) 13
****如果允许容错,可以使用Hyperloglog****
Bitmaps
位存储,都是操作二进制进行记录,就只有0和1两种状态
统计用户信息
- 活跃/不活跃
- 登录/未登录
- 打卡,365天打卡
365天=365bit 1字节=8bit 46个字节左右
127.0.0.1:6379> SETBIT sign 0 1
(integer) 0
127.0.0.1:6379> SETBIT sign 1 1
(integer) 0
127.0.0.1:6379> SETBIT sign 2 1
(integer) 0
127.0.0.1:6379> SETBIT sign 3 0
(integer) 0
127.0.0.1:6379> SETBIT sign 4 0
(integer) 0
127.0.0.1:6379> SETBIT sign 5 1
(integer) 0
127.0.0.1:6379> SETBIT sign 6 0
(integer) 0
127.0.0.1:6379> GETBIT sign 0
(integer) 1
127.0.0.1:6379> GETBIT sign 3 #查看某一天是否打卡
(integer) 0
127.0.0.1:6379> BITCOUNT sign #统计操作:统计是否全勤
(integer) 4