赞
踩
@Bean public RedisCacheManager classifyTreeList1d(RedisConnectionFactory connectionFactory) { //可配置多个 RedisCacheConfiguration config = instanceConfig(3600 * 24 * 1L); //设置过期时间为1天 return RedisCacheManager.builder(connectionFactory) .cacheDefaults(config) .transactionAware() .build(); } private RedisCacheConfiguration instanceConfig(Long ttl) { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.registerModule(new JavaTimeModule()); // 去掉各种@JsonSerialize注解的解析 objectMapper.configure(MapperFeature.USE_ANNOTATIONS, false); // 只针对非空的值进行序列化 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 将类型序列化到属性json字符串中 objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); return RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofSeconds(ttl)) .disableCachingNullValues() .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)); }
使用
@CachePut(value = {"classifyTreeList"}, key = "#classify.getClassifyId()", cacheManager = "classifyTreeList1d")
@Cacheable(value = {"classifyTreeList"}, key = "#queryReq.getParameter()", cacheManager = "classifyTreeList1d")
不删除value下所有数据,而删除value里面某一条数据,通过key字段进行删除
/**
* 前台_分类刷题_查看分类下试题
* @author kfj
* @return ApiResult
*/
@Override
@Cacheable(value = "classifySubjectList", key = "#classifyId", cacheManager = "oneDay")
public ApiResult<List<TotalSubject>> getCountByClassifyId(Long classifyId) {
System.out.println("============================");
QueryWrapper<TotalSubject> queryWrapper = new QueryWrapper<TotalSubject>();
queryWrapper.in("classify_id",classifyId);
List<TotalSubject> totalSubjects = totalSubjectMapper.selectList(queryWrapper); //根据分类id查询所有试题
return ApiResult.success(totalSubjects);
}
/** * 新增保存考试试题总 */ @Override @CacheEvict(value = {"totalSubjectList"}, allEntries=true) public ApiResult totalSubjectSave(TotalSubject totalSubject, UserRoot user) { try { if("1".equals(totalSubject.getIsRelease())){ totalSubject.setReleaseTime(new Date()); } if(totalSubject.getClassifyId() != null){ Classify classify = classifyMapper.selectById(totalSubject.getClassifyId()); totalSubject.setClassifyName(classify.getClassifyName()); } totalSubject.setCreateBy(user.getRealName()); totalSubject.setCreateTime(new Date()); super.save(totalSubject); classifyService.updateClassifyCache(totalSubject.getClassifyId()); return ApiResult.success(); }catch (Exception e){ e.printStackTrace(); return ApiResult.defaultError(null); } } /** * 修改保存考试试题总 */ @Override @CacheEvict(value = {"totalSubjectList"}, allEntries=true) public ApiResult totalSubjectUpdate(TotalSubject totalSubject, UserRoot user) { try { if(totalSubject.getSubjectId() != null) { if("1".equals(totalSubject.getIsRelease())){ totalSubject.setReleaseTime(new Date()); } if(totalSubject.getClassifyId() != null){ Classify classify = classifyMapper.selectById(totalSubject.getClassifyId()); totalSubject.setClassifyName(classify.getClassifyName()); } totalSubject.setUpdateBy(user.getRealName()); totalSubject.setUpdateTime(new Date()); super.update(totalSubject); classifyService.updateClassifyCache(totalSubject.getClassifyId()); return ApiResult.success(); } }catch (Exception e){ e.printStackTrace(); return ApiResult.defaultError(null); } return ApiResult.fail(MsgConstant.EXCEPTION_ID_NULL.getMsg()); }
@CacheEvict(value="classifySubjectList", key = "#classify",allEntries=false)
public void updateClassifyCache(long classify) {
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。