package cn.itcast.cache.ehcache;
import java.io.Serializable;
public class User implements Serializable {
private String name;
private int age;
public User (String name, int age) {
this .name = name;
this .age = 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;
}
}
package cn.itcast.cache.ehcache;
public interface UserService {
User getUsersByNameAndAge(String name, int age);
User getAnotherUser(String name, int age);
}
package cn.itcast.cache.ehcache;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service ("userService" )
@Cacheable (value="users" )
public class UserServiceImpl implements UserService {
@Override
public User getUsersByNameAndAge (String name, int age) {
System.out.println("正在执行getUsersByNameAndAge()方法" );
return new User(name, age);
}
@Override
public User getAnotherUser (String name, int age) {
System.out.println("正在执行getAnotherUser()方法" );
return new User(name, age);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<ehcache >
<diskStore path ="java.io.tmpdir" />
<defaultCache maxElementsInMemory ="10000" eternal ="false"
timeToIdleSeconds ="120" timeToLiveSeconds ="120" overflowToDisk ="true" />
<cache name ="users" maxElementsInMemory ="10000" eternal ="false"
timeToIdleSeconds ="300" timeToLiveSeconds ="600" overflowToDisk ="true" />
</ehcache >
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns ="http://www.springframework.org/schema/beans" xmlns:aop ="http://www.springframework.org/schema/aop"
xmlns:context ="http://www.springframework.org/schema/context" xmlns:tx ="http://www.springframework.org/schema/tx"
xmlns:cache ="http://www.springframework.org/schema/cache" xmlns:p ="http://www.springframework.org/schema/p"
xsi:schemaLocation ="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd" >
<cache:annotation-driven cache-manager = "cacheManager "/>
<context:component-scan base-package = "cn.itcast.cache.ehcache "> </context:component-scan >
<bean id = "ehCacheManager " class = "org.springframework.cache.ehcache.EhCacheManagerFactoryBean "
p:configLocation = "classpath:ehcache.xml "
p:shared = "false "/>
<bean id = "cacheManager " class = "org.springframework.cache.ehcache.EhCacheCacheManager "
p:cacheManager-ref = "ehCacheManager "/>
</beans >
package cn.itcast .cache .ehcache
import org.springframework .context .ApplicationContext
import org.springframework .context .support .ClassPathXmlApplicationContext
public class CacheTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext_cache.xml" )
UserService us = ctx.getBean ("userService" , UserService.class )
User u1 = us.getUsersByNameAndAge ("李旺红" , 13 )
User u2 = us.getAnotherUser ("李旺红" , 13 )
System.out .println (u1 == u2)
}
}