首页 > 代码库 > spring配置ehcache
spring配置ehcache
spring配置文件
<cache:annotation-driven cache-manager="cacheManager" /> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehcache"></property> </bean> <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"></property> </bean>
ehcache文件
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir" /> <defaultCache eternal="false" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" /> <cache name="departCache" eternal="false" maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" /> </ehcache>
实现类
import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; @Service public class CacheServiceImpl implements CacheService{ @Cacheable(value="departCache",key="#i") public String findUser(int i) { StringBuffer sb=new StringBuffer(); int j=0; for(;j<i;j++){ sb.append("hello"+i); } return sb.toString(); } }
测试类
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/application.xml") public class TestCache { @Resource CacheServiceImpl cacheServiceImpl; @Test public void testSend() { System.out.println(System.currentTimeMillis()); cacheServiceImpl.findUser(1000); System.out.println(System.currentTimeMillis()); System.out.println(System.currentTimeMillis()); cacheServiceImpl.findUser(1000); System.out.println(System.currentTimeMillis()); } }
spring配置ehcache
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。