`
zzc1684
  • 浏览: 1214762 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

Ehcache缓存之web应用实例

阅读更多

Ehcache缓存之web应用实例

前面学习了缓存的基本配置和使用,下面继续学习如何缓存一个页面

我搭建的环境是 spring+freemarker+Maven  ,没有使用hibernate配置缓存 ,想先弄个简单的页面缓存看看

需要jar包:

        <dependency>
          <groupId>net.sf.ehcache</groupId>
          <artifactId>ehcache-web</artifactId>
          <version>2.0.4</version>
        </dependency>

可以加上这个仓库  https://oss.sonatype.org/content/groups/sourceforge/

我测试ehcache配置如下

 

<ehcache updateCheck="false" dynamicConfig="false">
    
    
    !-- Sets the path to the directory where cache .data files are created.
 
         If the path is a Java System Property it is replaced by
         its value in the running VM.
 
         The following properties are translated:
         user.home - User's home directory
         user.dir - User's current working directory
         java.io.tmpdir - Default temp file path -->
    <diskStore path="d:\\ehcache\\tmpdir"/>
    
    <cacheManagerEventListenerFactory class="" properties=""/>
 
    <!--Default Cache configuration. These will applied to caches programmatically created through
        the CacheManager.
 
        The following attributes are required for defaultCache:
 
        maxInMemory       - Sets the maximum number of objects that will be created in memory
        eternal           - Sets whether elements are eternal. If eternal,  timeouts are ignored and the element
                            is never expired.
        timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
                            if the element is not eternal. Idle time is now - last accessed time
        timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
                            if the element is not eternal. TTL is now - creation time
        overflowToDisk    - Sets whether elements can overflow to disk when the in-memory cache
                            has reached the maxInMemory limit.
 
        -->
    <defaultCache
        maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="1200000"
        timeToLiveSeconds="1200000"
        overflowToDisk="true"
        />
 
    <cache name="icache-global"
        maxElementsInMemory="1"
        maxElementsOnDisk="1"
        eternal="true"
        timeToIdleSeconds="1800"
        timeToLiveSeconds="1800"
        overflowToDisk="true"
        />
    
    <!-- 测试用 -->
    <cache name="<span style="color:#e53333;font-weight:bold;">SimplePageCachingFilter</span>"
           maxElementsInMemory="10"    
           maxElementsOnDisk="10"      
           eternal="false"
           overflowToDisk="true"
           timeToIdleSeconds="100"
           timeToLiveSeconds="30"
           memoryStoreEvictionPolicy="LFU"
            />
    
    <cache name="SimplePageFragmentCachingFilter"
           maxElementsInMemory="1"
           maxElementsOnDisk="1"
           eternal="false"
           overflowToDisk="true"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
            />
            
</ehcache>

 首先学习下缓存配置中几个重要的参数配置

<cache name="SimplePageCachingFilter"
maxElementsInMemory="10"      //这个是表示SimplePageCachingFilter缓存在内存中的element数量
maxElementsOnDisk="10"    //这个是表示SimplePageCachingFilter缓存在磁盘中的element数量
       
eternal="false"      //说明该缓存会死亡
overflowToDisk="true"   //设置是否溢出是是否存到磁盘上
timeToIdleSeconds="10"    //多长时间不访问该缓存,那么ehcache 就会清除该缓存.
timeToLiveSeconds="30"    //缓存的存活时间,从开始创建的时间算起.
memoryStoreEvictionPolicy="LFU"   //缓存的清空策略
/>       

ehcache 中缓存的3 种清空策略:

1 FIFO ,first in first out ,这个是大家最熟的,先进先出,不多讲了

2 LFU , Less Frequently Used ,就是上面例子中使用的策略,直白一点就是讲一直以来最少被使用的.如上面所讲,缓存的元素有一个hit 属性,hit 值最小的将会被清出缓存.

2 LRU ,Least Recently Used ,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存.

 缓存的其他知识可以去百度去,这里介绍缓存的存储方式和一些原理

下面是web.xml方面的配置

 

 

<!-- ehcache -->
    <filter>
        <filter-name>SimplePageCachingFilter</filter-name>
        <filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter
        </filter-class>
      </filter>
    
  <!-- This is a filter chain. They are executed in the order below.
  Do not change the order. -->
     <filter-mapping>
        <filter-name><span style="color:#e53333;font-weight:bold;">SimplePageCachingFilter</span></filter-name>
        <url-pattern>*.do</url-pattern>
      </filter-mapping>

 注意: ehcache.xml文件中配置的缓存名称 必须和web.xml配置的缓存filter名字一致 不然会抛出找不到配置的异常 ;并且必须把ehcache.xml放在class根目录下。

接下来就是写一个Controller了

 

@Controller
public class EhcacheController extends BaseAction {
    
    
    
    @RequestMapping(value = "ehcache/save.do" ,method=RequestMethod.GET)
    public String index(Locale locale, Model model,HttpServletRequest request,
            HttpServletResponse response) throws Exception{
        logger.info("welcome  to  daotie home ,the client locale is "+ locale.toString());
        
        
          
        return "ehcache/show";
    }
    
}

 我写的是 spring的mvc实现,接下来我们就需要看ehcache/show.ftl页面看看是否用了缓存

你可以再访问一次后,里面修改show.ftl页面内容,会发现没有变化,等过了30秒(因为我在缓存中配置的SimplePageCachingFilter的缓存存活时间是30秒)后就变了,说明缓存起了作用了。

分享到:
评论

相关推荐

    ehcache项目缓存技术

    Ehcache是由Terracotta公司开发的内存缓存框架,它被广泛应用于各种Java应用程序,包括Web应用、大数据处理、数据库连接池等场景。Ehcache提供了一个简单易用的API,允许开发者轻松地在应用中集成缓存功能,从而减少...

    Mybatis入门实例(二)——添加ehcache缓存支持

    在本篇《Mybatis入门实例(二)——添加ehcache缓存支持》中,我们将深入探讨如何在Mybatis框架中集成Ehcache作为二级缓存,以提高数据访问的效率和性能。Ehcache是一个开源的Java分布式缓存,广泛用于缓存应用程序中...

    Java流行ehcache缓存

    - **初始化缓存**:在应用启动时,通过Ehcache API创建并初始化缓存实例。 4. **Ehcache配置** - **创建缓存区域**:定义缓存的名称和大小,例如`&lt;cache name="myCache" maxEntriesLocalHeap="1000"&gt;`。 - **...

    ehcache1.6 和 ehcache-web-2.0.4

    它也提供了监控和管理Web应用程序中Ehcache的工具,帮助开发者了解缓存的使用情况,优化性能。 SLF4J(Simple Logging Facade for Java)是一个日志抽象层,允许用户在部署时插入所需的日志实现。slf4j-api-1.7.21....

    缓存框架-Ehcache学习笔记

    例如,Ehcache 提供了一个基于 Web 的管理界面,可以实时查看缓存状态。 ### 总结 Ehcache 作为一款强大且灵活的缓存框架,不仅提供了高效的内存缓存,还支持磁盘存储和分布式缓存,广泛应用于 Java 开发领域。...

    项目优化之Ehcache页面缓存

    本文将深入探讨Ehcache的工作原理、优势、配置以及如何将其应用于页面缓存,同时结合提供的代码案例和文档,帮助你更全面地理解和运用Ehcache。 1. Ehcache简介 Ehcache是由Terracotta公司开发的开源缓存解决方案,...

    ehcache 缓存技术

    **EHCache缓存技术** EHCache是一款高性能、轻量级的Java缓存框架,它广泛应用于各种Java应用程序中,特别是需要提升数据访问速度和减少数据库负载的场景。EHCache是基于内存的,但同时支持持久化,能有效地提高...

    Ehcache 简单的监控

    Ehcache是一个开源的、高性能的缓存解决方案,广泛应用于Java应用程序中,以提高数据访问的速度和效率。本文将深入探讨Ehcache的简单监控,帮助开发者更好地理解其工作原理和性能状态。 首先,了解Ehcache的核心...

    Ehcache 整合Spring 使用页面、对象缓存

    - 获取Ehcache的相关jar包,包括`ehcache-core`用于对象和数据缓存,以及`ehcache-web`用于页面缓存。 - 将这些jar包添加到项目的lib目录中。 - 创建或引入配置文件`ehcache.xml`,此文件定义了缓存的配置细节。 ...

    高并发缓存器(基于ehcache)

    Ehcache支持本地缓存、分布式缓存和 terracotta 集群模式,广泛应用于Java Web应用和企业级系统中。其主要特性包括: 1. **内存和磁盘存储**:Ehcache允许将数据存储在内存中,当内存不足时,可以自动将部分数据...

    ehcache缓存教程

    **Ehcache缓存教程** Ehcache是一个广泛使用的开源Java缓存库,适用于各种Java应用程序,特别是...通过阅读提供的“ehcache缓存页面.doc”文档,可以获取更详细的配置示例和使用技巧,进一步提升你的Ehcache应用能力。

    细谈Ehcache页面缓存的使用

    通过对Ehcache页面缓存的具体配置和应用实例的分析,我们可以看到,合理利用缓存技术不仅可以有效提升系统的响应速度,还能大大减轻数据库的压力。当然,在实际操作过程中还需要根据具体的业务场景和性能需求来调整...

    ehcache jar包

    在Java应用程序中,尤其是在Web应用中,`ehcache`被用来存储和检索频繁访问的数据,从而减少数据库的负载,提高响应速度。以下是关于`ehcache`的一些核心知识点: 1. **缓存概念**:缓存是一种存储机制,用于临时...

    Ehcache Java 缓存框架.zip

    它是一个轻量级、高性能且可扩展的缓存解决方案,广泛应用于各种Java应用程序中,包括Web应用、企业服务和桌面应用等。Ehcache的核心特性包括内存和磁盘存储、缓存分区、缓存预热以及过期策略等。 首先,我们来了解...

    Spring基于注解的缓存配置--web应用实例

    在本实例中,我们将深入探讨如何在Spring框架中利用注解来实现缓存配置,特别是在Web应用程序中的实际应用。Spring Cache是一个强大的功能,它允许我们高效地管理应用程序中的数据,减少不必要的数据库查询,提高...

    Spring Boot 简单使用EhCache缓存框架的方法

    EhCache 是一个流行的缓存框架,广泛应用于 Java 应用程序中。在本文中,我们将介绍如何在 Spring Boot 应用程序中使用 EhCache 缓存框架。 首先,我们需要在 build.gradle 文件中添加 EhCache 的依赖项: ```...

    springmvc+ehcache 实例

    在IT行业中,Spring MVC是一个广泛使用...通过这个实例,你可以深入理解Spring MVC和Ehcache的集成,并了解到缓存在提高Web应用性能上的重要作用。同时,这也是一个很好的实战练习,帮助你提升在实际项目中的开发能力。

    SpringBoot+Layui搭建的学生管理系统,加入了shiro安全框架和Ehcache缓存框架.zip

    【标题】"SpringBoot+Layui搭建的学生管理系统,加入了shiro安全框架和Ehcache缓存框架"是一个综合性的项目,它展示了如何利用Java技术栈构建一个实用的管理信息系统。这个项目的核心技术包括SpringBoot、Layui、...

Global site tag (gtag.js) - Google Analytics