(十二)SpringBoot 2.0 Redisキャッシュ・テクノロジーの統合
15703 ワード
文書ディレクトリ
一.プロジェクト統合
1.pomファイル依存
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- redis lettuce commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
2.redis構成クラス
/**
* @author: hejr
* @desc: Redis
* @date: 2019/4/9 16:26
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// Jackson2JsonRedisSerialize jdkSerializeable
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// value key
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
3. application.xml構成
server:
port: 8096
# Redis
spring:
redis:
database: 0
host: 127.0.0.1
jedis:
pool:
# , 0
max-active: 8
# , 0
max-idle: 8
# 。 。 -1 。
max-wait: -1ms
# , 0
min-idle: 0
lettuce:
pool:
max-active: 8
max-idle: 8
max-wait: -1ms
min-idle: 0
shutdown-timeout: 100ms
password: ''
port: 6379
4.redisUtilツールクラスの新規作成
/**
* @author: hejr
* @desc: Redis
* @date: 2019/4/9 16:30
*/
@Component
public class RedisUtil {
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
/**
* KEY
* @param key KEY
* @param value
* @param expire
* @param timeUnit TimeUnit
*/
public void addKey(String key, Object value, long expire, TimeUnit timeUnit) {
this.redisTemplate.opsForValue().set(key, value, expire, timeUnit);
}
/**
* KEY VALUE
*
* @param key
* @return value
*/
public Object getValue(String key) {
return this.redisTemplate.opsForValue().get(key);
}
}
二.デモ
1.ユニットテストクラス
/**
* @author: hejr
* @desc:
* @date: 2019/4/9 16:35
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisUtilTest {
@Autowired
private RedisUtil redisUtil;
@Test
public void addKey() {
this.redisUtil.addKey("userName", " ", 60, TimeUnit.SECONDS);
System.out.println(" ");
}
@Test
public void getKey() {
String value1 = (String) this.redisUtil.getValue("userName");
System.out.println(" key userName :" + value1);
}
}
三.ソースのダウンロード
https://gitee.com/hejr.cn.com/SpringBoot2.0_2019/tree/master/springboot2_007
上一篇:(十三)SpringBoot 2.0熱配置Devtools原理
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- redis lettuce commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
/**
* @author: hejr
* @desc: Redis
* @date: 2019/4/9 16:26
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// Jackson2JsonRedisSerialize jdkSerializeable
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// value key
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
server:
port: 8096
# Redis
spring:
redis:
database: 0
host: 127.0.0.1
jedis:
pool:
# , 0
max-active: 8
# , 0
max-idle: 8
# 。 。 -1 。
max-wait: -1ms
# , 0
min-idle: 0
lettuce:
pool:
max-active: 8
max-idle: 8
max-wait: -1ms
min-idle: 0
shutdown-timeout: 100ms
password: ''
port: 6379
/**
* @author: hejr
* @desc: Redis
* @date: 2019/4/9 16:30
*/
@Component
public class RedisUtil {
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
/**
* KEY
* @param key KEY
* @param value
* @param expire
* @param timeUnit TimeUnit
*/
public void addKey(String key, Object value, long expire, TimeUnit timeUnit) {
this.redisTemplate.opsForValue().set(key, value, expire, timeUnit);
}
/**
* KEY VALUE
*
* @param key
* @return value
*/
public Object getValue(String key) {
return this.redisTemplate.opsForValue().get(key);
}
}
1.ユニットテストクラス
/**
* @author: hejr
* @desc:
* @date: 2019/4/9 16:35
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisUtilTest {
@Autowired
private RedisUtil redisUtil;
@Test
public void addKey() {
this.redisUtil.addKey("userName", " ", 60, TimeUnit.SECONDS);
System.out.println(" ");
}
@Test
public void getKey() {
String value1 = (String) this.redisUtil.getValue("userName");
System.out.println(" key userName :" + value1);
}
}