Redis概述
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库、缓存和消息中间件。 它支持多种类型的数据结构,如 字符串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 与范围查询, bitmaps, hyperloglogs 和 地理空间(geospatial) 索引半径查询。 Redis 内置了 复制(replication),LUA脚本(Lua scripting), LRU驱动事件(LRU eviction),事务(transactions) 和不同级别的 磁盘持久化(persistence), 并通过 Redis哨兵(Sentinel)和自动 分区(Cluster)提供高可用性(high availability)。
1、概念
Redis是用C语言开发的一个开源的高性能基于内存运行的键值对NoSQL数据库。
Redis默认拥有16个数据库,数据库编号从0开始,默认使用0号数据库。
使用select 数据库编号 可以切换使用的数据库。
2、Redis数据类型
Redis采用键值对存储数据,key永远是String类型,五大数据类型指的是value部分。
Redis有常用五种数据类型:String、List、Set、Zset、Hash。
3、String类型常用命令
String类型又可以分为字符串、数值、bitmap操作。
字符串常用操作
127.0.0.1:6379> set k1 hello
OK
127.0.0.1:6379> get k1
"hello"
# EX设置key的过期时间ttl key命令可以查看key剩余过期的时间
# NX当key不存在的时候进行操作,当key存在的时候setnx相当于灭有进行操作
127.0.0.1:6379> set key value [EX seconds] [PX milliseconds] [NX|XX]
# 存在k1所以setnx相当于没有操作
127.0.0.1:6379> set k1 ooxx Nx
(nil)
# k2不存在,把k2的值设为ooxx
127.0.0.1:6379> set k2 ooxx nx
OK
127.0.0.1:6379> get k2
"ooxx"
#获得所有的key
127.0.0.1:6379> keys *
1) "k2"
2) "k1"
#k1存在并把k1的值更新为ooxx
127.0.0.1:6379> set k1 ooxx xx
OK
127.0.0.1:6379> get k1
"ooxx"
#k3不存在 set xx 的时候相当于灭有进行操作
127.0.0.1:6379> set k3 xxoo xx
(nil)
127.0.0.1:6379> get k3
(nil)
127.0.0.1:6379> keys *
1) "k2"
2) "k1"
127.0.0.1:6379> get k1
"hello"
#APPEND key value 向key的value后追加
127.0.0.1:6379> APPEND k1 " world"
(integer) 11
127.0.0.1:6379> get k1
"hello world"
#GETRANGE key start end取出开始到结束部分的值。
#正向索引0123,反向索引倒数第一的索引是-1,倒数第二的索引-2
127.0.0.1:6379> GETRANGE k1 6 -1
"world"
#SETRANGE key offset value从偏移量索引位置设置
127.0.0.1:6379> SETRANGE k1 6 redis
(integer) 11
127.0.0.1:6379> get k1
"hello redis"
127.0.0.1:6379> SETRANGE k1 6 he
(integer) 11
127.0.0.1:6379> get k1
"hello hedis"
#查看key的长度
127.0.0.1:6379> STRLEN k1
(integer) 11
#查看key的value值的类型
127.0.0.1:6379> TYPE k1
string
数值常用操作
127.0.0.1:6379> OBJECT help
1) OBJECT <subcommand> key. Subcommands:
#返回与指定键关联的值的引用数。
2) refcount -- Return the number of references of the value associated with the specified key.
#返回用于存储与键关联的值的内部表示类型。
3) encoding -- Return the kind of internal representation used in order to store the value associated with a key.
4) idletime -- Return the idle time of the key, that is the approximated number of seconds elapsed since the last access to the key.
5) freq -- Return the access frequency index of the key. The returned integer is proportional to the logarithm of the recent access frequency of the key.
127.0.0.1:6379> set k1 hello
OK
127.0.0.1:6379> get k1
"hello"
#k1的value的编码是emb编码的字符串
127.0.0.1:6379> OBJECT encoding k1
"embstr"
127.0.0.1:6379> set k2 99
OK
127.0.0.1:6379> get k2
"99"
#k2的value的编码是int类型
127.0.0.1:6379> OBJECT encoding k2
"int"
#由于k2的value的编码是int数值类型可以用incr自增
127.0.0.1:6379> INCR k2
(integer) 100
127.0.0.1:6379> get k2
"100"
#INCRBY key increment 增加多少
127.0.0.1:6379> INCRBY k2 22
(integer) 122
127.0.0.1:6379> get k2
"122"
#自减
127.0.0.1:6379> DECR k2
(integer) 121
#DECRBY key decrement 自减多少
127.0.0.1:6379> DECRBY k2 21
(integer) 100
127.0.0.1:6379> get k2
"100"
#增加浮点数 没有减浮点数
127.0.0.1:6379> INCRBYFLOAT k2 0.5
"100.5"
bitmap常用操作
#一字节8位0000 0000
# SETBIT key offset value
# offset为bit(位)的索引,value的值只能是1或0
#设置k1的bit位的第一位的值为1 也就是0100 0000
127.0.0.1:6379> SETBIT k1 1 1
0
# 0100 0000对应的的ASCII码为@
127.0.0.1:6379> get k1
@
#k1的value的长度为1字节
127.0.0.1:6379> STRLEN k1
1
#在k1的bit位的第七位设置1 也就是0100 0001
127.0.0.1:6379> SETBIT k1 7 1
0
# 0100 000对应的ASCII码为A
127.0.0.1:6379> get k1
A
#在k1的bit位的第9位设置1 也就是0100 0001 , 0100 0000
127.0.0.1:6379> SETBIT k1 9 1
0
#k1的value的长度为2字节
127.0.0.1:6379> STRLEN k1
2
# 0100 0001 , 0100 0000第一个字节是A第二个字节是@所以就是A@
127.0.0.1:6379> get k1
A@
#BITPOS key bit [start] [end] bit只能为0或1
#start:为开始字节 end:结束字节
#查找bit第一次出现的索引位置
127.0.0.1:6379> help BITPOS
BITPOS key bit [start] [end]
summary: Find first bit set or clear in a string
since: 2.8.7
group: string
127.0.0.1:6379> get k1
A@
#第0个字节到第0个字节也就是第一个字节,第一次出现1的bit索引位置
127.0.0.1:6379> BITPOS k1 1 0 0
1
#第1个字节到第1个字节,也就是第二个字节,第一次出现1的bit索引位置
127.0.0.1:6379> BITPOS k1 1 1 1
9
#第0个字节到第1个字节,也就是前两个字节,第一次出现1的bit索引位置
127.0.0.1:6379> BITPOS k1 1 0 1
(integer) 1
#start:为开始字节 end:结束字节
127.0.0.1:6379> help BITCOUNT
BITCOUNT key [start end]
summary: Count set bits in a string
since: 2.6.0
group: string
#k1 0100 0001 , 0100 0000前两个字节中1出现的次数
127.0.0.1:6379> BITCOUNT k1 0 1
(integer) 3
#k1 0100 0001 , 0100 0000 第一个个字节中1出现的次数
127.0.0.1:6379> BITCOUNT k1 0 0
(integer) 2
#k1 0100 0001 , 0100 0000第二个字节中1出现的次数
127.0.0.1:6379> BITCOUNT k1 1 1
(integer) 1
#BITOP operation destkey key [key ...] 可以进行与或非操作
127.0.0.1:6379> help BITOP
BITOP operation destkey key [key ...]
summary: Perform bitwise operations between strings
since: 2.6.0
group: string
# k1 k2进行与运算
127.0.0.1:6379> BITOP and andkey k1 k2
(integer) 1
127.0.0.1:6379> get andkey
"@"
127.0.0.1:6379> BITOP or orkey k1 k2
(integer) 1
127.0.0.1:6379> get orkey
"C"
bitmap的两种应用场景
1、统计用户一年内的登录天数
#统计用户一年内的登录天数
#用户张三第三天登录
127.0.0.1:6379> SETBIT zhangsan 2 1
(integer) 0
#张三第六天登录
127.0.0.1:6379> SETBIT zhangsan 5 1
(integer) 0
127.0.0.1:6379> SETBIT zhangsan 235 1
(integer) 0
127.0.0.1:6379> SETBIT zhangsan 364 1
(integer) 0
#统计用户一年内登录的天数 一共4天
127.0.0.1:6379> BITCOUNT zhangsan
(integer) 4
#张三在最后十六天内登录的次数
127.0.0.1:6379> BITCOUNT zhangsan -2 -1
(integer) 1
2、统计系统在一到三号内登录的用户数
#SETBIT 20210101 1 1 20200101是20年一月一号内用户1(张三)登录
# SETBIT key offset value
#key是日期,bit位索引代表某一个用户 value是1表示登录0是没登陆
127.0.0.1:6379> SETBIT 20210101 1 1
(integer) 0
# 20200102是20年一月二号内用户1(张三)登录
127.0.0.1:6379> SETBIT 20210102 1 1
(integer) 0
# 20200103是20年一月三号内用户1(张三)登录
127.0.0.1:6379> SETBIT 20210103 1 1
(integer) 0
# 20200101是20年一月一号内用户2(李四)登录
127.0.0.1:6379> SETBIT 20210101 2 1
(integer) 0
# 20200103是20年一月三号内用户2(李四)登录
127.0.0.1:6379> SETBIT 20210103 2 1
(integer) 0
# 20200101是20年一月一号内用户三(王五)登录
127.0.0.1:6379> SETBIT 20210101 3 1
(integer) 0
127.0.0.1:6379> BITOP or resultkey 20210101 20210102 20210103
(integer) 1
127.0.0.1:6379> BITCOUNT resultkey
(integer) 3
string的全部命令
127.0.0.1:6379> help @string
APPEND key value
summary: Append a value to a key
since: 2.0.0
BITCOUNT key [start end]
summary: Count set bits in a string
since: 2.6.0
BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
summary: Perform arbitrary bitfield integer operations on strings
since: 3.2.0
BITOP operation destkey key [key ...]
summary: Perform bitwise operations between strings
since: 2.6.0
BITPOS key bit [start] [end]
summary: Find first bit set or clear in a string
since: 2.8.7
DECR key
summary: Decrement the integer value of a key by one
since: 1.0.0
DECRBY key decrement
summary: Decrement the integer value of a key by the given number
since: 1.0.0
GET key
summary: Get the value of a key
since: 1.0.0
GETBIT key offset
summary: Returns the bit value at offset in the string value stored at key
since: 2.2.0
GETRANGE key start end
summary: Get a substring of the string stored at a key
since: 2.4.0
GETSET key value
summary: Set the string value of a key and return its old value
since: 1.0.0
INCR key
summary: Increment the integer value of a key by one
since: 1.0.0
INCRBY key increment
summary: Increment the integer value of a key by the given amount
since: 1.0.0
INCRBYFLOAT key increment
summary: Increment the float value of a key by the given amount
since: 2.6.0
MGET key [key ...]
summary: Get the values of all the given keys
since: 1.0.0
MSET key value [key value ...]
summary: Set multiple keys to multiple values
since: 1.0.1
MSETNX key value [key value ...]
summary: Set multiple keys to multiple values, only if none of the keys exist
since: 1.0.1
PSETEX key milliseconds value
summary: Set the value and expiration in milliseconds of a key
since: 2.6.0
SET key value [EX seconds] [PX milliseconds] [NX|XX]
summary: Set the string value of a key
since: 1.0.0
SETBIT key offset value
summary: Sets or clears the bit at offset in the string value stored at key
since: 2.2.0
SETEX key seconds value
summary: Set the value and expiration of a key
since: 2.0.0
SETNX key value
summary: Set the value of a key, only if the key does not exist
since: 1.0.0
SETRANGE key offset value
summary: Overwrite part of a string at key starting at the specified offset
since: 2.2.0
STRLEN key
summary: Get the length of the value stored in a key
since: 2.2.0