专栏名称: ImportNew
伯乐在线旗下账号,专注Java技术分享,包括Java基础技术、进阶技能、架构设计和Java技术领域动态等。
目录
相关文章推荐
芋道源码  ·  Web 实时消息推送的 7 种实现方案 ·  昨天  
芋道源码  ·  今年这情况。。大家多一手准备吧 ·  昨天  
芋道源码  ·  为什么很多程序员讨厌低代码? ·  2 天前  
Java编程精选  ·  某华为od吐槽:我双非在华为OD觉得委屈想走 ... ·  3 天前  
51好读  ›  专栏  ›  ImportNew

强大的Spring缓存技术(中)

ImportNew  · 公众号  · Java  · 2016-12-20 20:44

正文

(点击 上方公众号 ,可快速关注)


来源:Rollen Holt

链接:www.cnblogs.com/rollenholt/p/4202631.html


如何清空缓存


好,到目前为止,我们的 spring cache 缓存程序已经运行成功了,但是还不完美,因为还缺少一个重要的缓存管理逻辑:清空缓存.


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


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


  • 当外部调用更新了账号,则我们需要更新此账号对应的缓存


  • 当外部调用说明重新加载,则我们需要清空所有缓存


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


import com.google.common.base.Optional;

import com.rollenholt.spring.cache.example1.Account;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.cache.annotation.CacheEvict;

import org.springframework.cache.annotation.Cacheable;

import org.springframework.stereotype.Service;

/**

* @author wenchao.ren

*         2015/1/5.

*/

@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 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 getFromDB(String accountName) {

logger.info("real querying db... {}", accountName);

//Todo query data from database

return Optional.fromNullable(new Account(accountName));

}

}


我们的测试代码如下:


import com.rollenholt.spring.cache.example1.Account;

import org.junit.Before;

import org.junit.Test;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.context.support.ClassPathXmlApplicationContext;

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注解的定义,我们会发现:


/**

* Annotation indicating that a method (or all methods on a class) trigger(s)

* a cache invalidate operation.

*

* @author Costin Leau

* @author Stephane Nicoll

* @since 3.1

* @see CacheConfig

*/

@Target({ElementType.METHOD, ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Inherited

@Documented

public @interface CacheEvict {

/**

* Qualifier value for the specified cached operation.

*

May be used to determine the target cache (or caches), matching the qualifier

* value (or the bean name(s)) of (a) specific bean definition.

*/

String[] value() default {};

/**

* Spring Expression Language (SpEL) attribute for computing the key dynamically.

*

Default is "", meaning all method parameters are considered as a key, unless

* a custom {@link #keyGenerator()} has been set.

*/

String key() default "";

/**

* The bean name of the custom {@link org.springframework.cache.interceptor.KeyGenerator} to use.

*

Mutually exclusive with the {@link #key()} attribute.

*/

String keyGenerator() default "";

/**

* The bean name of the custom {@link org.springframework.cache.CacheManager} to use to

* create a default {@link org.springframework.cache.interceptor.CacheResolver} if none

* is set already.

*

Mutually exclusive with the {@link #cacheResolver()}  attribute.

* @see org.springframework.cache.interceptor.SimpleCacheResolver

*/

String cacheManager() default "";

/**

* The bean name of the custom {@link org.springframework.cache.interceptor.CacheResolver} to use.

*/

String cacheResolver() default "";

/**

* Spring Expression Language (SpEL) attribute used for conditioning the method caching.

*

Default is "", meaning the method is always cached.

*/

String condition() default "";

/**

* Whether or not all the entries inside the cache(s) are removed or not. By

* default, only the value under the associated key is removed.

*

Note that setting this parameter to {@code true} and specifying a {@link #key()}

* is not allowed.

*/

boolean allEntries() default false;

/**

* Whether the eviction should occur after the method is successfully invoked (default)

* or before. The latter causes the eviction to occur irrespective of the method outcome (whether

* it threw an exception or not) while the former does not.

*/

boolean beforeInvocation() default false;

}


定义中有一个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()

public Account getAccountByName(String accountName) {

// 方法内部实现不考虑缓存逻辑,直接实现业务

return getFromDB(accountName);







请到「今天看啥」查看全文