美文网首页
Spring Cache简单介绍和使用

Spring Cache简单介绍和使用

作者: 超天大圣JR | 来源:发表于2019-12-17 12:29 被阅读0次

    概述

    Spring 3.1 引入了激动人心的基于注解(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(比如EHCache 或者 OSCache),而是一个对缓存使用的抽象,通过在既有代码中加入少量它定义的各种注解,即能够达到缓存方法的返回对象的效果。

    Spring 的缓存技术还具备相当的灵活性。不仅能够使用SpEL(Spring Expression Language)来定义缓存的 key 和各种 condition,还提供开箱即用的缓存暂时存储方案,也支持和主流的专业缓存比如 EHCache 集成。

    其特点总结例如以下:

    • 通过少量的配置 annotation 注解就可以使得既有代码支持缓存
    • 支持开箱即用 Out-Of-The-Box,即不用安装和部署额外第三方组件就可以使用缓存
    • 支持 Spring Express Language,能使用对象的不论什么属性或者方法来定义缓存的 key 和 condition
    • 支持 AspectJ,并通过事实上现不论什么方法的缓存支持
    • 支持自己定义 key 和自己定义缓存管理者,具有相当的灵活性和扩展性

    一、 自己实现缓存

    这里先展示一个自己定义的缓存实现,即不用第三方的组件来实现某种对象的内存缓存。
    场景例如以下:

    对一个账号查询方法做缓存,账号名称为 key,账号对象为 value,当以同样的账号名称查询账号的时候,直接从缓存中返回结果。否则更新缓存。账号查询服务还支持 reload 缓存(即清空缓存)

    首先定义一个实体类:账号类,具备主要的 id 和 name 属性。且具备 getter 和 setter 方法

    public class Account {
    
        private int id;
        private String name;
    
        public Account(String name) {
            this.name = name;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    
    }
    

    然后定义一个缓存管理器,这个管理器负责实现缓存逻辑,支持对象的添加、改动和删除,支持值对象的泛型。

    public class CacheContext<T> {
    
        private Map<String, T> cache = Maps.newConcurrentMap();
    
        public T get(String key){
            return  cache.get(key);
        }
    
        public void addOrUpdateCache(String key,T value) {
            cache.put(key, value);
        }
    
        // 依据 key 来删除缓存中的一条记录
        public void evictCache(String key) {
            if(cache.containsKey(key)) {
                cache.remove(key);
            }
        }
    
        // 清空缓存中的全部记录
        public void evictCache() {
            cache.clear();
        }
    
    }
    

    好,如今我们有了实体类和一个缓存管理器,还须要一个提供账号查询的服务类。此服务类使用缓存管理器来支持账号查询缓存。例如以下:

    @Service
    public class AccountService1 {
    
        private final Logger logger = LoggerFactory.getLogger(AccountService1.class);
    
        @Resource
        private CacheContext<Account> accountCacheContext;
    
        public Account getAccountByName(String accountName) {
            Account result = accountCacheContext.get(accountName);
            if (result != null) {
                logger.info("get from cache... {}", accountName);
                return result;
            }
    
            Optional<Account> accountOptional = getFromDB(accountName);
            if (!accountOptional.isPresent()) {
                throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName));
            }
    
            Account account = accountOptional.get();
            accountCacheContext.addOrUpdateCache(accountName, account);
            return account;
        }
    
        public void reload() {
            accountCacheContext.evictCache();
        }
    
        private Optional<Account> getFromDB(String accountName) {
            logger.info("real querying db... {}", accountName);
            //Todo query data from database
            return Optional.fromNullable(new Account(accountName));
        }
    
    }
    

    现在我们写一个测试类,用于测试刚才的缓存是否有效

    public class AccountService1Test {
    
        private AccountService1 accountService1;
    
        private final Logger logger = LoggerFactory.getLogger(AccountService1Test.class);
    
        @Before
        public void setUp() throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext1.xml");
            accountService1 = context.getBean("accountService1", AccountService1.class);
        }
    
        @Test
        public void testInject(){
            assertNotNull(accountService1);
        }
    
        @Test
        public void testGetAccountByName() throws Exception {
            accountService1.getAccountByName("accountName");
            accountService1.getAccountByName("accountName");
    
            accountService1.reload();
            logger.info("after reload ....");
    
            accountService1.getAccountByName("accountName");
            accountService1.getAccountByName("accountName");
        }
    }
    

    依照分析,运行结果应该是:首先从数据库查询,然后直接返回缓存中的结果,重置缓存后,应该先从数据库查询。然后返回缓存中的结果. 查看程序运行的日志例如以下:

    00:53:17.166 [main] INFO  c.r.s.cache.example1.AccountService - real querying db... accountName
    00:53:17.168 [main] INFO  c.r.s.cache.example1.AccountService - get from cache... accountName
    00:53:17.168 [main] INFO  c.r.s.c.example1.AccountServiceTest - after reload ....
    00:53:17.168 [main] INFO  c.r.s.cache.example1.AccountService - real querying db... accountName
    00:53:17.169 [main] INFO  c.r.s.cache.example1.AccountService - get from cache... accountName
    

    能够看出我们的缓存起效了,可是这样的自己定义的缓存方案有例如以下劣势:

    • 缓存代码和业务代码耦合度太高。如上面的样例,AccountService 中的 getAccountByName()方法中有了太多缓存的逻辑,不便于维护和变更
    • 不灵活,这样的缓存方案不支持依照某种条件的缓存,比方仅仅有某种类型的账号才须要缓存,这样的需求会导致代码的变更
    • 缓存的存储这块写的比較死,不能灵活的切换为使用第三方的缓存模块

    二、Spring cache实现缓存

    我们对AccountService1 进行改动。创建AccountService2:

    @Service
    public class AccountService2 {
    
        private final Logger logger = LoggerFactory.getLogger(AccountService2.class);
    
        // 使用了一个缓存名叫 accountCache
        @Cacheable(value="accountCache")
        public Account getAccountByName(String accountName) {
    
            // 方法内部实现不考虑缓存逻辑,直接实现业务
            logger.info("real querying account... {}", accountName);
            Optional<Account> accountOptional = getFromDB(accountName);
            if (!accountOptional.isPresent()) {
                throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName));
            }
    
            return accountOptional.get();
        }
    
        private Optional<Account> getFromDB(String accountName) {
            logger.info("real querying db... {}", accountName);
            //Todo query data from database
            return Optional.fromNullable(new Account(accountName));
        }
    
    }
    

    我们注意到在上面的代码中有一行:

     @Cacheable(value="accountCache")
    

    这个注解的意思是,当调用这种方法的时候。会从一个名叫 accountCache 的缓存中查询,假设没有,则运行实际的方法(即查询数据库),并将运行的结果存入缓存中。否则返回缓存中的对象。这里的缓存中的 key 就是參数 accountName,value 就是 Account 对象。“accountCache”缓存是在 spring*.xml 中定义的名称。我们还须要一个 spring 的配置文件来支持基于注解的缓存

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           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/context
               http://www.springframework.org/schema/context/spring-context.xsd
               http://www.springframework.org/schema/cache
               http://www.springframework.org/schema/cache/spring-cache.xsd">
    
        <context:component-scan base-package="com.rollenholt.spring.cache"/>
    
        <context:annotation-config/>
    
        <cache:annotation-driven/>
    
        <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
            <property name="caches">
                <set>
                    <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                        <property name="name" value="default"/>
                    </bean>
                    <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
                        <property name="name" value="accountCache"/>
                    </bean>
                </set>
            </property>
        </bean>
    
    </beans>
    

    注意这个 spring 配置文件有一个关键的支持缓存的配置项:

    <cache:annotation-driven />
    

    这个配置项缺省使用了一个名字叫 cacheManager 的缓存管理器,这个缓存管理器有一个 spring 的缺省实现,即 org.springframework.cache.support.SimpleCacheManager。这个缓存管理器实现了我们刚刚自己定义的缓存管理器的逻辑,它须要配置一个属性 caches,即此缓存管理器管理的缓存集合,除了缺省的名字叫 default 的缓存,我们还自己定义了一个名字叫 accountCache 的缓存,使用了缺省的内存存储方案 ConcurrentMapCacheFactoryBean,它是基于 java.util.concurrent.ConcurrentHashMap 的一个内存缓存实现方案。

    然后我们编写測试程序:

    public class AccountService2Test {
    
        private AccountService2 accountService2;
    
        private final Logger logger = LoggerFactory.getLogger(AccountService2Test.class);
    
        @Before
        public void setUp() throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
            accountService2 = context.getBean("accountService2", AccountService2.class);
        }
    
        @Test
        public void testInject(){
            assertNotNull(accountService2);
        }
    
        @Test
        public void testGetAccountByName() throws Exception {
            logger.info("first query...");
            accountService2.getAccountByName("accountName");
    
            logger.info("second query...");
            accountService2.getAccountByName("accountName");
        }
    }
    

    以上測试代码主要进行了两次查询。第一次应该会查询数据库,第二次应该返回缓存。不再查数据库,我们运行一下。看看结果

    01:10:32.435 [main] INFO  c.r.s.c.example2.AccountService2Test - first query...
    01:10:32.457 [main] INFO  c.r.s.cache.example2.AccountService2 - real querying db... accountName
    01:10:32.458 [main] INFO  c.r.s.c.example2.AccountService2Test - second query...
    01:10:32.456 [main] INFO  c.r.s.cache.example2.AccountService2 - real querying db... accountName
    

    能够看出我们设置的基于注解的缓存起作用了,而在 AccountService.java 的代码中。我们没有看到不论什么的缓存逻辑代码。仅仅有一行注解:@Cacheable(value="accountCache"),就实现了主要的缓存方案。

    三、清空缓存

    当账号数据发生变更,那么必须要清空某个缓存,另外还须要定期的清空全部缓存,以保证缓存数据的可靠性。

    为了加入清空缓存的逻辑。我们仅仅要对 AccountService2.java 进行改动,从业务逻辑的角度上看,它有两个须要清空缓存的地方

    • 当外部调用更新了账号,则我们须要更新此账号相应的缓存
    • 当外部调用说明又一次载入,则我们须要清空全部缓存

    我们在AccountService2的基础上进行改动,改动为AccountService3,代码例如以下:

    @Service
    public class AccountService3 {
    
        private final Logger logger = LoggerFactory.getLogger(AccountService3.class);
    
        // 使用了一个缓存名叫 accountCache
        @Cacheable(value="accountCache")
        public Account getAccountByName(String accountName) {
    
            // 方法内部实现不考虑缓存逻辑,直接实现业务
            logger.info("real querying account... {}", accountName);
            Optional<Account> accountOptional = getFromDB(accountName);
            if (!accountOptional.isPresent()) {
                throw new IllegalStateException(String.format("can not find account by account name : [%s]", accountName));
            }
    
            return accountOptional.get();
        }
    
        @CacheEvict(value="accountCache",key="#account.getName()")
        public void updateAccount(Account account) {
            updateDB(account);
        }
    
        @CacheEvict(value="accountCache",allEntries=true)
        public void reload() {
        }
    
        private void updateDB(Account account) {
            logger.info("real update db...{}", account.getName());
        }
    
        private Optional<Account> getFromDB(String accountName) {
            logger.info("real querying db... {}", accountName);
            //Todo query data from database
            return Optional.fromNullable(new Account(accountName));
        }
    }
    

    我们的测试代码例如以下:

    public class AccountService3Test {
    
    
        private AccountService3 accountService3;
    
        private final Logger logger = LoggerFactory.getLogger(AccountService3Test.class);
    
        @Before
        public void setUp() throws Exception {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
            accountService3 = context.getBean("accountService3", AccountService3.class);
        }
    
        @Test
        public void testGetAccountByName() throws Exception {
    
            logger.info("first query.....");
            accountService3.getAccountByName("accountName");
    
            logger.info("second query....");
            accountService3.getAccountByName("accountName");
    
        }
    
        @Test
        public void testUpdateAccount() throws Exception {
            Account account1 = accountService3.getAccountByName("accountName1");
            logger.info(account1.toString());
            Account account2 = accountService3.getAccountByName("accountName2");
            logger.info(account2.toString());
    
            account2.setId(121212);
            accountService3.updateAccount(account2);
    
            // account1会走缓存
            account1 = accountService3.getAccountByName("accountName1");
            logger.info(account1.toString());
            // account2会查询db
            account2 = accountService3.getAccountByName("accountName2");
            logger.info(account2.toString());
    
        }
    
        @Test
        public void testReload() throws Exception {
            accountService3.reload();
            // 这2行查询数据库
            accountService3.getAccountByName("somebody1");
            accountService3.getAccountByName("somebody2");
    
            // 这两行走缓存
            accountService3.getAccountByName("somebody1");
            accountService3.getAccountByName("somebody2");
        }
    }
    

    在这个測试代码中我们重点关注testUpdateAccount()方法。在測试代码中我们已经注释了在update完account2以后,再次查询的时候。account1会走缓存,而account2不会走缓存,而去查询db,观察程序运行日志,运行日志为:

    01:37:34.549 [main] INFO  c.r.s.cache.example3.AccountService3 - real querying account... accountName1
    01:37:34.551 [main] INFO  c.r.s.cache.example3.AccountService3 - real querying db... accountName1
    01:37:34.552 [main] INFO  c.r.s.c.example3.AccountService3Test - Account{id=0, name='accountName1'}
    01:37:34.553 [main] INFO  c.r.s.cache.example3.AccountService3 - real querying account... accountName2
    01:37:34.553 [main] INFO  c.r.s.cache.example3.AccountService3 - real querying db... accountName2
    01:37:34.555 [main] INFO  c.r.s.c.example3.AccountService3Test - Account{id=0, name='accountName2'}
    01:37:34.555 [main] INFO  c.r.s.cache.example3.AccountService3 - real update db...accountName2
    01:37:34.595 [main] INFO  c.r.s.c.example3.AccountService3Test - Account{id=0, name='accountName1'}
    01:37:34.596 [main] INFO  c.r.s.cache.example3.AccountService3 - real querying account... accountName2
    01:37:34.596 [main] INFO  c.r.s.cache.example3.AccountService3 - real querying db... accountName2
    01:37:34.596 [main] INFO  c.r.s.c.example3.AccountService3Test - Account{id=0, name='accountName2'}
    

    四、依照条件操作缓存

    前面介绍的缓存方法,不论什么条件,都全部对 accountService 对象的 getAccountByName 方法的调用都会启动缓存效果,无论參数是什么值。

    设有一个需求,就是仅仅有账号名称的长度小于等于 4 的情况下,才做缓存,大于 4 的不使用缓存

    @CacheEvict源码:

    @Target({ElementType.METHOD, ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Inherited
    @Documented
    public @interface CacheEvict {
    
        String[] value() default {};
        String key() default "";
        String keyGenerator() default "";
        String cacheManager() default "";
        String cacheResolver() default "";
        String condition() default "";
        boolean allEntries() default false;
        boolean beforeInvocation() default false;
    }
    

    通过查看CacheEvict注解的定义,我们会发现定义中有一个condition描写叙述:

    Spring Expression Language (SpEL) attribute used for conditioning the method caching.Default is "", meaning the method is always cached.

    我们能够利用这种方法来完毕这个功能,以下仅仅给出演示样例代码:

    @Cacheable(value="accountCache",condition="#accountName.length() <= 4")// 缓存名叫 accountCache 
    public Account getAccountByName(String accountName) {
        // 方法内部实现不考虑缓存逻辑,直接实现业务
        return getFromDB(accountName);
    }
    

    注意当中的 condition=”#accountName.length() <=4”,这里使用了 SpEL 表达式訪问了參数 accountName 对象的 length() 方法,条件表达式返回一个布尔值,true/false,当条件为 true。则进行缓存操作,否则直接调用方法运行的返回结果。

    五、假设有多个參数,怎样进行 key 的组合

    我们要依据账号名、password对账号对象进行缓存,而第三个參数“是否发送日志”对缓存没有不论什么影响。所以,我们能够利用 SpEL 表达式对缓存 key 进行设计
    我们为Account类添加一个password 属性, 然后改动AccountService代码:

     @Cacheable(value="accountCache",key="#accountName.concat(#password)") 
     public Account getAccount(String accountName,String password,boolean sendLog) { 
       // 方法内部实现不考虑缓存逻辑。直接实现业务
       return getFromDB(accountName,password); 
     }
    

    注意上面的 key 属性,当中引用了方法的两个參数 accountName 和 password,而 sendLog 属性没有考虑。由于其对缓存没有影响。

    accountService.getAccount("accountName", "123456", true);// 查询数据库
    accountService.getAccount("accountName", "123456", true);// 走缓存
    accountService.getAccount("accountName", "123456", false);// 走缓存
    accountService.getAccount("accountName", "654321", true);// 查询数据库
    accountService.getAccount("accountName", "654321", true);// 走缓存
    

    六、怎样做到:既要保证方法被调用。又希望结果被缓存

    依据前面的样例,我们知道,假设使用了 @Cacheable 注解,则当反复使用同样參数调用方法的时候,方法本身不会被调用运行。即方法本身被略过了,取而代之的是方法的结果直接从缓存中找到并返回了。

    现实中并不总是如此,有些情况下我们希望方法一定会被调用,由于其除了返回一个结果,还做了其它事情。比如记录日志。调用接口等。这个时候。我们能够用 @CachePut 注解,这个注解能够确保方法被运行,同一时候方法的返回值也被记录到缓存中。

    @Cacheable(value="accountCache")
     public Account getAccountByName(String accountName) { 
       // 方法内部实现不考虑缓存逻辑,直接实现业务
       return getFromDB(accountName); 
     } 
    
     // 更新 accountCache 缓存
     @CachePut(value="accountCache",key="#account.getName()")
     public Account updateAccount(Account account) { 
       return updateDB(account); 
     } 
     private Account updateDB(Account account) { 
       logger.info("real updating db..."+account.getName()); 
       return account; 
     }
    

    測试代码例如以下:

    Account account = accountService.getAccountByName("someone"); 
    account.setPassword("123"); 
    accountService.updateAccount(account); 
    account.setPassword("321"); 
    accountService.updateAccount(account); 
    account = accountService.getAccountByName("someone"); 
    logger.info(account.getPassword()); 
    

    如上面的代码所看到的。我们首先用 getAccountByName 方法查询一个人 someone 的账号。这个时候会查询数据库一次。可是也记录到缓存中了。然后我们改动了password,调用了 updateAccount 方法。这个时候会运行数据库的更新操作且记录到缓存,我们再次改动password并调用 updateAccount 方法。然后通过 getAccountByName 方法查询,这个时候。由于缓存中已经有数据,所以不会查询数据库,而是直接返回最新的数据,所以打印的password应该是“321”

    七、@Cacheable、@CachePut、@CacheEvict 凝视介绍

    1.@Cacheable执行缓存

    @Cacheable可用于修饰类或修饰方法,当使用@Cacheable修饰类时,用于告诉Spring在类级别上进行缓存———程序调用该类的实例的任何方法时都需要缓存,而且共享同一个缓存区;当使用@Cacheable修饰方法时,用于告诉Spring在方法级别上进行缓存———只有当程序调用该方法时才需要缓存

    类级别的缓存:
    使用@Cacheable修饰类时,就可控制Spring在类级别进行缓存,这样当程序调用该类的任意方法时,只要传入的参数相同,Spring就会使用缓存

    @Service("userService")
    // 指定将数据放入users缓存区
    @Cacheable(value = "users")
    public class UserServiceImpl implements UserService
    {
        public User getUsersByNameAndAge(String name, int age)
        {
            System.out.println("--正在执行findUsersByNameAndAge()查询方法--");
            return new User(name, age);
        }
        public User getAnotherUser(String name, int age)
        {
            System.out.println("--正在执行findAnotherUser()查询方法--");
            return new User(name, age);
        }
    }
    

    当程序第一次调用该类的实例的某个方法时,Spring缓存机制会将该方法返回的数据放入指定缓存区。以后程序调用该类的实例的任何方法时,只要传入的参数相同,Spring将不会真正执行该方法,而是直接利用缓存区中的数据

    使用@Cacheable是可指定如下属性:

    • value:必须属性。该属性可指定多个缓存区的名字,用于指定将方法返回值放入指定的缓存区内
    • key:通过SpEL表达式显式指定缓存的key
    • condition:该属性指定一个返回boolean值的SpEL表达式,只有当该表达式返回true时,Spring才会缓存方法返回值
    • unless:该属性指定一个返回boolean值的SpEL表达式,当该表达式返回true时,Spring就不缓存方法返回值

    与@Cache注解功能类似的还有一个@Cacheput注解,与@Cacheable不同的是,@Cacheput修饰的方法不会读取缓存区中的数据———这意味着不管缓存区是否已有数据,@Cacheput总会告诉Spring要重新执行这些方法,并在此将方法返回值放入缓存区

    方法级别的缓存:
    使用@Cacheable修饰方法时,就可控制Spring在方法级别进行缓存,这样当程序调用该方法时,只要传入的参数相同,Spring就会使用缓存

    @Service("userService")
    public class UserServiceImpl implements UserService
    {
        @Cacheable(value = "users1")
        public User getUsersByNameAndAge(String name, int age)
        {
            System.out.println("--正在执行findUsersByNameAndAge()查询方法--");
            return new User(name, age);
        }
        @Cacheable(value = "users2")
        public User getAnotherUser(String name, int age)
        {
            System.out.println("--正在执行findAnotherUser()查询方法--");
            return new User(name, age);
        }
    }
    

    上面代码中指定getUsersByNameAndAge()和getAnotherUser()方法分别使用不同的缓存区,这意味着两个方法都会缓存,但由于它们使用了不同的缓存区,因此它们不能共享缓存区数据

    2.使用@CacheEvict清除缓存

    被@CacheEvict注解修饰的方法可用于清除缓存,使用@CacheEvict注解时可指定如下属性:

    • value:必需属性。用于指定该方法用于清除哪个缓存区的数据
    • key:通过SpEL表达式显式指定缓存的key
    • allEntries:该属性指定是否清空整个缓存区
    • beforeInvocation:该属性指定是否在执行方法之前清除缓存。默认实在false
    • condion:该属性指定一个SpEL变道时,只有当该表达式为true时才清除缓存
    @Service("userService")
    @Cacheable(value = "users")
    public class UserServiceImpl implements UserService
    {
        public User getUsersByNameAndAge(String name, int age)
        {
            System.out.println("--正在执行findUsersByNameAndAge()查询方法--");
            return new User(name, age);
        }
        public User getAnotherUser(String name, int age)
        {
            System.out.println("--正在执行findAnotherUser()查询方法--");
            return new User(name, age);
        }
        // 指定根据name、age参数清除缓存
        @CacheEvict(value = "users")
        public void evictUser(String name, int age)
        {
            System.out.println("--正在清空"+ name
                + " , " + age + "对应的缓存--");
        }
        // 指定清除user缓存区所有缓存数据
        @CacheEvict(value = "users" , allEntries=true)
        public void evictAll()
        {
            System.out.println("--正在清空整个缓存--");
        }
    }
    
    3.基本原理

    一句话介绍就是Spring AOP的动态代理技术。

    相关文章

      网友评论

          本文标题:Spring Cache简单介绍和使用

          本文链接:https://www.haomeiwen.com/subject/hyfhnctx.html