1.首先是在spring的配置文件的头部增加:
xmlns:cache="http://www.springframework.org/schema/cache"和
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
2.在spring配置文件内容中增加配置:
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache" />
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
3.增加ehcache的配置文件ehcache.xml,内容为:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
name="eyasAppApi" maxBytesLocalHeap="32M">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxBytesLocalHeap="24M" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
maxElementsOnDisk="100000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>
<cache name="bankNameCodes" maxBytesLocalHeap="8M" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"
maxElementsOnDisk="10000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>
</ehcache>
需要缓存的内容需要配置对应的名字:name="bankNameCodes"
4.然后在对应的service上面用注解进行配置,首先需要建一个接口(必须要实现接口的才能够):
public interface IBankNameCodeService {
List<BankNameCode> getBankNameCodeList();
}
实现类:
@Service
public class BankNameCodeServiceImpl implements IBankNameCodeService {
/**
* 获取银行名字和编码列表,并且缓存
* @return List<BankNameCode>
*/
@Override
@Cacheable(value = "bankNameCodes")
public List<BankNameCode> getBankNameCodeList() {
}
}
配置好过后第一次和第二次请求情况完全不一样,第二次直接从缓存中获取。
ehcache通过注解实现缓存配置就是这样的,ehcache对于少量数据缓存,已经性能要求不高的还是比较适用的,但是当性能要求特别高的时候,不能够很好的满足性能要求,
会对程序进行升级,用redis来进行数据的缓存,下一篇会介绍redis的配置和使用。
1.首先是在spring的配置文件的头部增加:
xmlns:cache="http://www.springframework.org/schema/cache"和
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
2.在spring配置文件内容中增加配置:
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache" />
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
3.增加ehcache的配置文件ehcache.xml,内容为:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
name="eyasAppApi" maxBytesLocalHeap="32M">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxBytesLocalHeap="24M" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
maxElementsOnDisk="100000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>
<cache name="bankNameCodes" maxBytesLocalHeap="8M" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"
maxElementsOnDisk="10000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>
</ehcache>
需要缓存的内容需要配置对应的名字:name="bankNameCodes"
4.然后在对应的service上面用注解进行配置,首先需要建一个接口(必须要实现接口的才能够):
public interface IBankNameCodeService {
List<BankNameCode> getBankNameCodeList();
}
实现类:
@Service
public class BankNameCodeServiceImpl implements IBankNameCodeService {
/**
* 获取银行名字和编码列表,并且缓存
* @return List<BankNameCode>
*/
@Override
@Cacheable(value = "bankNameCodes")
public List<BankNameCode> getBankNameCodeList() {
}
}
配置好过后第一次和第二次请求情况完全不一样,第二次直接从缓存中获取。
ehcache通过注解实现缓存配置就是这样的,ehcache对于少量数据缓存,已经性能要求不高的还是比较适用的,但是当性能要求特别高的时候,不能够很好的满足性能要求,
会对程序进行升级,用redis来进行数据的缓存,下一篇会介绍redis的配置和使用。
xmlns:cache="http://www.springframework.org/schema/cache"和
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
2.在spring配置文件内容中增加配置:
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache" />
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
3.增加ehcache的配置文件ehcache.xml,内容为:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
name="eyasAppApi" maxBytesLocalHeap="32M">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxBytesLocalHeap="24M" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
maxElementsOnDisk="100000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>
<cache name="bankNameCodes" maxBytesLocalHeap="8M" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"
maxElementsOnDisk="10000" diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>
</ehcache>
需要缓存的内容需要配置对应的名字:name="bankNameCodes"
4.然后在对应的service上面用注解进行配置,首先需要建一个接口(必须要实现接口的才能够):
public interface IBankNameCodeService {
List<BankNameCode> getBankNameCodeList();
}
实现类:
@Service
public class BankNameCodeServiceImpl implements IBankNameCodeService {
/**
* 获取银行名字和编码列表,并且缓存
* @return List<BankNameCode>
*/
@Override
@Cacheable(value = "bankNameCodes")
public List<BankNameCode> getBankNameCodeList() {
}
}
配置好过后第一次和第二次请求情况完全不一样,第二次直接从缓存中获取。
ehcache通过注解实现缓存配置就是这样的,ehcache对于少量数据缓存,已经性能要求不高的还是比较适用的,但是当性能要求特别高的时候,不能够很好的满足性能要求,
会对程序进行升级,用redis来进行数据的缓存,下一篇会介绍redis的配置和使用。