上一步:教你一步步搭建ssm框架,第五步之分页插件pagehelper - 2018
合适的使用缓存可有效的提高系统的性能
pom.xml添加ehcache依赖
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.5.2</version> </dependency>
classpath目录下spring目录下新增cache_spring.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:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <bean id="springCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehcacheManager"/> </bean> <!--ehcache--> <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"/> </bean> </beans>
spring_context.xml下导入cache_spring.xml
<!-- spring ehcache --> <import resource="spring/cache_spring.xml" />
classpath下新增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" dynamicConfig="true"> <!-- 用户主目录/appEhCacheDir --> <diskStore path="user.home/appEhCacheDir"/> <!-- 默认缓存 缓存中的最大数目 硬盘最大缓存个数 ,0不限制 对象是否永久有效,一但设置了,timeout将不起作用 是否保存到磁盘,当系统当机时 缓存区大小 先进先出 是否缓存虚拟机重启期数据,保证缓存在重启后依然有效 --> <cache name="appDefault" maxEntriesLocalHeap="1024" maxEntriesLocalDisk="0" eternal="true" overflowToDisk="true" diskSpoolBufferSizeMB="20" memoryStoreEvictionPolicy="FIFO" diskPersistent="true" /> </ehcache>
创建缓存工具类EhCacheUtil.java
package com.faceghost.app.utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.Cache; import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.stereotype.Component; /** * ehcache 缓存工具类 * * @author * */ @Component public class EhCacheUtil { @Autowired() private EhCacheCacheManager ehCacheCacheManager; /** * classpath 下 ehcache.xml */ private static final String defautCache = "appDefault"; public Cache getDefault() { return ehCacheCacheManager.getCache(defautCache); } }
测试
package com.faceghost.app.utils; import org.springframework.cache.Cache; import org.springframework.cache.Cache.ValueWrapper; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class EhCacheUtilTest { static AbstractApplicationContext context = null; private static void before() { context = new ClassPathXmlApplicationContext("spring_context.xml"); } private static void after() { context.close(); } private static void test() { EhCacheUtil cacheUtil = context.getBean(EhCacheUtil.class); System.err.println(cacheUtil); Cache cache = cacheUtil.getDefault(); cache.put("test","我是缓存"); ValueWrapper vw = cache.get("test"); System.err.println(vw.get()); } public static void main(String[] args) { before(); test(); after(); } }
github地址: https://github.com/FaceGhost/Simple-SSM