@EnableCaching注解是spring framework中的注解驱动的缓存管理功能。自spring版本3.1起加入了该注解。如果你使用了这个注解,那么你就不需要在XML文件中配置cache manager了。文章来源:https://www.toymoban.com/news/detail-547875.html
当你在配置类(@Configuration)上使用@EnableCaching注解时,会触发一个post processor,这会扫描每一个spring bean,查看是否已经存在注解对应的缓存。如果找到了,就会自动创建一个代理拦截方法调用,使用缓存的bean执行处理。文章来源地址https://www.toymoban.com/news/detail-547875.html
@EnableCaching
@Configuration
public class RedisConfig {
/**
* @Cacheable注解不支持配置过期时间,所有需要通过配置CacheManneg来配置默认的过期时间和针对每个类或者是方法进行缓存失效时间配置。
* @param redisConnectionFactory
* @return
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
return new RedisCacheManager(
RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
this.getRedisCacheConfigurationWithTtl(600), // 默认策略,未配置的 key 会使用这个
this.getRedisCacheConfigurationMap() // 指定 key 策略
);
}
private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
redisCacheConfigurationMap.put("wxcl", this.getRedisCacheConfigurationWithTtl(60*60*24*7));
//redisCacheConfigurationMap.put("UserInfoListAnother", this.getRedisCacheConfigurationWithTtl(18000));
return redisCacheConfigurationMap;
}
private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer seconds) {
StringRedisSerializer keyStringRedisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
RedisSerializationContext
.SerializationPair
.fromSerializer(jackson2JsonRedisSerializer)
).entryTtl(Duration.ofSeconds(seconds));
redisCacheConfiguration.serializeKeysWith(
RedisSerializationContext
.SerializationPair
.fromSerializer(keyStringRedisSerializer)
);
return redisCacheConfiguration;
}
}
@Slf4j
@Service("myUserCacheService")
public class MyUserCacheService {
/**
* sso登录用户获取当前用户
* @param token token
* @param user 获取默认null
*/
@Cacheable(
value = "iotgw",
key = "'user'+#token",
unless = "#result eq null")
public Map<String, String> getUserFromRedis(String token, Map<String, String> user) {
log.info("从缓存中获取sso登录人信息失败 {},准备写入缓存", user);
return user;
}
/**
* 获取企业信息
* @param token 存储关键值
* @param company 企业信息
*/
@Cacheable(
value = "devicecenter",
key = "'company'+#token",
unless = "#result eq null")
public Map<String, String> getCompanyFromRedis(String token, Map<String, String> company) {
log.info("从缓存中获取登录人信息失败 {},准备写入缓存", company);
return company;
}
/**
* 企业信息缓存变更
* @param token 存储关键值
* @param company 企业信息
*/
@CachePut(
value = "devicecenter",
key = "'company'+#token",
unless = "#result eq null")
public Map<String, String> setCompanyFromRedis(String token, Map<String, String> company) {
log.info("从缓存中获取登录人信息失败 {},准备写入缓存", company);
return company;
}
/**
* 后台登录用户获取当前用户
*/
@Cacheable(
value = "iotmgw",
key = "'user'+#userId",
unless = "#result eq null")
public Map<String, String> getManagerUserFromRedis(String userId, Map<String, String> user) {
log.info("从缓存中获取后台登录人信息失败 {},准备写入缓存", user);
return user;
}
}
到了这里,关于@EnableCaching @Cacheable @CachePut redis注解缓存的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!