目录
5.lettcus(springboot默认)与jedis的区别
1.Redis下载安装与基本使用
redis下载地址:Releases · microsoftarchive/redis · GitHub
如果无法进入github,可自行百度(常见方法有改host文件以及使用fastgithub)
选择zip包
下载完成后对其进行解压操作,然后进入解压的路径,并输入cmd
然后输入客户端启动命令
redis-server.exe redis.windows.conf
看到这个标志说明启动成功
然后再在该路径用cmd打开一个黑窗口
输入redis-cli 打开客户端
常用命令(来自Redis常用命令大全_wscra的博客-CSDN博客_redis常用命令大全)
启动redis服务
在redis的src目录下执行命令:
./redis-server
启动redis客户端实例
在redis的src目录下执行命令:
./redis-cli
连接远程redis服务器:
redis-cli -h host -p port -a password
设置key-value
set key value
获取值
get key
删除
del key
判断key是否存在
exists key
设置10秒过期
expire key 10
设置10毫秒过期
pexpire key 10
删除过期时间
persist key
切换数据库
redis有16个数据库,默认使用0号数据库,切换数据库的命令为:
select index(index表示数据库编号)
清空当前选中的数据库
flushdb
清空所有数据库
flushall
查看当前数据库的所有key
keys *
查看字段类型
type key
2.SpringBoot整合Redis
springboot导入redis坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置redis:
spring:
redis:
host: localhost
port: 6379
客户端测试
@Test
void set(){
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("age",41);
}
@Test
void get(){
ValueOperations valueOperations = redisTemplate.opsForValue();
Object age=valueOperations.get("age");
System.out.println(age);
}
3.读写Redis客户端
客户端测试
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void get(){
ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
String name = ops.get("name");
System.out.println(name);
}
4.操作Redis客户端实现技术转换jedis
导入jedis坐标
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
配置客户端
spring:
redis:
host: localhost
port: 6379
client-name: jedis
5.lettcus(springboot默认)与jedis的区别
jedis连接Redis服务器是直连模式 当多线程模式下jedis会存在线程安全问题,解决方案可通过配置连接池使每个连接专用 但这样会大幅影响性能
letcus基于Netty框架与Redis服务器连接 底层设计采用StatefulRedisConnection StatefulRedisConnection 自身是线程安全的,可以保障并发方位安全问题,所以一个链接可以被多线程复用 当然 lettcus也支持多连接实例一起工作 因此springboot默认使用lettcue