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

7 篇文章 0 订阅
6 篇文章 0 订阅

接着前面的四篇,今天将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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本工程用于研究如何借助Ehcache缓存框架实现对页面的缓存 本工程编码方式:UTF-8 本工程开发工具:MyEclipse 说明: 1、ehcache.xml和ehcache.xsd两个文件可以在下在下载下来的名为“ehcache-core-x.x.x-distribution.tar.gz”压缩文件中找到 2、由于要实现Ehcache缓存页面,所以必须要添加“ehcache-web-2.0.4.jar” jar包,该jar包主要用于辅助Ehcache实现页面缓存 注意: 本web工程的发布不要使用Tomcat7,否则会出现如下异常: 2015-3-25 9:53:50 org.apache.catalina.loader.WebappClassLoader loadClass 信息: Illegal access: this web application instance has been stopped already. Could not load net.sf.ehcache.store.disk.DiskStore$KeySet. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact. java.lang.IllegalStateException at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1600) at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) at net.sf.ehcache.store.disk.DiskStore.keySet(DiskStore.java:560) at net.sf.ehcache.store.disk.DiskStorageFactory$DiskExpiryTask.run(DiskStorageFactory.java:838) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441) at java.util.concurrent.FutureTask$Sync.innerRunAndReset(FutureTask.java:317) at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:150) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$101(ScheduledThreadPoolExecutor.java:98) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:181) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:205) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619) 相关jar包下载地址: Ehcache 对象、数据缓存:http://ehcache.org/downloads/destination?name=ehcache-core-2.5.2-distribution.tar.gz&bucket=tcdistributions&file=ehcache
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值