springboot 接入redis作为缓存
1.pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
2. 配置
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.EnableCaching;
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.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/**
* redis有三种模式-standalone,sentinel,cluster,
* @EnableCaching 开启缓存
*/
@Configuration
@EnableCaching
public class SpringRedisSessionConfig{
private String redisHost = "127.0.0.1";
private Integer port = 6379;
@Bean("connectionFactory")
public RedisConnectionFactory redisStandaloneMasterConnectionFactory() {
RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration();
redisConfig.setHostName(redisHost);
redisConfig.setPort(port);
redisConfig.setDatabase(0);
RedisConnectionFactory lettuceConnectionFactory = new JedisConnectionFactory(redisConfig);
return lettuceConnectionFactory;
}
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration= RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(1000)); //entryTtl 代表过期时间
return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
// 对象序列化
@Bean
public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory connectionFactory){
RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
//使用Jackson2JsonRedisSerializer替换默认的序列化规则
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的序列化规则
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//设置key的序列化规则
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
/**
* sentinel模式的通道提供
* @return
*/
// @Bean("redisSentinelctionFactory")
// public RedisConnectionFactory redisSentinelctionFactory() {
// RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration()
// .master("mymaster")
// .sentinel("10.70.33.238", 26379)
// .sentinel("10.70.33.239", 26379)
// .sentinel("10.70.33.246", 26379);
// return new JedisConnectionFactory(redisSentinelConfiguration);
// }
/**
* cluster模式的通道提供
* @return
*/
// @Bean("redisRedisClusterFactory")
// public RedisConnectionFactory redisSentinelctionFactory() {
// RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration()
// RedisNode redisNode1 = new RedisNode("127.0.0.1", 6379); // 有多少个节点就写多少个节点
// RedisNode redisNode2 = new RedisNode("127.0.0.1", 6379);
// redisClusterConfiguration.addClusterNode(redisNode1);
// redisClusterConfiguration.addClusterNode(redisNode2);
// return new JedisConnectionFactory(redisClusterConfiguration);
// }
}
3.定义key 生成规则
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.lang.reflect.Method;
/**
* 自定义缓存的 key生成
*/
@Component
public class MyKeyGenerator implements KeyGenerator {
@Override
public Object generate(Object o, Method method, Object... objects) {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(o.getClass().getSimpleName())
.append("_")
.append(method.getName())
.append("_")
.append(StringUtils.arrayToDelimitedString(objects, "_"));
return stringBuffer.toString();
}
}
4. 使用
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
/**
* @Cacheable 加上代表需要缓存的方法
* @CacheEvict 注解代表需要清除缓存
* cacheNames 为缓存的key
*
* 这样使用缺点是 缓存不会自动过期,只有调用 @CacheEvict 的方法才会过期
*
*/
@RequestMapping
@RestController
public class Demo01Controller {
@GetMapping("a")
@Cacheable(cacheNames = "demo01",keyGenerator = "myKeyGenerator")
public String test1() {
System.out.println(LocalDateTime.now() + " 进入了方法 ");
return "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
}
@GetMapping("b")
public String test2() {
System.out.println(LocalDateTime.now() + " 进入了方法 ");
return "bbbbbbbbbbbbbbbbbb";
}
@CacheEvict(cacheNames = "demo01", allEntries = true)
@GetMapping("clear")
public String clear() {
return "清除成功";
}
}
其中key 可以使用el表达式
属性名称 |
描述 |
示例 |
methodName |
当前方法名 |
#root.methodName |
method |
当前方法 |
#root.method.name |
target |
当前被调用的对象 |
#root.target |
targetClass |
当前被调用的对象的class |
#root.targetClass |
args |
当前方法参数组成的数组 |
#root.args[0] |
caches |
当前被调用的方法使用的Cache |
#root.caches[0].name |
例如:
@GetMapping("c")
// @Cacheable(cacheNames = "demo01",key = "#root.method.name")
@Cacheable(cacheNames = "demo01",key = "#user.id.toString().concat(#user.name)") // 支持el表达式,注意不能为空
public String test3(@RequestBody User user) {
System.out.println("user = " + user);
System.out.println(LocalDateTime.now() + " 进入了方法 ");
return "bbbbbbbbbbbbbbbbbb";
}
5. 缓存设置时间
可以在CacheManager 设置过期时间,见上面配置第2点。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了