Ehcache缓存配置

2024-04-12 09:48
文章标签 配置 缓存 ehcache

本文主要是介绍Ehcache缓存配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

Ehcache缓存配置

 

简介

 

 

Cache的配置很灵活,官方提供的Cache配置方式有好几种。你可以通过声明配置、在xml中配置、在程序里配置或者调用构造方法时传入不同的参数。

 

你可以将Cache的配置从代码中剥离出来,也可以在使用运行时配置,所谓的运行时配置无非也就是在代码中配置。以下是运行时配置的好处:

 

·   在同一个地方配置所有的Cache,这样很容易管理Cache的内存和磁盘消耗。

·   发布时可更改Cache配置。

·   可再安装阶段就检查出配置错误信息,而避免了运行时错误。

 

本文将会对ehcache.xml配置文件进行详细的阐述。在配置的时可以拷贝一个现有的ehcache.xml,如果没有请点击这里去下载。

 

 

ehcache-failsafe.xml

 

 

如果你调用了CacheManager默认构造方法去创建CacheManager的实例,此方法会到classpath中找ehcache.xml文件,否则它会到类路径下找ehcache-failsafe.xml文件。而ehcache-failsafe.xml被包含在jar包中,所有它肯定能找的到。

 

ehcache-failsafe.xml提供了一个非常简单的默认配置,这样可以使用户在没有创建ehcache.xml的情况下使用Ehcache

 

不过这样做Ehcache会提醒用户创建一个正确的Ehcache配置。

 

ehcache.xml片段:

<ehcache>
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="true"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
            />
</ehcache>

 

ehcache.xml和其他配置文件

 

 

Ehcache-1.6之前的版本,只支持ASCII编码的ehcache.xml配置文件。在Ehcach-1.6之后版本中,支持UTF8编码的ehcache.xml配置文件。因为向后兼容,所有采用ASCII编码的配置文件完全没有必要转换为UTF8

 

一个CacheManager必须要有一个XML配置。由于磁盘路径或是监听端口,多个CacheManager使用同一个配置文件时会出现错误。

 

下面是ehcache.xml具体实例以及配置指南

 

<ehcache xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance

 

·   CacheManager配置

 

DmulticastGroupPort=4446,这样可以配置监听端口。

 

·   DiskStore配置

 

如果你使用的DiskStore(磁盘缓存),你必须要配置DiskStore配置项。如果不配置,Ehcache将会使用java.io.tmpdir

 

diskStroe的“path”属性是用来配置磁盘缓存使用的物理路径的,Ehcache磁盘缓存使用的文件后缀名是.data.index

 

<disStore path=”java.io.tmpdir”/>

 

·   CacheManagerEventListener配置

 

我们通过CacheManagerEventListenerFactory可以实例化一个CacheManagerPeerProvider,当我们从CacheManageraddedremoved Cache时,将通知CacheManagerPeerProvider,这样一来,我们就可以很方面的对CacheManager中的Cache做一些统计。

 

注册到CacheManager的事件监听类名有: adding a Cacheremoving a Cache

 

<cacheManagerEventListenerFacotory class=”” properties=””/>

 

·   CacheManagerPeerProvider配置

 

在集群中CacheManager配置CacheManagerPeerProviderFactory创建CacheManagerPeerProvider。具体的实例如下:

<cacheManagerPeerProviderFactoryclass="net.sf.ehcache.distribution.

RMICacheManagerPeerProviderFactory"

properties="peerDiscovery=manual, rmiUrls=//server1:40000/sampleCache1|//server2:40000/sampleCache1| //server1:40000/sampleCache2|//server2:40000/sampleCache2"

propertySeparator="," />

·   CacheManagerPeerListener配置

 

CacheManagerPeerListener配置是用来监听集群中缓存消息的分发的。

 

<cacheManagerPeerListenerFactory

    class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"

    properties="hostName=fully_qualified_hostname_or_ip,

                port=40001,

                socketTimeoutMillis=120000"

                propertySeparator="," />

·   Cache配置

 

·           nameCache的唯一标识

·           maxElementsInMemory:内存中最大缓存对象数。

·           maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大。

·           eternalElement是否永久有效,一但设置了,timeout将不起作用。

·           overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中。

·           timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。

·           timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大。

·           diskPersistent:是否缓存虚拟机重启期数据。(这个虚拟机是指什么虚拟机一直没看明白是什么,有高人还希望能指点一二)。

·           diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。

·           diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。

·           memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。这里比较遗憾,Ehcache并没有提供一个用户定制策略的接口,仅仅支持三种指定策略,感觉做的不够理想。

 

·   Cache Exception Handling配置

 

<cacheExceptionHandlerFactory class="com.example.ExampleExceptionHandlerFactory"                              properties="logLevel=FINE"/>

 

 

总结

 

 

这里只对通用缓存的配置做了详细的阐述,至于RMI缓存和集群缓存可以参考这里。

 

下面给出几个配置示例:

 

·   Ehcache默认Cache配置

 

<defaultCache

        maxElementsInMemory="10000"

        eternal="false"

        timeToIdleSeconds="120"

        timeToLiveSeconds="120"

        overflowToDisk="true"

        diskSpoolBufferSizeMB="30"

        maxElementsOnDisk="10000000"

        diskPersistent="false"

        diskExpiryThreadIntervalSeconds="120"

        memoryStoreEvictionPolicy="LRU"

        />

 

·   SampleCache1配置

 

简单配置,在ehcache.xml文件中有此配置,在使用Ehcache前最好将其删除掉,自己配置。

 

缓存名sampleCache1,内存中最多可缓存10000Element,其中的element会在闲置5分钟或是存活10分钟之后失效。

 

超过10000element时,element将会输出到磁盘中,输出路径是java.io.tmpdir

 

<cache name="sampleCache1"

       maxElementsInMemory="10000"

       maxElementsOnDisk="1000"

       eternal="false"

       overflowToDisk="true"

       diskSpoolBufferSizeMB="20"

       timeToIdleSeconds="300"

       timeToLiveSeconds="600"

       memoryStoreEvictionPolicy="LFU"

        />

 

·   SampleCache2配置

 

Cache名为SampleCache2,内存中最多可以缓存1000element,超出1000不能输出到磁盘中。缓存是永久有效的。

 

<cache name="sampleCache2"

       maxElementsInMemory="1000"

       eternal="true"

       overflowToDisk="false"

       memoryStoreEvictionPolicy="FIFO"

        />

 

·   SampleCache3配置

 

Cache名为SampleCache3。可缓存到磁盘。磁盘缓存将会缓存虚拟机重启期的数据。磁盘缓存失效线程运行间隔时间是10分钟。

 

<cache name="sampleCache3"

       maxElementsInMemory="500"

       eternal="false"

       overflowToDisk="true"

       timeToIdleSeconds="300"

       timeToLiveSeconds="600"

       diskPersistent="true"

       diskExpiryThreadIntervalSeconds="1"

       memoryStoreEvictionPolicy="LFU"

        />

 

·   sampleDistributedCache1配置

 

 

Cache名为sampleDistributedCache1

 

<cache name="sampleDistributedCache1"

       maxElementsInMemory="10"

       eternal="false"

       timeToIdleSeconds="100"

       timeToLiveSeconds="100"

       overflowToDisk="false">

    <cacheEventListenerFactory

            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>

    <bootstrapCacheLoaderFactory

            class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>

</cache>

 

·   sampleDistributedCache2配置

 

<cache name="sampleDistributedCache2"

       maxElementsInMemory="10"

       eternal="false"

       timeToIdleSeconds="100"

       timeToLiveSeconds="100"

       overflowToDisk="false">

    <cacheEventListenerFactory

            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"

            properties="replicateAsynchronously=false, replicatePuts=false,

                        replicateUpdates=true, replicateUpdatesViaCopy=true,

                        replicateRemovals=false"/>

</cache>

 

·   sampleDistributedCache3配置

 

<!--

Sample distributed cache named sampleDistributedCache3.

This cache replicates using defaults except that the asynchronous replication

interval is set to 200ms.

-->

<cache name="sampleDistributedCache3"

       maxElementsInMemory="10"

       eternal="false"

       timeToIdleSeconds="100"

       timeToLiveSeconds="100"

       overflowToDisk="false">

    <cacheEventListenerFactory

            class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"

            properties="asynchronousReplicationIntervalMillis=200"/>

</cache>

 

文章转自:http://blog.csdn.net/mgoann/article/details/4087714

这篇关于Ehcache缓存配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/896795

相关文章

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关