介绍
缓存是一种存储机制,它将数据保存在某个地方,并以一种更快的方式为日后的请求提供服务,在应用程序中使用缓存机制,可以避免方法的多次执行,可以根据所提供的输入值对方法的结果数据进行缓存
通过使用AOP原则,对方法进行编织,如果已经为提供的参数执行过,那么不必执行实际方法就可以返回被缓存的结果,所以需要为应用程序的所有类生成代理类,并使用缓存注解对类的方法进行标记
创建第一个缓存应用程序
(1)创建一个空Maven,添加以下依赖,缓存注金额之类的类位于spring-contextJAR中
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
(2)在com.wiley.beginningspring.ch10包中创建域类User
public class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
(3)在com.wiley.beginningspring.ch10包中创建
public class UserService {
private Map<Integer,User> users=new HashMap<Integer, User>();
{
users.put(1,new User(1,"Kenan"));
users.put(2,new User(2,"Mert"));
}
@Cacheable(value="users")//根据方法参数对方法的返回值进行缓存
public User getUser(int id)
{
System.out.println("id = [" + id + "]");
return users.get(id);
}
}
(4)在src/main/resources/文件中创建applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--支持cache注解-->
<cache:annotation-driven/>
<bean id="usersService" class="com.wiley.beginningspring.ch10.UserService">
</bean>
<!--定义缓存管理器-->
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<!--定义缓存区域,根据命名约定,将缓存区域与所使用的注解绑定起来-->
<property name="caches">
<set>
<bean id="users" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"></bean>
</set>
</property>
</bean>
</beans>
(5)测试
public class Main {
public static void main(String[] args) {
ApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=context.getBean(UserService.class);
User userFetch1=userService.getUser(1);// 第一次执行etUser方法返回
System.out.println(userFetch1);
User userFetch2=userService.getUser(2);
System.out.println(userFetch2);
User userFetch3=userService.getUser(1);// 第二次直接从缓存返回
System.out.println(userFetch3);
User userFetch4=userService.getUser(1);// 第三次也是直接从缓存返回
System.out.println(userFetch4);
}
}
运行结果
使用不同的ID/name显式地指定缓存管理器
<cache:annotation-driven cache-manager="myCacheManager"/>
<bean id="myCacheManager" class="org.springframework.cache.support.SimpleCacheManager">
</bean>
使用注解配置缓存对象
除了xml配置还可以使用注解配置
@Configuration
@ComponentScan(basePackages = {"com.wiley.beginningspring.ch10"})
@EnableCaching
public class ApplicationConfig {
@Bean
public CacheManager cacheManager()
{
//创建一个SimpleCacheManager并使用了名称users设置了新ConcurrentMapCache
SimpleCacheManager cacheManager=new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache(
"users"
)));
return cacheManager;
}
}
测试
public class Main {
public static void main(String[] args) {
// ApplicationContext context=
// new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext context=new AnnotationConfigApplicationContext(ApplicationConfig.class);
UserService userService=context.getBean(UserService.class);
User userFetch1=userService.getUser(1);// 第一次执行etUser方法返回
System.out.println(userFetch1);
User userFetch2=userService.getUser(2);
System.out.println(userFetch2);
User userFetch3=userService.getUser(1);// 第二次直接从缓存返回
System.out.println(userFetch3);
User userFetch4=userService.getUser(1);// 第三次也是直接从缓存返回
System.out.println(userFetch4);
}
}