现在随着业务的增大,单纯的依赖数据库已经不能满足快速的I/O读写了,所以现在很多项目都需要使用缓存,而对于缓存的使用,目前使用最广泛的是redis。
现在简单记录下springboot集成redis的过程。很简单的。
看下pom.xml文件
<!-- Redis的依赖库 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- springboot启动核心类 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- 使用thymeleaf模板引擎-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 使用web模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
其实前两个足以,但是因为我个人这个例子还使用了thymeleaf,所以需要后两个。
然后在配置文件中配置redis信息
spring:
redis:
#服务器地址
host: 127.0.0.1
#redis密码默认为空
password:
#服务器连接端口
port: 6379
#链接池
pool:
#最大连接数(负值表示没有限制)
max-active: 8
#最大阻塞等待时间(负值表示没有限制)
max-wait: 1
#最大空闲链接
max-idle: 8
#最小空闲链接
min-idle: 0
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML5
encoding: utf-8
servlet:
content-type: text/html
cache: false
然后在启动类中配置如下
package com.zjg.smsrt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
//使用缓存
@EnableCaching
@SpringBootApplication
public class SmsrtApplication {
public static void main(String[] args) {
SpringApplication.run(SmsrtApplication.class, args);
}
}
然后配置redis配置类
package com.zjg.smsrt.util;
import java.util.concurrent.CountDownLatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
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.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
/**
* reids配置
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(listenerAdapter, new PatternTopic("chat"));
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
@Bean
Receiver receiver(CountDownLatch latch) {
return new Receiver(latch);
}
@Bean
CountDownLatch latch() {
return new CountDownLatch(1);
}
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
public class Receiver {
private CountDownLatch latch;
@Autowired
public Receiver(CountDownLatch latch) {
this.latch = latch;
}
public void receiveMessage(String message) {
latch.countDown();
}
}
}
然后所有的配置就完成了,就这么简单。
书写一个测试例子
package com.zjg.smsrt.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zjg
* @date 2018/7/8 20:14
* @Description
*/
@RestController
public class LoginController {
@Autowired
private StringRedisTemplate sRT;
//添加修改缓存
@GetMapping("/setValue")
public String setValue (String key) {
//判断是否已存在
if (!sRT.hasKey(key)) {
//添加进缓存,追加
sRT.opsForValue().append("key",key);
return "使用redis缓存数据成功";
} else {
//如果存在则修改缓存内容
sRT.opsForValue().set("key",key);
return "数据存在并已更新";
}
}
//得到缓存
@GetMapping("/getValue")
public String getValue () {
if (!sRT.hasKey("key")) {
return "数据不存在";
} else {
return sRT.opsForValue().get("key");
}
}
//删除缓存
@GetMapping("/deleteValue")
public String deleteValue () {
sRT.delete("key");
return "删除key缓存成功";
}
}
访问http://127.0.0.1:8080/setValue?key=123
发现可以正常使用。springboot集成redis就完成了