pom
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!--redis-->
配置
package com.practice.app.config;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
@Configuration
public class RedisConfig {
@Bean(name = "redisTemplate")
public RedisTemplate<Object, Object> getRedisTemplate(RedisConnectionFactory connectionFactory) {
// 这个connectionFactory的Bean是哪里定义的:boot帮忙创建的
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
// key序列化:String~
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
// value序列化:obj2json
GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
redisTemplate.setValueSerializer(jsonRedisSerializer);
redisTemplate.setHashValueSerializer(jsonRedisSerializer);
// return Bean
return redisTemplate;
}
/*
* 默认健的过期时间为 180 秒
*
* @param connectionFactory
* @return
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
return new RedisCacheManager(redisCacheWriter, this.getRedisCacheConfigurationWithTtl(180));
}
/*
* 设置一般key的过期时间
*
* @param seconds
* @return
*/
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
return RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())).entryTtl(Duration.ofSeconds(seconds));
}
}
代码
controller
package com.practice.app.controller;
import com.practice.service.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Map;
@RestController
@RequestMapping("/hello")
public class HelloController {
@Resource
private HelloService helloService;
@GetMapping("/h1")
public String h1(@RequestParam Map<String, String> params) {
return helloService.h1();
}
@GetMapping("/h2")
public String h2(@RequestParam Map<String, String> params) {
return helloService.h2();
}
}
service
package com.practice.service.impl;
import com.practice.service.HelloService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class HelloServiceImpl implements HelloService {
private static final String APP_KEY = "APP";
@Override
public String h1() {
return "h1";
}
@Override
@Cacheable(value = APP_KEY, key = "#root.targetClass+':'+#root.method")
public String h2() {
return "h2";
}
}
效果
![](https://i-blog.csdnimg.cn/blog_migrate/f59bb8a1f13e6063ddd9dbf07d8c8c0b.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6af356f9fceeeb99ba7583d3215a00c0.png)