网站开发与rest,某企业网络营销方案,分类导航wordpress,flash做网站的论文⛰️个人主页: 蒾酒
#x1f525;系列专栏#xff1a;《spring boot实战》
#x1f30a;山高路远#xff0c;行路漫漫#xff0c;终有归途。 目录
前置条件
1.导依赖
2.配置连接信息以及连接池参数
3.配置序列化方式
4.编写测试 前置条件
已经初始化好一个spr…
⛰️个人主页: 蒾酒
系列专栏《spring boot实战》
山高路远行路漫漫终有归途。 目录
前置条件
1.导依赖
2.配置连接信息以及连接池参数
3.配置序列化方式
4.编写测试 前置条件
已经初始化好一个spring boot项目且版本为3X项目可正常启动。
作者版本为3.2.2
初始化教程
新版idea(2023)创建spring boot3项目-CSDN博客https://blog.csdn.net/qq_62262918/article/details/135785412?spm1001.2014.3001.5501
1.导依赖
pom.xml:
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId
/dependency
如果还没安装redis可以参照这篇
阿里云ECS使用docke搭建redis服务-CSDN博客https://blog.csdn.net/qq_62262918/article/details/135707725?spm1001.2014.3001.5502 2.配置连接信息以及连接池参数
application.yml server:port: 8080
spring:data:redis: # Redis连接配置host: localhost # Redis主机地址port: 6379 # Redis端口号password: 123456 # 访问Redis所需密码database: 0 # 使用的数据库编号lettuce: #Lettuce客户端配置pool: # 连接池配置max-active: 8 # 最大活跃连接数max-wait: -1 # 最大等待时间-1表示无限等待max-idle: 8 # 最大空闲连接数min-idle: 0 # 最小空闲连接数
修改为你的连接信息即可。
这里要说的是
Lettuce和Jedis两者都是Java连接Redis的客户端
选择使用Lettuce而不是Jedis的原因如下 线程安全性 Lettuce 是基于 Netty 构建的它使用异步和事件驱动的方式处理连接。因此它可以在多个线程之间共享一个连接而不需要额外的同步因此在高并发环境下更高效。Jedis 是基于阻塞 I/O 的并且不是线程安全的如果在多个线程中共享同一个 Jedis 实例需要使用连接池进行同步管理这可能引入额外的复杂性。 连接方式 Lettuce 支持基于 Reactive Streams 的响应式编程模型能够更好地与 Spring Reactor、Project Reactor 等框架集成提供异步和非阻塞的操作。Jedis 是同步的并且在执行某些操作时会阻塞线程这可能会影响应用程序的性能和响应性。 性能和扩展性 Lettuce 的设计目标是高性能和扩展性它可以更好地利用 Redis 4.0 中引入的一些新特性如 Redis Sentinel 和 Redis Cluster。Jedis 的设计目标更偏向于简单易用对于一些特殊的 Redis 集群模式可能支持不够完善。 维护和更新 Lettuce 是一个活跃的项目并且持续地得到更新和改进。Jedis 在某些方面已经相对稳定并且在一段时间内没有大的更新。 3.配置序列化方式
config目录下新建redis配置类 配置类代码如下
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;/*** author mijiupro*/
Configuration
public class RedisConfig {Beanpublic RedisTemplateString, Object redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplateString, Object redisTemplate new RedisTemplate();redisTemplate.setConnectionFactory(connectionFactory);// 设置key和value的序列化方式redisTemplate.setKeySerializer(new StringRedisSerializer()); // 设置key的序列化器为StringRedisSerializerredisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); // 设置value的序列化器为JdkSerializationRedisSerializerredisTemplate.setHashKeySerializer(new StringRedisSerializer()); // 设置hash key的序列化器为StringRedisSerializerredisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer()); // 设置hash value的序列化器为JdkSerializationRedisSerializerredisTemplate.afterPropertiesSet(); // 初始化RedisTemplatereturn redisTemplate; // 返回配置好的RedisTemplate}
}
4.编写测试 import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;import java.util.concurrent.TimeUnit;SpringBootTest
public class RedisTest {Autowiredprivate StringRedisTemplate stringRedisTemplate;Test//测试redisvoid contextLoads2() {//添加缓存键值对name:mijiu并设置过期时间为1小时stringRedisTemplate.opsForValue().set(name,mijiu,10, TimeUnit.SECONDS);System.out.println(stringRedisTemplate.opsForValue().get(name));}
}
运行测试 测试成功整合完毕