Ehcache学习总结三:Ehcache页面缓存

一:需要jar包

1、ehcache-2.10.2.jar

2、ehcache-web-2.0.4.jar

3、slf4j-api-1.5.8.jar

4、slf4j-log4j12-1.5.8.jar

5、log4j-1.2.15.jar

由于我是结合Spring操作,所以还需导入Spring相关包

二:在项目根路径src目录下加入Ehcache配置文件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">

     <!-- 默认缓存 -->
    <defaultCache
           maxElementsInMemory="1000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           overflowToDisk="false"/>  

    <!-- 指定名字缓存缓存 -->
    <cache name="studyCache"
           maxElementsInMemory="1000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           overflowToDisk="false"
           memoryStoreEvictionPolicy="LRU"/> 

      <!-- 指定名字缓存缓存 -->
    <cache name="pageCache"
           maxElementsInMemory="1000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           overflowToDisk="false"
           memoryStoreEvictionPolicy="LRU"/> 

</ehcache>

三:在web.xml中加入Ehcache页面过滤器

<filter>
    <filter-name>CachePageCachingFilter</filter-name>
    <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class>
    <init-param>
        <param-name>cacheName</param-name>
        <param-value>pageCache</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>CachePageCachingFilter</filter-name>
    <url-pattern>/pageEhcache/*</url-pattern>
  </filter-mapping>

这样凡是访问路径带 pageEhcache 都会被缓存起来

四:java代码

package com.ehcache.study.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ehcache.study.beans.TradeBlotter;
import com.ehcache.study.service.IAppTradeBlotterService;

@Scope("prototype")
@Controller
public class IndexCtrl {

    @Autowired
    private IAppTradeBlotterService appTradeBlotterService;

    @RequestMapping("/ehcacheStudy/myTradeBlotter")
    public ModelAndView myTradeBlotter(HttpServletRequest request,HttpServletResponse response,ModelAndView view)throws Exception{
        view.setViewName("/WEB-INF/ehcacheStudy/myTradeBlotter.jsp");

        System.out.println("执行myTradeBlotter方法");

        String appNo = request.getParameter("appNo");
        String opId = request.getParameter("opId");
        String fundAcct = request.getParameter("fundAcct");
        String fundId = request.getParameter("fundId");

        long startTime = System.currentTimeMillis();
        List<TradeBlotter> tradeBlotterList = appTradeBlotterService.qryTradeBlotterList(appNo, opId, fundAcct, fundId);
        long endTime = System.currentTimeMillis(); 

        long excuteTime = endTime-startTime;
        System.out.println("excuteTime=" + excuteTime);

        view.addObject("tradeBlotterList", tradeBlotterList);

        return view;
    }

    @RequestMapping("/pageEhcache/myCachePage")
    public ModelAndView myCachePage(HttpServletRequest request,HttpServletResponse response,ModelAndView view)throws Exception{
        view.setViewName("/WEB-INF/pageEhcache/myCachePage.jsp");

        System.out.println("执行myCachePage方法");

        String appNo = request.getParameter("appNo");
        String opId = request.getParameter("opId");
        String fundAcct = request.getParameter("fundAcct");
        String fundId = request.getParameter("fundId");

        long startTime = System.currentTimeMillis();
        List<TradeBlotter> tradeBlotterList = appTradeBlotterService.qryTradeBlotterList(appNo, opId, fundAcct, fundId);
        long endTime = System.currentTimeMillis(); 

        long excuteTime = endTime-startTime;
        System.out.println("excuteTime=" + excuteTime);

        view.addObject("tradeBlotterList", tradeBlotterList);

        return view;
    }

}

结果:当执行http://localhost:8080/pageEhcache/myCachePage?opId=Mobile11请求时,页面被缓存

五:上面代码已经能够缓存页面了,如果还需结合缓存方法则在Spring配置applicationcontext.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
           http://www.springframework.org/schema/cache
           http://www.springframework.org/schema/cache/spring-cache-3.2.xsd">

    <mvc:annotation-driven/>
    <context:annotation-config/>
    <context:component-scan base-package="com.ehcache.study"/>

    <!-- 数据库配置 -->
    <bean id="dataSourceSales" class="org.apache.commons.dbcp.BasicDataSource" >
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
        <property name="url" value="jdbc:oracle:thin:@10.1.50.25:1521:tadev"></property>
        <property name="username" value="fund_sales_dev"></property>
        <property name="password" value="pass4tadev"></property>
        <property name="maxActive" value="100"></property>
        <property name="maxIdle" value="30"></property>
        <property name="maxWait" value="30000"></property>
        <property name="defaultAutoCommit" value="true"></property>
    </bean>

    <!-- sqlSessionFactory -->
    <bean id="sqlSessionFactorySales" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSourceSales"></property>
        <property name="mapperLocations" value="classpath*:com/ehcache/study/dao/**/*.xml"></property>
        <property name="configLocation" value="classpath:MybatisConfig.xml"></property>
    </bean>
    <bean name="salesConfig" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ehcache.study.dao"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactorySales"></property>
    </bean>

    <!-- 此处为Ehcache相关配置 -->
    <cache:annotation-driven cache-manager="cacheCacheManager"/>
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml" />
        <property name="cacheManagerName" value="myCacheManager"></property>
    </bean>
    <bean id="cacheCacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager"  ref="cacheManagerFactory"/>
    </bean>

</beans>

此处注意要加上 <property name="cacheManagerName" value="myCacheManager"></property> ,value的值是自己随便定义, 此处如果不写的话会报already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following 错误

六:service层代码

package com.ehcache.study.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.ehcache.study.beans.TradeBlotter;
import com.ehcache.study.dao.IAppTradeBlotterDao;
import com.ehcache.study.service.IAppTradeBlotterService;

@Service
public class AppTradeBlotterServiceImpl implements IAppTradeBlotterService{

    @Autowired
    private IAppTradeBlotterDao appTradeBlotterDao;

    @Cacheable(value="studyCache")
    public List<TradeBlotter> qryTradeBlotterList(String appNo, String opId,String fundAcct, String fundId) {
        System.out.println("执行查询数据库");
        return appTradeBlotterDao.selectTradeBlotterList(appNo, opId, fundAcct, fundId);
    }

}

以上就是Ehcache页面缓存和方法缓存,仅供参考,如有不当之处,欢迎大家多多指点!

时间: 2024-08-02 00:15:23

Ehcache学习总结三:Ehcache页面缓存的相关文章

Ehcache学习(三)ehcache与springAOP拦截器实例

本次我们使用springAOP+ehcache结合来实现数据的缓存,我们可以 Cache 系统中 Service 或则 DAO 层的 get/find 等方法返回结果,如果数据更新( 使用Create/update/delete 方法), 则刷新 cache 中相应的内容. Aop中最常见的就是拦截器,那么我们首先需要创建的就是一个拦截器: ?  MethodCacheInterceptor <span style="font-family:Microsoft YaHei;font-siz

Ehcache学习总结(1)--Ehcache入门介绍

Ehcache是现在最流行的纯Java开源缓存框架,配置简单.结构清晰.功能强大,最初知道它,是从hibernate的缓存开始的.网上中文的EhCache材料以简单介绍和配置方法居多,如果你有这方面的问题,请自行google:对于API,官网上介绍已经非常清楚,请参见官网:但是很少见到特性说明和对实现原理的分析,因此在这篇文章里面,我会详细介绍和分析EhCache的特性,加上一些自己的理解和思考,希望对缓存感兴趣的朋友有所收获. 一.特性一览,来自官网,简单翻译一下: 1.快速轻量 过去几年,诸

Ehcache学习笔记——初识Ehcache

1. Ehcache 的主要特性和集群方案 EHCache EHCache 是一个纯 java 的在进程中的缓存,是 Hibernate 中默认的 CacheProvider,最小的依赖性, 全面的文档和测试,最新版本为 2.0.1. 缓存应用在多个领域并发挥作用,ehcache 可应用于数据库访问缓存,安全认证缓存,web 缓存,soap 和 RESTFul 服务缓存,应用程序持久对象缓存以及分布式缓存. (1)EhCache 的主要特性有: a) 快速: b) 简单: c)多种缓存策略: d

AngularJS学习笔记(三) 单页面webApp和路由(ng-route)

就我现在的认识,路由($route)这个东西(也许可以加上$location)可以说是ng最重要的东西了.因为angular目前最重要的作用就是做单页面webApp,而路由这个东西是能做到页面跳转的关键. 1.单页面webApp 为什么叫单页面webApp?因为它是单页面的....额...关键在于我们看到的不少webApp是多页面的啊,可以从一页跳到另一页.嗯,看起来是这样,不过实际上这几页实际还是一个页面,因为它们是一次性加载进来的,至于为啥会有跳转效果呢?嗯,有点儿像选项卡,嗯.其实现原理在

缓存插件 EHCache 页面缓存CachingFilter

Ehcache基本用法 CacheManager cacheManager = CacheManager.create(); // 或者 cacheManager = CacheManager.getInstance(); // 或者 cacheManager = CacheManager.create("/config/ehcache.xml"); // 或者 cacheManager = CacheManager.create("http://localhost:8080

EHCache页面缓存技术

EHCache的jar包 :http://ehcache.org/downloads/catalog   这里用的是EHcache的web包. 在项目的web.xml文件中加入一个filter: <!--EHCache 页面缓存技术,该页面缓存技术用于page -> filter之间,若是局部页面缓存,           则用SimplePageFragmentCachingFilter,但应注意局部页面要被其他页面 jsp:include     -->     <filter

Ehcache 页面缓存

页面缓存,毫无疑问,几乎所有的网站的首页都是访问率最高的,而首页上的数据来源又是非常广泛的,大多数来自不同的对象,而且有可能来自不同的db ,所以给首页做缓存是一个不错的主意,那么主页的缓存策略是什么样子的呢,我认为应该是某个固定时间之内不变的,比如说2 分钟更新一次.那么这个缓存应该做在什么地方呢,让我们来看一下,假设您的应用的结构是page-filter-action-service-dao-db ,这个过程中的- 的地方都是可以做缓存的地方,根据页面缓存的特征,应该把页面缓存做到尽量靠近客

ehcache、memcache、redis三大缓存比较

最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考!  Ehcache 在Java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS中取出来的高花费.高延迟采取的一种缓存方案.正因为Ehcache具有健壮性(基于java开发).被认证(具有apache 2.0  license).充满特色(稍后会详细介绍),所以被用于大型复杂分布式web application的各个节点中. 什么特色? 1.  够快 Ehcache的发行有一段

Hibernate学习笔记(三) — Hibernate 的一级缓存意义

什么是缓存? 缓存说白了,就是应用程序向数据库要数据,然后把一些数据,临时的放在了内存的区域中,第二次再要数据的时候,直接从内存中拿即可. 缓存需要解决的事情: 1.能把数据放入缓存 2.能把数据从缓存中取出来 3.如果缓存中的数据发生变化,需要把数据同步到数据库中 4.把数据库中的数据同步到缓存中 5.hits命中率低的对象应该及时从缓存中移走 分布式缓存: 为什么会有分布式缓存? 应用程序运行在服务器上,并发访问时,服务器压力过大,分布式缓存就是来分担服务器压力的. 分布式缓存之间的数据是同