在Spring中经过获取MemCachedClient来实现与memcached服务器进行数据读取的方式。不过,在实际开发中,咱们每每是经过Spring的@Cacheable来实现数据的缓存的,因此,本文给你们详细介绍一下@Cacheable的用法。首先,在使用@Cacheable以前,咱们要作好准备工做。java
第一步:要导入相应的jar包。
<classpathentry kind="lib" path="lib/spring-core-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/spring-cache-1.0.10.jar"/>
<classpathentry kind="lib" path="lib/spring-context-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/spring-beans-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.2.jar"/>
<classpathentry kind="lib" path="lib/log4j-1.2.17.jar"/>
<classpathentry kind="lib" path="lib/spring-expression-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/java_memcached-release_2.0.1.jar"/>
<classpathentry kind="lib" path="lib/spring-aop-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/spring-aspects-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/spring-context-support-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/spring-tx-4.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/aopalliance-1.0.jar"/>
<classpathentry kind="lib" path="lib/ognl-3.0.6.jar"/>
<classpathentry kind="lib" path="lib/trafficCounter-1.0.2.jar"/>
<classpathentry kind="lib" path="lib/aspectjweaver-1.8.4.jar"/>
<classpathentry kind="lib" path="lib/javassist-3.11.0.GA.jar"/>spring
第二步:xml文件中增长命名空间。数据库
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
第三步:添加自动扫描功能。express
<context:component-scan base-package="service" /> <aop:config proxy-target-class="true"/>
第四步:增长缓存管理类。缓存
<bean id="memCacheProvider" class="com.springcache.memcache.MemCacheProvider"> <property name="memCache" ref="memCacheClient"/> </bean> <bean id="cacheManager" class="com.springcache.CacheManager"> <property name="elParserName" value="ognl"/> <property name="cacheProviders"> <map> <entry key="remote" value-ref="memCacheProvider"></entry> </map> </property> </bean>
第五步:创建一个测试类。服务器
package service;
import org.springframework.stereotype.Service;
import com.springcache.annotation.Cacheable;
@Service
@Cacheable
public class MemcachedService {
@Cacheable(name = "remote", key = "'USER_NAME_'+#args[0]", expire = 60 )
public String storeUserName(String accountId, String name)
{
return name;
}
@Cacheable(name = "remote", expire = 60)
public String storeUserAddress(String accountId, String address)
{
return address;
}
}
@Cacheable支持以下几个参数:
key:缓存的key,默认为空,既表示使用方法的参数类型及参数值做为key,支持SpEL。例如:memCachedService.storeUserAddress("user", "BeiJing");
因此对应的key为:service.MemcachedService-storeUserAddress_user_BeiJing
name:存储位置。在原本中remote表示使用memcached服务器。
condition:触发条件,只有知足条件的状况才会加入缓存,默认为空,既表示所有都加入缓存,支持SpEL。
expire:过时时间,单位为秒。多线程
第六 扩展:使用Spring4.3解决缓存过时后多线程并发访问数据库的问题并发
缓存过时以后,若是多个线程同时请求对某个数据的访问,会同时去到数据库,致使数据库瞬间负荷增高。Spring4.3为@Cacheable注解提供了一个新的参数“sync”(boolean类型,缺省为false),ide
当设置它为true时,只有一个线程的请求会去到数据库,其余线程都会等待直到缓存可用。这个设置能够减小对数据库的瞬间并发访问。memcached
不过不必定全部的缓存系统都支持这个配置。通过验证,Guava Cache是支持的 参考:http://blog.csdn.net/clementad/article/details/51250472
@Service public class UserServiceCacheablesImpl implements UserServiceCacheables{ private final static Logger logger = LoggerFactory.getLogger(UserServiceCacheablesImpl.class); @Autowired UserDAO userDAO; @Override @Cacheable(value="getPhoneNoByUserId", sync=true) public String getPhoneNoByUserId(int userId) { logger.debug("getting data from database, userId={}", userId); return userDAO.getPhoneNoByUserId(userId); } }
最后总结一下:当执行到一个被@Cacheable注解的方法时,Spring首先检查condition条件是否知足,若是不知足,执行方法,返回;若是知足,在name所命名的缓存空间中查找使用key存储的对象,若是找到,将找到的结果返回,若是没有找到执行方法,将方法的返回值以key-value对象的方式存入name缓存中,而后方法返回。