码迷,mamicode.com
首页 > 编程语言 > 详细

SpringCache

时间:2019-06-27 01:06:07      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:ble   接口   col   ati   repo   conf   rop   property   信息   

1.原理图

技术图片

2.SpringCache缓存实现(此项目是通过mybatis-ssm项目复制得来的)

(1).创建一个spring-cache.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache.xsd"> <cache:annotation-driven cache-manager="cacheManager"/><!--需要启用SpringCache--> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"><!--id="news"就是在业务层里面进行缓存具体操作时使用的名称--> <bean id="news" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/> </property> </bean> </beans>

 (2).如果要想让缓存配置生效。则一定要在spring-base.xml配置文件里面追加配置项

<import resource="spring-cache.xml"/><!--将cache配置文件导入spring-base.xml配置文件即可-->

 (3).修改业务层接口。这里以INewsService接口为例

public interface INewsService{//下面的news就是上面的那个"id="news""
    @Cacheable(cacheNames="news")//调用此方法就会触发id缓存
    public News get(long id);或
  
  @Cacheable(cacheNames = "news",condition = "#id<10",unless = "#result==null" )//调用此方法id<10就会触发id缓存,id>10则不再触发缓存

  public News get(long id) ;
  @Cacheable(cacheNames = "news",key = "#id")//调用此方法就会触发id缓存,如果不声明key,则默认为当id和title同时输入时才会触发
   public News get(long id,String title);
   @Cacheable(cacheNames = "news",key = "#id",sync = true)//"sync"为同步标记,此时由于配置了"sync"属性,所以在进行并发访问的时候只会由一个线程负责缓存数据的添加
   public News get(long id,String title);
}

3.缓存更新

1.缓存更新用@CachePut注解控制
2.因为每次注解之前都要注解cacheNames="news",也可以在接口上面直接使用@CacheConfig(cacheNames="news")进行了缓存信息的统一配置处理
3.@CachePut(key="#vo.nid",unless="#result==null")
  public News update(News vo);//一定要返回新的News对象实例

4.缓存清除

缓存清除用@CacheEvict
      public boolean delete(long id);

5.多级缓存如果既想保存id的缓存又想保存title的缓存,这样操作的前提是这两个字段都没有重复的数据内容

@Cacheable(unless="#result==null")
public News get(long id);

@Cacheable(unless="#result==null")
public News get(String title);

修改两个参数的get()方法定义,在此操作之中使用多级缓存设置(当前的操作表示对id和title都进行缓存设置):
@Cacheable(cacheable={
   @Cacheable(key="#id"),   @Cacheable(key="#title")  })
public News get(long id,String title);

在进行缓存更新的时候也可以使用多级缓存设置
@Cacheable(put={
   @Cacheput(key="#vo.nid",unless="#result==null")
   @Cacheput(key="#vo.title",unless="#result==null")
 })
public News update(News vo);

6.SpringCache整合EhCache缓存组件

(1).导入依赖包

<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.10.6</version>
</dependency>

(2).在"src/main/resources/"目录下创建一个ehache.xml配置文件

(3).如果想使用ehcache缓存组件买就必须在spring-cache.xml配置文件中进行缓存的相关定义

辅导班

  

 

 

  

SpringCache

标签:ble   接口   col   ati   repo   conf   rop   property   信息   

原文地址:https://www.cnblogs.com/wxl123/p/11094644.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!