JCache只缓存部分字段
作者:互联网
项目中使用的JCache缓存实体,发现每次缓存时存进去了实体,取出的时候字段有些是空的。
具体环境为
Springboot v2.01
JCache(ehcache 3.4.0)
jdk 1.8.0_201
maven 3.6.0
最开始以为是取的时候无法取出父类的字段(实体继承了一个父类,空的字段都是父类的字段,所以定位到取的问题)。
1 @MappedSuperclass 2 @Data 3 //@Entity 4 public class AlertRuleSuperBean { 5 /** 6 * 报警规则主键 7 */ 8 @Id 9 @GeneratedValue(strategy = GenerationType.IDENTITY) 10 @Column(name = "alert_rule_id") 11 protected Integer alertRuleId; 12 }
决定手动赋值
1 @Service 2 @EnableCaching 3 public class CacheServiceImpl implements CacheService { 4 5 @Override 6 @Cacheable(key = "#key",cacheNames = "dtuRule") 7 public AlertRuleProjectBean doDtuRule(String key, AlertRuleProjectBean bean) { 8 System.out.println("I am a doDtuRule!key is "+ key); 9 //只缓存子类bug,所以手动赋值 10 AlertRuleProjectBean resultBean=new AlertRuleProjectBean(); 11 resultBean.setDtuEventTimestamp(bean.getDtuEventTimestamp()); 12 resultBean.setProjectId(bean.getProjectId()); 13 resultBean.setAlertRuleMax(bean.getAlertRuleMax()); 14 resultBean.setAlertRule(bean.getAlertRule()); 15 resultBean.setAlertRuleStatus(bean.getAlertRuleStatus()); 16 resultBean.setAlertRuleMin(bean.getAlertRuleMin()); 17 resultBean.setDtu_data_id(bean.getDtu_data_id()); 18 resultBean.setAlertConditionType(bean.getAlertConditionType()); 19 resultBean.setAlertPriority(bean.getAlertPriority()); 20 resultBean.setAlertRuleId(bean.getAlertRuleId()); 21 resultBean.setAlertRuleName(bean.getAlertRuleName()); 22 resultBean.setSmoothTime(bean.getSmoothTime()); 23 resultBean.setAlertType(bean.getAlertType()); 24 resultBean.setIsProjectUserVisual(bean.getIsProjectUserVisual()); 25 resultBean.setRuleStatus(bean.getRuleStatus()); 26 resultBean.setTimestamp(bean.getTimestamp()); 27 return resultBean; 28 } 29 30 @Override 31 @CacheEvict(key="#key",condition = "#key!='mfb'",cacheNames = "dtuRule",allEntries = false) 32 public void removeDtuRule(String key) { 33 System.out.println("cache removed!key is "+key); 34 35 } 36 }
赋值后仍然是部分为空,完全没有效果!
试着取消继承(将继承的一些父类字段手动copy到子类,取消extend),问题解决!
标签:AlertRuleProjectBean,JCache,缓存,bean,resultBean,key,父类,部分,public 来源: https://www.cnblogs.com/dzxw2371/p/10535039.html