学习来源:狂神说Javabilibili
Jedis
Jedis是redis官方推荐的java连接开发工具!使用java操作redis中间件,如果要操作redis,一定要对jedis十分熟悉。
测试
-
导入对应依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency>
-
编码测试:
- 连接数据库
- 操作命令
- 断开链接
package com.zahng; import redis.clients.jedis.Jedis; public class TestPing { public static void main(String[] args) { // 1.new Jedis对象 Jedis jedis = new Jedis("127.0.01", 6379); // jedis 所有函数就是之前学习的命令 System.out.println(jedis.ping()); } }
连接成功,输出PONG。
SpringBoot整合redis
整合测试
注意:
- 在springboot2.x之后,原来的Jedis被替换为lettuce
- Jedis:采用的是直连,多个线程操作是不安全的,如果想要避免不安全,需要使用redis pool,更像BIO模式。
- lettuce:地产采用netty,实例可以在多个线程中共享,不存在线程不安全的情况,可以减少线程数据,更像NIO模式
源码分析:
@Bean @ConditionalOnMissingBean(name = "redisTemplate") public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { // 默认的redisTemplate,没有过多的设置,redis对象都是需要序列化 // 两个泛型都是object的。我们使用需要强转<String,Object> RedisTemplate<Object, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); return template; } @Bean @ConditionalOnMissingBean // 由于Stirng类型是redis常用的,所以单独设置了一个,操作string可以使用这个 public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException { StringRedisTemplate template = new StringRedisTemplate(); template.setConnectionFactory(redisConnectionFactory); return template; }
Redis.conf详解
单位(UNITS)
1k => 1000bytes
1kb => 1024bytes
1m => 1000000bytes
1mb => 1024 * 1024bytes
1g => 1000000000bytes
1gb => 1024 * 1024 * 1024bytes
1 、配置文件 untoit单位 对大小写不敏感!
包含 (INpathCLUDES)
include /path/to/local.conf
include /path/to/other.conf
就像我们学习过的Spring、import、include
网络(NETWORK)
bind 127 .0.0.1 # 绑定的ip
protected-mode yes # 保护模式
port 6379 # 端口设置
通用 (GENERAL)
daemonize yes # 以守护进程的方式运行,默认是 no,我们需要自己开启为yes!
pidfile /var/run/redis_6379.pid # 如果以后台的方式运行,我们就需要指定一个 pid 文件!
# 日志
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably) 生产环境
# warning (only very important / critical messages are logged)
loglevel notice
logfile "" # 日志的文件位置名
databases 16 # 数据库的数量,默认是 16 个数据库
always-show-logo yes # 是否总是显示LOGO
快照
持久化, 在规定的时间内,执行了多少次操作,则会持久化到文件 .rdb. aof
redis 是内存数据库,如果没有持久化,那么数据断电及失!
# 如果900s内,如果至少有一个1 key进行了修改,我们及进行持久化操作
save 900 1
# 如果300s内,如果至少10 key进行了修改,我们及进行持久化操作
save 300 10
# 如果60s内,如果至少10000 key进行了修改,我们及进行持久化操作
save 60 10000
# 我们之后学习持久化,会自己定义这个测试!
stop-writes-on-bgsave-error yes # 持久化如果出错,是否还需要继续工作!
rdbcompression yes # 是否压缩 rdb 文件,需要消耗一些cpu资源!
rdbchecksum yes # 保存rdb文件的时候,进行错误的检查校验!
dir ./ # rdb 文件保存的目录!
REPLICATION 复制,后面讲解主从复制的,时候再进行记录
安全(SECURITY)
可以在这里设置redis的密码,默认是没有密码!
127 .0.0.1:6379> ping
PONG
127 .0.0.1:6379> config get requirepass # 获取redis的密码
1 ) "requirepass"
2 ) ""
127 .0.0.1:6379> config set requirepass "123456" # 设置redis的密码
OK
127 .0.0.1:6379> config get requirepass # 发现所有的命令都没有权限了
(error) NOAUTH Authentication required.
127 .0.0.1:6379> ping
(error) NOAUTH Authentication required.
127 .0.0.1:6379> auth 123456 # 使用密码进行登录!
OK
127 .0.0.1:6379> config get requirepass
1 ) "requirepass"
2 ) "123456"
限制 CLIENTS
maxclients 10000 # 设置能连接上redis的最大客户端的数量
maxmemory <bytes> # redis 配置最大的内存容量
maxmemory-policy noeviction # 内存到达上限之后的处理策略
1 、volatile-lru:只对设置了过期时间的key进行LRU(默认值)
2 、allkeys-lru : 删除lru算法的key
3 、volatile-random:随机删除即将过期key
4 、allkeys-random:随机删除
5 、volatile-ttl : 删除即将过期的
6 、noeviction : 永不过期,返回错误
APPEND ONLY 模式 aof配置
appendonly no # 默认是不开启aof模式的,默认是使用rdb方式持久化的,在大部分所有的情况下,
rdb完全够用!
appendfilename "appendonly.aof" # 持久化的文件的名字
# appendfsync always # 每次修改都会 sync。消耗性能
appendfsync everysec # 每秒执行一次 sync,可能会丢失这1s的数据!
# appendfsync no # 不执行 sync,这个时候操作系统自己同步数据,速度最快!