@EnableCaching注解是spring framework中的注解驅(qū)動(dòng)的緩存管理功能。自spring版本3.1起加入了該注解。如果你使用了這個(gè)注解,那么你就不需要在XML文件中配置cache manager了。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-547875.html
當(dāng)你在配置類(@Configuration)上使用@EnableCaching注解時(shí),會(huì)觸發(fā)一個(gè)post processor,這會(huì)掃描每一個(gè)spring bean,查看是否已經(jīng)存在注解對(duì)應(yīng)的緩存。如果找到了,就會(huì)自動(dòng)創(chuàng)建一個(gè)代理攔截方法調(diào)用,使用緩存的bean執(zhí)行處理。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-547875.html
@EnableCaching
@Configuration
public class RedisConfig {
/**
* @Cacheable注解不支持配置過(guò)期時(shí)間,所有需要通過(guò)配置CacheManneg來(lái)配置默認(rèn)的過(guò)期時(shí)間和針對(duì)每個(gè)類或者是方法進(jìn)行緩存失效時(shí)間配置。
* @param redisConnectionFactory
* @return
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
return new RedisCacheManager(
RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory),
this.getRedisCacheConfigurationWithTtl(600), // 默認(rèn)策略,未配置的 key 會(huì)使用這個(gè)
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登錄用戶獲取當(dāng)前用戶
* @param token token
* @param user 獲取默認(rèn)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登錄人信息失敗 {},準(zhǔn)備寫(xiě)入緩存", user);
return user;
}
/**
* 獲取企業(yè)信息
* @param token 存儲(chǔ)關(guān)鍵值
* @param company 企業(yè)信息
*/
@Cacheable(
value = "devicecenter",
key = "'company'+#token",
unless = "#result eq null")
public Map<String, String> getCompanyFromRedis(String token, Map<String, String> company) {
log.info("從緩存中獲取登錄人信息失敗 {},準(zhǔn)備寫(xiě)入緩存", company);
return company;
}
/**
* 企業(yè)信息緩存變更
* @param token 存儲(chǔ)關(guān)鍵值
* @param company 企業(yè)信息
*/
@CachePut(
value = "devicecenter",
key = "'company'+#token",
unless = "#result eq null")
public Map<String, String> setCompanyFromRedis(String token, Map<String, String> company) {
log.info("從緩存中獲取登錄人信息失敗 {},準(zhǔn)備寫(xiě)入緩存", company);
return company;
}
/**
* 后臺(tái)登錄用戶獲取當(dāng)前用戶
*/
@Cacheable(
value = "iotmgw",
key = "'user'+#userId",
unless = "#result eq null")
public Map<String, String> getManagerUserFromRedis(String userId, Map<String, String> user) {
log.info("從緩存中獲取后臺(tái)登錄人信息失敗 {},準(zhǔn)備寫(xiě)入緩存", user);
return user;
}
}
到了這里,關(guān)于@EnableCaching @Cacheable @CachePut redis注解緩存的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!