整体代码结构:
User.java
package com.kinsey.woo.dto;
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
UserService.java
package com.kinsey.woo.service;
import com.kinsey.woo.dto.User;
public interface UserService {
public User getUserByNameAndAge(String name,int age);
public User getAntherUser(String name, int age);
}
UserServiceImpl.java
package com.kinsey.woo.service.impl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.kinsey.woo.dto.User;
import com.kinsey.woo.service.UserService;
@Service("userService")
@Cacheable(value="users")
public class UserServiceImpl implements UserService {
public User getUserByNameAndAge(String name,int age){
System.out.print("getUserByNameAndAge is called...\n");
return new User(name,age);
}
@Override
public User getAntherUser(String name, int age) {
System.out.println("getAntherUser is called...\n");
return new User(name,age);
}
}
ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
4 <!--
5 磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
6 path:指定在硬盘上存储对象的路径
7 -->
8 <diskStore path="java.io.tmpdir" />
9
10
11 <!--
12 defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
13 maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
14 eternal:代表对象是否永不过期
15 timeToIdleSeconds:最大的发呆时间
16 timeToLiveSeconds:最大的存活时间
17 overflowToDisk:是否允许对象被写入到磁盘
18 -->
19 <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="120000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
21
22 <!--
23 cache:为指定名称的对象进行缓存的特殊配置
24 name:指定对象的完整名
25 -->
26 <cache name="users" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true" />
28
29
30 </ehcache>
application.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
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-4.3.xsd">
<bean id="ehCacheManger" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:configLocation="classpath:ehcache.xml"
/>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
p:cacheManager-ref="ehCacheManger"
/>
<context:component-scan base-package="com.kinsey.woo.service"></context:component-scan>
<cache:annotation-driven cache-manager="cacheManager"/>
</beans>
RunMain.java
package com.kinsey.woo.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.kinsey.woo.dto.User;
import com.kinsey.woo.service.UserService;
public class RunMain {
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
UserService userService = ctx.getBean("userService", UserService.class);
User u1 = userService.getUserByNameAndAge("Godwin", 10);
User u2 = userService.getAntherUser("Godwin", 10);
if(u1==u2){
System.out.print("u1=u2 \n");
}
}
}
jar:
ehcache-core-2.6.11.jar
slf4j-api-1.7.25.jar