设为首页 收藏本站
查看: 1146|回复: 0

[经验分享] 170316、spring4:@Cacheable和@CacheEvict实现缓存及集成redis

[复制链接]

尚未签到

发表于 2017-12-21 16:55:35 | 显示全部楼层 |阅读模式
  注:1、放入cache中,采用@Cacheable;使缓存失效@CacheEvict
  2、自定义CacheManager只需要继承org.springframework.cache.support.AbstractCacheManager(该类中的map采用了ConcurrentHashMap,解决了并发等问题,可以自己去看原代码),
  然后需要自己实现loadCaches方法,同时也可以实现getCaches方法(如下面代码)
  3、@Cacheable和@CacheEvict中对应的value为为spring配置文件中配置对应,key为放入缓存中的key
  key的常用写法:一、直接写死,这种方法比较适合固定唯一的key
  如:@Cacheable(value="indexCache", key="'web_index_adv_top'")
  二、动态,带参数(注意不支持Constants),下面列子中的#这些都是传入的参数,condition是条件(例子中是加入缓存的条件)
  如:@Cacheable(value="indexCache", key="'web_index_goods_'+#columnId+'_' + #param.page +'_' + #param.size", condition="#param.page <=2")
  public SearchResult<IndexGoodsBean> queryByParam(long columnId, PageParam param) {
  4、注意@Cacheable是把方法的结果放入了缓存;@CacheEvict是将key从缓存中移除(注意这里移除的理解)
  实现步骤:
  一、自定义CacheManager
  二、spring配置文件(CacheManager和caches,集成redis,cache可以多个;注意这里的caches对应于@Cacheable和@CacheEvict中对应的value)
  三、CacheService(也可以采用service,我这里分成了好几个项目,有parent、support、core、web、admin,core中写的是共用dao和共用的service,所以我的结构是web中的service调用web中的cacheService,然后web中的cacheService调用core的service,缓存是在web中的cacheService中实现)
  四、@Cacheable和@CacheEvict注解写完整
  下面来看完整实现
  一、自定义CacheManager(我这里提供缓存的是redis)
  import java.util.Collection;
  import org.springframework.cache.Cache;
  import org.springframework.cache.support.AbstractCacheManager;

  public>  private Collection<Cache> caches;
  @Override
  protected Collection<? extends Cache> loadCaches() {
  return this.caches;
  }
  public Collection<Cache> getCaches() {
  return caches;
  }
  public void setCaches(Collection<Cache> caches) {
  this.caches = caches;
  }
  }
  二、spring配置文件(配置cacheManager,caches,集成redis)
  <cache:annotation-driven />
  <!--这里的class就是上面定义的cacheManager-->
  <bean>
  <property name="caches">
  <set>
  <ref bean="indexCache"/>
  </set>
  </property>
  </bean>
  <bean>
  <property name="name" value="indexCache" />
  <!--注入org.springframework.data.redis.core.StringRedisTemplate,这种写法很奇怪吧,你可以手动把这个类配置进也可以-->
  <property name="stringRedisTemplate" ref="stringRedisTemplate" />
  <property name="expireTime" value="300" />
  </bean>
  三、
  import org.springframework.cache.annotation.Cacheable;
  @Service

  public>  @Autowired
  private AdvColumnService advColumnService;
  @Autowired
  private AdvService advService;
  /**顶部广告位*/
  public AdvColumnBean getAdvColumnTop() {
  return advColumnService.getByPosition(AdvColumnBean.POSTION_TOP_VALUE);
  }
  /**
  * 头部广告位
  * 广告位中的广告
  */
  public List<AdvBean> advInpire(long columnId) {
  List<AdvBean> list = CopyUtil.copyList(advService.getByColumnIdInpire(columnId), AdvBean.class);
  Collections.sort(list, new AdvComparator());
  return list;
  }

  private static>  @Override
  public int compare(AdvBean arg0, AdvBean arg1) {
  if(null == arg0 || null == arg1){
  return 0;
  }
  if(arg0.getSortIndex() > arg1.getSortIndex()){
  return 1;
  }
  if(arg0.getSortIndex() < arg1.getSortIndex()){
  return -1;
  }
  return 0;
  }
  }
  //我这里的key是可以固定的,如果不是固定的可以使用@Cacheable的语法来使用
  @Override
  @Cacheable(value="indexCache", key="'web_index_adv_top'")
  public List<AdvBean> advTop() {
  if(getAdvColumnTop() != null) {
  return advInpire(getAdvColumnTop().getId());
  }
  return new ArrayList<AdvBean>();
  }
  }
  //使缓存失效@CacheEvict
  import org.springframework.cache.annotation.CacheEvict;
  @Service

  public>  @Autowired
  private AdvService advService;
  @Override
  @CacheEvict(value="indexCache", key="'web_index_adv_top'")
  public boolean updateOne(AdvBean bean) {
  return advService.updateOne(bean);
  }
  @Override
  @CacheEvict(value="indexCache", key="'web_index_adv_top'")
  public long save(AdvBean bean) {
  return advService.save(bean);
  }
  @Override
  @CacheEvict(value="indexCache", key="'web_index_adv_top'")

  public boolean deleteByIds(long[]>  return advService.deleteByIds(ids);
  }
  }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426549-1-1.html 上篇帖子: SessionStateMode之Redis共享session 下篇帖子: java SSM 框架 代码生成器 websocket即时通讯 shiro redis
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表