SpringBoot整合Redis 分布式缓存 详解
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了SpringBoot整合Redis 分布式缓存 详解,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4109字,纯文字阅读大概需要6分钟。
内容图文
在上一篇中我们使用了,Spring Cache 来做缓存,这一章我们使用Redis 分布式数据库来做缓存,达到中间件的作用。
不熟悉Redis的朋友,请看官方文档,或者看看教程,这里我列出一些。
Redis官方文档
菜鸟教程
接下来,我们使用SpringBoot 来整合Redis做缓存,先创建一个SpringBoot项目。
1.pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
2.yml配置
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost/user?serverTimezone=UTC&useSSL=false&allowMultiQueries=true
driver-class-name: com.mysql.jdbc.Driver
redis:
host: 127.0.0.1
username: root
如果有设置密码的,自己加上,或者用户名不一致的,请自行修改。
3.Redis 对象序列化与反序列化
新建一个FastJsonRedisSerializer 类,实现RedisSerializer接口:
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length <= 0) {
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
ParserConfig.getGlobalInstance().setAsmEnable(true);
return JSON.parseObject(str, clazz);
}
使用fastjson去实现序列化的操作。
创建一个配置类,引入当前类加入到Redis配置中
@Configuration
public class RedisConfig {
/**
* 设置 redis 数据默认过期时间
* s设置redis 序列化
* @return
*/
@Bean
public RedisCacheConfiguration redisCacheConfiguration() {
FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
configuration = configuration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(fastJsonRedisSerializer)).entryTtl(Duration.ofDays(30));
return configuration;
}
}
上面的都配置好了之后,接下来也就和上一遍没有什么区别了,启动类开启注解缓存
@SpringBootApplication
@EnableCaching
public class SpringBootRedisIntegratApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRedisIntegratApplication.class, args);
}
}
然后在访问方法的时候,@Cacheable 注解到时候,缓存数据会存入到redis中了
/**
* 数据执行完成后,加入到缓存中,当下一次执行时候,直接从缓存中查询数据,无需查库。
* @param id
* @return
*/
@Override
@Cacheable(cacheNames = "emp",sync = true)
public Employee getByEmployee(Integer id) {
return baseMapper.selectById(id);
}
其他就是自己实现了一些Redis的操作了,还有更多的数据结构的实现,请大家自行去实现,我这里就只实现一部分代码。
@Autowired
StringRedisTemplate stringRedisTemplate;
@Override
public void set(String key, Object value) {
stringRedisTemplate.opsForValue().set(key,value.toString());
}
@Override
public void set(String key, Object value, long var3, TimeUnit var5) {
stringRedisTemplate.opsForValue().set(key,value.toString(),var3,var5);
}
@Override
public Long size(String key) {
return stringRedisTemplate.opsForValue().size(key);
}
@Override
public String get(Object var1) {
return stringRedisTemplate.opsForValue().get(var1);
}
其他好玩的,请自行读文档,调试开发,实例代码在github上面需要的请自行拉取:spring-boot-integrate
然后后续会集成更多的模块进去,需要请点个star。有问题下方讨论一起学习
内容总结
以上是互联网集市为您收集整理的SpringBoot整合Redis 分布式缓存 详解全部内容,希望文章能够帮你解决SpringBoot整合Redis 分布式缓存 详解所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。