上一節DB已經集成過redis了,就是下面這個
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
今天希望能把redis搞通吧
配置類
package com.jiataoyuan.demo.springboot.config;
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;
import java.util.concurrent.CountDownLatch;
/**
* @author TaoYuan
* @version V1.0.0
* @date 2018/4/18 0018
* @description description
*/
@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();
}
}
}
關于配置類,有時候不一定是必須的,因為有的需要,有時候不需要,加個注解也可以達到通用的效果,就比如上一節的MyBatis就沒寫配置類,所以,如果不想在寫這個配置類的話,也可以直接在application類中加@EnableCaching注解
配置
spring:
thymeleaf:
cache: false
freemarker:
cache: false
groovy:
template.cache: false
# 數據源
datasource:
# 數據庫的URL、帳號、密碼、驅動
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
# 使用druid連接池
type: com.alibaba.druid.pool.DruidDataSource
# druid連接池的配置信息
initialSize: 5
# 最小連接數量
minIdle: 5
# 最大連接數量
maxActive: 20
# 獲取連接等待超時的時間
maxWait: 60000
# 間隔多久進行一次檢測(需要關閉的空閑連接)
timeBetweenEvictionRunsMillis: 30000
# 連接在池中最小生存的時間
minEvictableIdleTimeMillis: 30000
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# Redis 配置
redis:
# Redis數據庫索引(默認為0)
database: 0
# Redis服務器地址,默認localhost
host: 127.0.0.1
# 端口 默認6379
port: 6379
# 密碼 默認空
password:
# 連接超時時間
pool:
# 連接池最大連接數(使用負值表示沒有限制)
max-active: 8
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
max-wait: -1
# 連接池中的最大空閑連接
max-idle: 8
# 連接池中的最小空閑連接
min-idle: 0
timeout: 0
# MyBatis 配置
mybatis:
# mybatis的mapper.xml文件的路徑
mapper-locations: classpath:mapper/*.xml
# mybatis的model所在的包
type-aliases-package: com.jiataoyuan.demo.springboot.model
到這里,整合基本就完成了
如果沒裝redis的話,先裝一下,然后開啟服務
上面是windows版本的,直接解壓即可,建議在C盤單獨建個redis文件夾,然后命令行進入,并執行一下命令,就開啟了
redis-server.exe redis.windows.conf
redis server
然后編寫業務邏輯
@RequestMapping("/setValue")
public String setValue(){
if(!template.hasKey("name")){
template.opsForValue().append("name", "王二小");
return "使用redis緩存保存數據成功";
}else{
template.delete("name");
return "key已存在";
}
}
@RequestMapping("/getValue")
public String getValue(){
if(!template.hasKey("name")){
return "key不存在,請先保存數據";
}else{
String name = template.opsForValue().get("name");//根據key獲取緩存中的val
return "獲取到緩存中的數據:name:"+name;
}
}
redis_get.png
redis_set.png
這就是一個最基本的redis使用案例了
關于redis更多的,可以查查詳細的教程,由于現在主要學SpringBoot,redis就先放放吧。
redis菜鳥教程