spring中的缓存--Caching

1.spring从3.1开始支持缓存功能。spring 自带的缓存机制它只在方法上起做用,对于你使用其余持久化层的框架来说,是没有影响的,相对来说这种缓存方式仍是不错的选择。java

2.提供缓存的接口:org.springframework.cache.Cache ;org.springframework.cache.CacheManager这两个接口都在context中,一个是用来提供缓存的,一个是用来提供管理缓存的。spring

3.缓存是使用键值对的形式存在的,对应Java中就要使用Map<K,V>这种数据结构来处理,推荐使用java.util.concurrent.ConcurrentMap来存放。缓存

4.注解:数据结构

image

在上面的注解中咱们经常使用到的有@Cacheable,@CacheEvict,@CachePut这3个,咱们目前也只学这3个框架

5. 测试

@Cacheable:用来定义缓存的。经常使用到是value,key;分别用来指明缓存的名称和方法中参数,对于value你也可使用cacheName,在查看源代码是咱们能够看到:二者是指的同一个东西。spa

image

@CacheEvict:用来清理缓存。经常使用有cacheNames,allEntries(默认值false);分别表明了要清除的缓存名称和是否所有清除(true表明所有清除)。3d

@CachePut:用来更新缓存,用它来注解的方法都会被执行,执行完后结果被添加到缓存中。该方法不能和@Cacheable同时在同一个方法上使用。code

6.对于@Caching注解来说,若是有两种不一样的需求,都是放在同一个方法上,这种需求若是只是使用@CacheEvict或者@CachePut是没法实现,由于他们不能多样化的做用在同一个方法上。可使用@Caching(evict={@CacheEvict(“a1”),@CacheEvict(“a2”,allEntries=true)});@Caching源代码以下:xml

image

7.下面咱们来看看CacheManager这个接口,源代码以下:

image

不难看出,最终目的仍是用来获取Cache这个对象的,而咱们缓存的数据都放在Cache中,部分源代码以下:

image

8.上面的一些基本的东西都已说完,下面看看怎么配置,让缓存真正的起做用:

来看看官方文档给的写法,里面重要的是spring-cache.xsd

image

其中的注解添加完,你就能够中代码中使用了:

image

在使用spring中缓存时,咱们通常选择SimpleCacheManager这个类。

image

SimpleCacheManager源代码以下:

image

能够看出咱们须要配置caches这个属性,来看看官方文档的例子吧,在xml中添加以下代码:建立了两个缓存的名称一个是books 一个是 default,咱们能够只建立一个。

image

可是set中咱们因该怎么写,在文章开头咱们提到了ConcurrentMap这个类,再看下下面的红框中,咱们选择一个,其中看其源代码可知道ConcurrentMapCacheFactoryBean内容包含其余两个。全部咱们选择这个。

image

其源代码以下:

image

9.如今基本的配置都完成,如今就须要配置本身的bean去作下测试了。

编写一个逻辑处理的类,把该类放在xml定义:

public class TestCacheService {
    //经过参数name
    @Cacheable(cacheNames="uCache",key="#name")
    public User get(String name){
        User u = new User();
        u.setAge(12);
        u.setUserName("jobs");
        System.out.println("没有缓存"+name);
        return u;
    }
    //经过方法名
    @Cacheable(value="uCache",key="#root.methodName")
    public User get2(){
        User u = new User();
        u.setAge(12);
        u.setUserName("gate");
        System.out.println("没有缓存");
        return u;
    }
}

编写一个测试类

public class TestCache {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("config/spring-cache.xml");
        TestCacheService testCache = (TestCacheService)context.getBean("testCache");
        testCache.get("jobs");
        testCache.get("jobs");
    }    
}

运行测试一下,会发现报错,缘由是xml中的p:name没有绑定:

在xml中添加:xmlns:p="http://www.springframework.org/schema/p"就可。

测试结果只有一行被打印处理,说明咱们的缓存起做用了。

10.如何从缓存中提取缓存的数据:

首先咱们要知道怎么从缓存中提取已经缓存过的数据:

public class TestCache {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("config/spring-cache.xml");
        TestCacheService testCache = (TestCacheService)context.getBean("testCache");
        CacheManager cm =  (CacheManager) context.getBean("cacheManager");
        //添加数据到缓存中
        testCache.get("job");
        Cache uCache = cm.getCache("uCache");
        //经过参数做为key,获得对应的value
        User u1 = (User) uCache.get("job").get();
        u1.show();
        //添加数据到缓存中
        testCache.get2();
        //经过方法名做为key,获得对应value
        User u2 = (User) uCache.get("get2").get();
        u2.show();
    }
}

固然还有其余形式做为其key,类如官方文档就给了以下参考,它是使用spel:

image

 

 

当让还有其它的缓存技术,例如ehcache,guava,jcache:

image

这个是我例子中用到的源代码:spring缓存技术Caching例子

相关文章
相关标签/搜索