启动redis

  • 启动redis
systemctl start redis.service
  • 连接redis
1:方式一
redis-cli -h 192.168.23.10 -p 6379 (无密码登入,会报错,(error) NOAUTH Authentication required. 可以在redis的shell接口中 auth 123456,完成授权)
 
2:方式二
redis-cli -h 192.168.23.10 -p 6379 -a 123456 (有密码读入,已经被授权)
  • 选择数据库
由于在配置文件中默认定义使用16个database,因此每次登入Redis时,可以选择使用哪个数据库
select 0:表示使用0号数据库
select 1:表示使用1号数据库
  • 清空数据库
flushdb :清空当前数据库
flushall :清空所有的库
  • 在Redis中的命令命令组
1:命令组与命令的联系
一个命令组中包含多个命令,可以使用 help @命令组,查看某个命令组中有多少命令,已经这些命令如何使用
help @string
help @server
 
2:也可以查看特定命令的用法
help LLEN
help STRLEN

(二) Redis常用命令介绍

Redis命令参考手册 https://redis.io/commands
各种语言的API接口:https://redis.io/clients

  • Redis有很多的常用命令
1:赋值与取值 (set key value [EX seconds] [PX milliseconds] [NX|XX])
set uplooking ops
get uplooking
 
EX:表示超时时长
NX:如果一个键不存在则创建,存在不创建
XX:如果一个键存在则创建,不存在不创建
 
2:判断一个键值是否存在, 如果存在返回整数类型1 ,否则返回0
exists uplooking
 
3:追加值
append uplooking " java"
 
4:删除键
del key [key.....]
del uplooking
 
5:设置一个整数值,让数值增加
set number 1
incr number (加1)
decr number (减1)
 
6:使用keys命令搜索已经设置的key
keys 正则表达式
? 匹配一个字符
* 匹配任意个(包括0个)字符
[] 匹配括号间的任一个字符,可以使用 "-" 符号表示一个范围,如 a[b-d] 可以匹配 "ab","ac","ad"
x 匹配字符x,用于转义符号,如果要匹配 "?" 就需要使用 ?
 
例如:keys * :查看有哪些key
  • Redis中的5种数据结构
Redis的数据结构指的是key-value中value的数据结构,还记得Redis作为ELK的消息中间件吗?使用的是Redis的list数据结构
 
Redis的数据结构有5中,也就是说,value的结构有五种
1:string
例如:
set uplooking ops
 
2:list:value就是一个string数组,操作这组string时,可以像对待栈一样使用pop和push操作,但是这个栈两端都能进行操作;也可以像对待数组一样使用一个index参数来操作。list的操作命令略杂,主要分为两类:L开头的和R开头的,L代表LEFT或者LIST,进行一些从列表左端进行的操作,或者一些与端无关的操作;R代表RIGHT,进行一些从列表右端进行的操作
例如:
使用help @list可以查看帮助
lpush uplooking java (给uplooking键对应的列表添加一个值)
lindex uplooking 0 (通过索引从uplooking中取值)
lpush uplooking ops (从左边插入值)
rpush uplooking c++ (从右边插入值)
lpop uplooking (从左边pop一个值)
rpop uplooking (从右边pop一个值)
lset uplooking 0 Python (将索引为0对应的值改为Python)
 
3:set:用于存储一组不重复的值,也可以进行一些集合的操作,就像数学上的集合,它是无序的,且可以就交集、并集、补集运算
例如:
sadd home laoge dage zhongge xiaoge xiaoxiaoge (创建一个home集合,集合中有五个元素)
sismember home dage (查看home集合中是否有dage元素)
 
sadd home1 dage xiaoge niubi tainiubi (再创建一个集合)
sinter home home1 (求home和home1两个集合的交集)
sunion home home1 (求home和home1两个集合的并集)
smembers home (查看集合中所有的元素)
spop home1 (在home1集合中随机删除一个元素)
 
 
4:sorted set:类似set,但是sorted set里每个元素都有一个score,这个score可用于排序和排名
例如:
del home home1 (删除之前的两个set)
help @sorted_set
zadd home1 1 niubi 2 tainiubi (创建一个有序集合,给两个元素)
 
 
 
5:hash:hash结构,其实就是value本身就是一组key-value对,不过redis将这里的key称为field,也就是value是一组field-value对
例如:
help @hash
hset home2 class ops (设置值)
hget home2 class (取值)
hdel home2 class (删除值)
hkeys home2 (查看home2中所有的field)
hvals home2 (查看home2中所有的value)

(三) Redis 其他知识点介绍

这里的事务与MySQL的事务类似:多个操作要么一次性执行完毕,要么都不执行

  • 使用multi开启一个事务
multi (开启一个事务日志)
set name yhy
set age 25
set address changsha
exec (提交,执行)

(四) Redis 其他知识点介绍


  • connection命令介绍
AUTH password
summary: Authenticate to the server
since: 1.0.0
 
ECHO message
summary: Echo the given string
since: 1.0.0
 
PING [message]
summary: Ping the server
since: 1.0.0
 
QUIT -
summary: Close the connection
since: 1.0.0
 
SELECT index
summary: Change the selected database for the current connection
since: 1.0.0
  • server相关的命令介绍
help @server
client setname yhy_client (设置客户端名为yhy_client)
client getname (查看当前的客户端的名称)
info (查看服务器端的各个参数)
config get bind (拿到bind指令的值)
ocnfig set bind
dbsize (查看当前数据库的键值对数量)
  • Redis的发布与订阅功能
1:什么是发布(publish)与订阅功能(subscribe),什么是消费者与生产者
生产者把需要发布的信息发布到固定的队列或者频道,消费者从对应的队列或者频道中取出信息
 
2:使用subscribe命令订阅一个频道
subscribe uplooking (订阅一个uplooking频道)
 
3:使用publish发布消息
publish uplooking "hello uplooking" (在uplooking频道发布一条消息“hello uplooking”)
 
4:使用正则表达式订阅频道
psubscribe uplooking.*
  • Redis持久化实现
1:RDB
基于snapshot实现持久化存储,数据文件叫dump.rdb
save 900 1
save 300 10
save 60 10000
 
2:AOF
记录每一次的写操作于文件中,实现数据持久化
appendonly no
原文地址:https://www.cnblogs.com/liu1026/p/7685976.html