SpringBoot整合Redis之使用@Cacheable注解

@Cacheable实现自动缓存,属性为value、key和condition:

参数作用
value缓存的名称
key缓存的 key, SpEL 表达式
condition缓存的条件

本文环境为SpringBoot2.X,以下为使用过程及个人理解:

添加依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
	<exclusions>
		<exclusion>
			<groupId>io.lettuce</groupId>
			<artifactId>lettuce-core</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
</dependency>

SpringBoot2.X使用lettuce,但是个人习惯加上其他原因所以换成了jedis。

配置文件配置

redis: 
    port: 6379
    jedis:
      pool:
        max-active: 500
        max-wait: 50000
        max-idle: 500
        min-idle: 0
    timeout: 50000
    host: www.baidu.com
  cache:
    type: redis #缓存类型
    redis:
      cache-null-values: false #不缓存null数据
      time-to-live: 50000ms #超时时间
      use-key-prefix: false #不使用前缀

host换成你自己的IP即可

配置文件设置(敲黑板)

  • RedisConstant.java

    public class RedisConstant {
    	/**
    	 * 数据自增步长为1
    	 */
    	public static final Integer INCREMENT = 1;
    	/**
    	 * 数据自减步长为1
    	 */
    	public static final Integer REDUCTION = -1;
    	/**
    	 * 存储用户信息前缀
    	 */
    	public static final String USER = "user:";
    }
    

鉴于篇幅,此处只放USER一个常量,这个常量类的作用:全局使用,一改全改,注意,如果你不想麻烦就一定要将USER常量的:留着,对于后续操作缓存提供了太多的方便!!

  • RedisConfig.java
    配置类,大用!即使不使用@Cacheable这个类也该有

    package org.config;
    
    import java.sql.SQLException;
    import java.time.Duration;
    import java.util.HashMap;
    import java.util.Map;
    import javax.sql.DataSource;
    import org.constant.RedisConstant;
    import org.springframework.cache.CacheManager;
    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.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.Jackson2JsonRedisSerializer;
    import org.springframework.data.redis.serializer.RedisSerializationContext;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.transaction.PlatformTransactionManager;
    import com.fasterxml.jackson.annotation.JsonAutoDetect;
    import com.fasterxml.jackson.annotation.PropertyAccessor;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    /**
     * 缓存Redis配置
     */
    @Configuration
    @EnableCaching
    public class RedisConfig extends CachingConfigurerSupport {
    
    	/**
    	 * 使用Jackson2JsonRedisSerialize 替换默认序列化
    	 */
    	@Bean
    	public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
    		RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
    		// 使用Jackson2JsonRedisSerialize 替换默认序列化
    		Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
    				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);
    		// hash参数序列化方式
    		redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    		redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
    		// 缓存支持回滚(事务管理)
    		redisTemplate.setEnableTransactionSupport(true);
    		redisTemplate.setConnectionFactory(redisConnectionFactory);
    		redisTemplate.afterPropertiesSet();
    		return redisTemplate;
    	}
    
    	// 配置事务管理器
    	@Bean
    	public PlatformTransactionManager transactionManager(DataSource dataSource) throws SQLException {
    		return new DataSourceTransactionManager(dataSource);
    	}
    
    	/**
    	 * 配置自动化缓存使用的序列化方式以及过期时间
    	 */
    	@Bean
    	public RedisCacheConfiguration redisCacheConfiguration() {
    		Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
    				Object.class);
    		ObjectMapper objectMapper = new ObjectMapper();
    		objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    		objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    		jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    		RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig();
    		configuration = configuration
    				.serializeValuesWith(
    						RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
    				.entryTtl(Duration.ofDays(1));
    		return configuration;
    	}
    
    	/**
    	 * 缓存过期时间自定义配置
    	 */
    	@Bean
    	public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    		// 设置CacheManager的值序列化方式为Jackson2JsonRedisSerializer,默认就是使用StringRedisSerializer序列化key,JdkSerializationRedisSerializer序列化value
    		Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
    				Object.class);
    		ObjectMapper objectMapper = new ObjectMapper();
    		objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    		objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    		jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    		// 配置value序列化方式为Jackson2JsonRedisSerializer,key序列化方式采用默认的StringRedisSerializer
    		RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(
    				RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));
    		// 每一类信息进行缓存配置
    		Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
    		// 用户信息
    		redisCacheConfigurationMap.put(RedisConstant.USER, cacheConfiguration.entryTtl(Duration.ofDays(10))
    				.disableCachingNullValues().prefixKeysWith(RedisConstant.USER));
    	
    		// 初始化一个RedisCacheWriter
    		RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory);
    		RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(
    				RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));
    		// 设置默认超过期时间是1天(短时间的已经做了其他的处理,不会采用注解形式加入缓存)
    		defaultCacheConfig.entryTtl(Duration.ofDays(1));
    		// 初始化RedisCacheManager
    		RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig,
    				redisCacheConfigurationMap);
    		return cacheManager;
    	}
    }
    

嗯,redisCacheConfiguration是我看的别人的配置抄的,设置默认的过期时间为1天
重点是cacheManager方法:在本文中我写了两个方法,但是在实际开发中你可以根据RedisConstant里面常量的数量及需求自行增加,这样就不需要在方法上面修改了(万一一个key在多处调用岂不是蛋疼?),而且我是为了省事所以所有的都使用的Jackson2JsonRedisSerializer序列化方式,但是你可以根据自己的需要设置每一类的序列化方式。

注意:

上述配置中.disableCachingNullValues()配置要根据个人系统决定!此配置会导致@Cacheable如果返回null会报错,缓存拒绝存储null值:

java.lang.IllegalArgumentException: Cache 'user:' does not allow 'null' values. Avoid storing null via '@Cacheable(unless="#result == null")' or configure RedisCache to allow 'null' via RedisCacheConfiguration.
at org.springframework.data.redis.cache.RedisCache.put(RedisCache.java:139)

不建议进行上述配置,一方面报错很难受,另一方面数据如果从数据库中查不到大概率数据库中根本就不存在此条记录,所以让此后的数据继续去查询数据库没什么意义,还不如在缓存中放置一个null值的数据,指定过期时间即可。
但是有一些特殊的数据需要进行上述设置,这个根据个人系统决定。

开始使用

我一般使用@Cacheable注解的时候会在service和mapper层之间再加一层操作,当然你可以直接使用在service层或者dao层的接口上,这个随意。

@Override
@Cacheable(value = RedisConstant.USER, key = "#id", condition = "#bool==true", unless = "#result == null")
public UserShow getUser(String id, boolean bool) {
	return userMapper.userShow(id);
}

上面这一种存储到缓存里面key的形式为:user:userId,其中userId为方法参数id的值,绝大多数情况下建议使用keyGenerator,编译过程中可能会解析不到参数名,导致缓存时key拼接错误。

@Override
@Cacheable(value = RedisConstant.CATEGORY, key = "#root.methodName", unless = "#result == null")
public Object hot() {
	return kingdomMapper.selectWithScore();
}

上面这一种存储到缓存里面key的形式为:category:hot,其中hot的来源是方法名,这个涉及到key的相关取值,本文不介绍了。百度一大堆,但是其他的本人没用过。。。
下面来说一下@Cacheable注解的好处:

  • redisTemplate.opsForValue().get(key)方法不用写了,直接避免存数据和取数据时数据类型不一致情况的出现
  • 缓存为空判断不需要做了,省下大堆代码
  • 缓存有效期设置可以在配置文件中实现,具体人员只管使用,防止某类数据缓存时间变化时需要改动多处代码

下面说一下我在配置过程中遇到的坑,希望不要再犯:

  • 最初的时候我在RedisConfig配置文件中未配置cacheManager方法,同时RedisConstant常量类参数也没有加:,导致的情况是我在用Redis可视化工具查看数据的时候userid之间有两个:,虽然不影响使用,但是看着蛋疼啊!!
  • 当我配置了cacheManager方法之后,测试又发现userid之间没有:符号了,这不更是蛋疼吗?(如果某一天user展示数据突然要求加一个字段,难道我使用keys命令查询再删除?要知道生产环境明令禁止使用keys命令啊!)
  • 最终测试的结果是我前面的配置个人感觉更合适,使一类数据放到一组,方便管理。

如有高见敬请指出,在线更新。。

  • 5
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个使用Spring Boot整合Redis的`@Cacheable`、`@CachePut`、`@CacheEvict`注解实现增删改查的示例: 1. 添加Redis依赖 在pom.xml中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> ``` 2. 配置Redis连接 在application.properties中添加以下配置: ```properties # Redis连接配置 spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= # 开启缓存 spring.cache.type=redis ``` 3. 编写Redis配置类 ```java import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; 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.RedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import java.lang.reflect.Method; import java.util.Arrays; @Configuration @EnableCaching public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); // key序列化方式 RedisSerializer<String> stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringRedisSerializer); // key redisTemplate.setHashKeySerializer(stringRedisSerializer); // hash key // value序列化方式 RedisSerializer<Object> valueRedisSerializer = new GenericJackson2JsonRedisSerializer(); redisTemplate.setValueSerializer(valueRedisSerializer); // value redisTemplate.setHashValueSerializer(valueRedisSerializer); // hash value redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory); return builder.build(); } @Bean public KeyGenerator wiselyKeyGenerator() { return (Object target, Method method, Object... params) -> { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); Arrays.stream(params).forEach(param -> sb.append(param.toString())); return sb.toString(); }; } } ``` 4. 编写实体类 ```java import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 1L; private Integer id; private String name; private Integer age; public User(Integer id, String name, Integer age) { this.id = id; this.name = name; this.age = age; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } ``` 5. 编写Service ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserDao userDao; @Cacheable(value = "user", keyGenerator = "wiselyKeyGenerator") public User getUserById(Integer id) { return userDao.getUserById(id); } @CachePut(value = "user", keyGenerator = "wiselyKeyGenerator") public User updateUser(User user) { userDao.updateUser(user); return user; } @CacheEvict(value = "user", keyGenerator = "wiselyKeyGenerator") public void deleteUserById(Integer id) { userDao.deleteUserById(id); } } ``` 6. 编写Controller ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public User getUserById(@PathVariable Integer id) { return userService.getUserById(id); } @PutMapping("") public User updateUser(@RequestBody User user) { return userService.updateUser(user); } @DeleteMapping("/{id}") public void deleteUserById(@PathVariable Integer id) { userService.deleteUserById(id); } } ``` 7. 测试 启动应用后,可以通过以下方式测试: - 获取用户:GET http://localhost:8080/user/1 - 更新用户:PUT http://localhost:8080/user 请求体: ```json { "id": 1, "name": "Tom", "age": 20 } ``` - 删除用户:DELETE http://localhost:8080/user/1 以上就是一个使用Spring Boot整合Redis的`@Cacheable`、`@CachePut`、`@CacheEvict`注解实现增删改查的示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值