maven搭建ssm分模块框架+ehcache (myeclipse版) (五)

来源:互联网 发布:jordan淘宝官方旗舰店 编辑:程序博客网 时间:2024/04/28 16:21

接着前面的四篇,今天将ehcache引入,我自己对ehcache的理解停留在缓存数据的 层面,第一次运行是走sql查询出数据,当第二次调用的时候,则是取的缓存数据。既然会缓存数据,那么如果数据是经常变动的话,就可能不适合缓存了….因为变动的数据 肯定就需要经常刷新缓存了。不过具体的使用场景 还是要具体分析…. 我这边只是大概说一下引入和使用的方式。
首先还是 ssmDemo父工程的pom 引入ehcache的相关jar包
pom.xml

        <!-- ehcache -->        <dependency>            <groupId>net.sf.ehcache</groupId>            <artifactId>ehcache</artifactId>            <version>2.8.3</version>        </dependency>        <dependency>            <groupId>net.sf.ehcache</groupId>            <artifactId>ehcache-core</artifactId>            <version>2.6.9</version>        </dependency>

接下来只需要在ssmDemo-service层 添加2个xml文件
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"         updateCheck="false">    <diskStore path="java.io.tmpdir"/><!--       name:缓存名称。       maxElementsInMemory:缓存最大个数。       eternal:对象是否永久有效,一但设置了,timeout将不起作用。       timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。                仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。       timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。                    仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。       overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。       diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。       maxElementsOnDisk:硬盘最大缓存个数。       diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts                 of the Virtual Machine. The default value is false.       diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。       memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。                        默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。       clearOnFlush:内存数量最大时是否清除。    -->    <defaultCache            maxElementsInMemory="10000"            eternal="false"            timeToIdleSeconds="60"            timeToLiveSeconds="60"            overflowToDisk="true"            maxElementsOnDisk="10000000"            diskPersistent="false"            diskExpiryThreadIntervalSeconds="120"            memoryStoreEvictionPolicy="LRU"            />   <cache name="userCache" maxElementsInMemory="10000" eternal="false"           timeToIdleSeconds="20" timeToLiveSeconds="20" overflowToDisk="false"           diskPersistent="false" diskExpiryThreadIntervalSeconds="1">           </cache></ehcache>

spring-ehcache.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"        xmlns:cache="http://www.springframework.org/schema/cache"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd         http://www.springframework.org/schema/cache         http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">    <!-- ehcache 缓存集成配置 -->    <bean id="ehCacheManager"        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">        <property name="configLocation" value="classpath:ehcache.xml" />        <property name="shared" value="false" />    </bean>    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">        <property name="cacheManager" ref="ehCacheManager" />    </bean>    <cache:annotation-driven cache-manager="cacheManager" /></beans>

我使用的是注解缓存的方式
缓存注解有以下三个:
@Cacheable @CacheEvict @CachePut

相关解释的地址:
http://tom-seed.iteye.com/blog/2104430

DubboServiceImpl.java
我是直接在dubbo服务的service里 使用了一个@Cacheable

@Service("DubboService")public class DubboServiceImpl implements DubboDao{    @Autowired    private UserMapper userMapper;    @Override    public void dubboTest() {        System.out.println("web层引用service服务成功");    }    @Override    @Cacheable(value="userCache")    public User getUserById(Integer id) {        return userMapper.selectByPrimaryKey(id);    }}

我在TestMain里值跑service层,跑起来发现缓存没有生效,所以我打算从web层调到service层

ssmDemo-web
调用缓存注解是在dubboTest方法里

@Controller@RequestMapping("/test")public class TestController {    @Autowired    private DubboDao dubboDao;    @RequestMapping("/testPage")    public String testPage(){        return "test";    }    @RequestMapping("/dubbo")    public String dubboTest(){        System.out.println("调用 service层的dubbo服务开始");        dubboDao.getUserById(1);        return "dubbo";    }}

接下来就是用tomcat启动web层,将TestMain跑起来
访问localhost:8080/ssmDemo-web/test/dubbo
可能会遇到报错
这里写图片描述
User.java需要实现Serializable接口

要测试缓存是否生效的话,我使用的方法可能比较笨,就是访问两次上面那个地址,第一次会看到tomcat后台打印出了sql语句,第二次访问会发现没有打印sql,但是数据还是查询出来了。
再次判断缓存注解是否生效,可以将dubboServiceImpl的缓存注解去掉,然后再访问两次该地址,看后台的日志是否有打印两次sql

代码下载地址:
http://download.csdn.net/detail/i_popular/9535914

0 0