注: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 class RedisCacheManager extends AbstractCacheManager {

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 id="cacheManager" class="com.xxx.core.aop.RedisCacheManager">

<property name="caches">

<set>

<ref bean="indexCache"/>

</set>

</property>

</bean>

<bean id="indexCache" class="com.xxx.core.aop.RedisCache">

<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 class IndexAdvWebCacheServiceImpl implements IndexAdvWebCacheService{

@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 class AdvComparator implements Comparator<AdvBean> {

@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 class AdvAdminCacheServiceImpl implements AdvAdminCacheService{

@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[] ids) {

return advService.deleteByIds(ids);

}

}

170316、spring4:@Cacheable和@CacheEvict实现缓存及集成redis的更多相关文章

  1. Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用(转)

    原文地址:https://www.cnblogs.com/fashflying/p/6908028.html 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对 ...

  2. Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  3. 缓存注解@Cacheable、@CacheEvict、@CachePut使用及注解失效时间

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  4. Spring Boot缓存注解@Cacheable、@CacheEvict、@CachePut使用

    从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该 ...

  5. Spring4.1新特性——Spring缓存框架增强(转)

    目录 Spring4.1新特性——综述 Spring4.1新特性——Spring核心部分及其他 Spring4.1新特性——Spring缓存框架增强 Spring4.1新特性——异步调用和事件机制的异 ...

  6. 8 -- 深入使用Spring -- 5...3 使用@CacheEvict清除缓存

    8.5.3 使用@CacheEvict清除缓存 被@CacheEvict注解修饰的方法可用于清除缓存,使用@CacheEvict注解时可指定如下属性: ⊙ value : 必须属性.用于指定该方法用于 ...

  7. SpringBoot集成Redis分布式锁以及Redis缓存

    https://blog.csdn.net/qq_26525215/article/details/79182687 集成Redis 首先在pom.xml中加入需要的redis依赖和缓存依赖 < ...

  8. SpringBoot集成Redis来实现缓存技术方案

    概述 在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. ...

  9. (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...

随机推荐

  1. unity, mono断点

    在unity编辑器中点运行后,如果直接在mono中打断点是不起作用的,需要再点击mono的run按钮,此时弹出Attach to Process对话框,如图: 选中其中的Unity Editor (U ...

  2. 用注册表更改DNS的代码分享

    用注册表更改DNS,1秒切换完毕,快速又方便,不用麻烦的去等待了,支持远程路劲运行 最进我这里DNS老是间歇性掉,很不稳定,广州地区,如果你的DNS经常需要更换,试试这个批处理, 论坛很多人发过了更改 ...

  3. asp.net mvc部署iis常见问题

    1.Q:iis比网上的少很多选项 A:iis没装全,去控制面板里把没勾选的选项勾选 2.Q:发布mvc遇到的HTTP错误 403.14-Forbidden解决办法 A:需要在web.config里添加 ...

  4. vim手记

    1.normal 模式进入edit模式 i(a,o),进入 command 模式 :,回到normal模式Esc(ctrl+c,ctrl+[)2.help urs_toc 进入帮助文档目录,退出目录: ...

  5. storyboard三种sugue 和 跳转场景的三种方式 以及控制器之间的传值

    Storyboard引入了2个概念:1. scene:一个场景,由一个viewController和相关的xib表示. 2. segue:在这是用于连接scenes,其有多种类型,iphone包括:P ...

  6. (3)FluidMoveBehavior 之模仿 Windows Phone 开始菜单的 Tile 长按后排序

    这个工程和上一篇 (2)中介绍的排序大同小异,只是比上一篇交换复杂一点,不是通过单击进行交换, 而是拖动一个 Tile 到另一个 Tile 上时,判断两个 Tile 的中心距离是否符合条件来判断是否进 ...

  7. java ssm框架入门(二)添加语言滤器

    使用过滤器是在web.xml中使用filter,以下是码过滤器,过滤所有资源的使用 web.xml <filter> <filter-name>setCharactor< ...

  8. [内核]Linux UserSpace和Kernel之间如何联系

    转自:http://blog.csdn.net/dreaming_my_dreams/article/details/8272586 应用层和驱动的衔接,一直是一个老大难问题,若弄不清楚,总觉得驱动写 ...

  9. 素数 + 背包 - SGU 116. Index of super-prime

    Index of super-prime Problem's Link Mean: 如果一个素数所在的位置还是素数,那么这个素数就是超级素数,比如3在第2位置,那么3就是超级素数. 现在给你一个数,求 ...

  10. IOC控制反转

    IOC是Inversion of Control的缩写,多数书籍翻译成“控制反转”,还有些书籍翻译成为“控制反向”或者“控制倒置”.     1996年,Michael Mattson在一篇有关探讨面 ...