zoukankan      html  css  js  c++  java
  • spring中的缓存--Caching

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

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

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

    4.注解:

    image

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

    5.

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

    image

    @CacheEvict:用来清理缓存。常用有cacheNames,allEntries(默认值false);分别代表了要清除的缓存名称和是否全部清除(true代表全部清除)。

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

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

    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例子

     

  • 相关阅读:
    Leetcode 191.位1的个数 By Python
    反向传播的推导
    Leetcode 268.缺失数字 By Python
    Leetcode 326.3的幂 By Python
    Leetcode 28.实现strStr() By Python
    Leetcode 7.反转整数 By Python
    Leetcode 125.验证回文串 By Python
    Leetcode 1.两数之和 By Python
    Hdoj 1008.Elevator 题解
    TZOJ 车辆拥挤相互往里走
  • 原文地址:https://www.cnblogs.com/haoke/p/4986999.html
Copyright © 2011-2022 走看看