EhCache实例

来源:互联网 发布:音箱测试软件 编辑:程序博客网 时间:2024/06/06 19:21

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:noNamespaceSchemaLocation="ehcache.xsd"          updateCheck="true"         monitoring="autodetect">    <diskStore path="java.io.tmpdir" />    <defaultCache maxElementsInMemory="10000"                   eternal="false"                  timeToIdleSeconds="120"                   timeToLiveSeconds="120"                   overflowToDisk="false"                  maxElementsOnDisk="10000000"                   diskPersistent="false"                  diskExpiryThreadIntervalSeconds="120"                   memoryStoreEvictionPolicy="LRU" />    <cache name="test1"            maxElementsInMemory="10000"           maxElementsOnDisk="1000"            eternal="false"            overflowToDisk="false"           diskSpoolBufferSizeMB="20"            timeToIdleSeconds="300"            timeToLiveSeconds="600"           memoryStoreEvictionPolicy="LRU" />    <cache name="test2"            maxElementsInMemory="10000"           maxElementsOnDisk="1000"            eternal="false"            overflowToDisk="false"           diskSpoolBufferSizeMB="20"            timeToIdleSeconds="300"            timeToLiveSeconds="600"           memoryStoreEvictionPolicy="LRU" /></ehcache>

Spring配置

<cache:annotation-driven cache-manager="cacheManager" /><bean id="ehcacheManagerFactory"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    <property name="configLocation" value="classpath:ehcache.xml" /></bean><bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">    <property name="cacheManager" ref="ehcacheManagerFactory" />    <property name="transactionAware" value="true" /></bean>

MenuService.java

@Servicepublic class MenuService {    @Cacheable(value="menuCache",key="'UserMenuKey'+#userid")    public List<Menu> queryMenuByUserId(String userid){        long startTime = System.currentTimeMillis();        log.info("###开始查询菜单###");        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        //这里模拟从数据库中取数据        List<Menu> menuList = new ArrayList<Menu>();        Menu m1 = new Menu(1L,"用户管理");        Menu m2 = new Menu(2L,"角色管理");        Menu m3 = new Menu(3L,"权限管理");        menuList.add(m1);        menuList.add(m2);        menuList.add(m3);        long endTime = System.currentTimeMillis();        log.info("###菜单查询结束###, 用时:{}ms",(endTime-startTime));        return menuList;    }}

Test.java

@RunWith(value=SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={    "classpath:servlet-context.xml"})@WebAppConfigurationpublic class Test {    @Autowired    private MenuService menuService;    @org.junit.Test    public void test(){        //执行第一次查询        //第一次正常从数据库中获取数据        List<Menu> list = menuService.queryMenuByUserId("1");        System.out.println("结果:"+JSONObject.toJSONString(list));        //执行第二次查询        //第二次执行查询的时候, 由于加了@Cacheable注解,如果还是执行条件是userid是1, 那么从缓存中取数据        List<Menu> list1 = menuService.queryMenuByUserId("1");        System.out.println("结果:"+JSONObject.toJSONString(list1));        System.err.println("###completed###");    }}
0 0