配置文件ehcache.xml详解(3)—<ehcache>

本文详细介绍了Ehcache配置文件ehcache.xml中的<ehcache>元素及其属性的作用,包括CacheManager资源调整、动态配置和监控等功能。此外还讨论了如何在CacheManager级别或Cache级别指定缓存内存限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<ehcache>是整个配置文件的根配置,在最外侧,其他配置元素包含在<ehcache>中

<ehcache>
  *****其他元素
</ehcache>
从ehcache.xml中找到相应注释和代码如下,配上翻译:

<?xml version="1.0" encoding="UTF-8"?>
<!--
CacheManager Configuration
==========================
An ehcache.xml corresponds to a single CacheManager一个ehcache.xml 相当于一个单个的CacheManager.

See instructions below or the ehcache schema (ehcache.xsd) on how to configure.
System property tokens can be specified in this file which are replaced when the configuration
is loaded. For example multicastGroupPort=${multicastGroupPort} can be replaced with the
System property either from an environment variable or a system property specified with a
command line switch such as -DmulticastGroupPort=4446. Another example, useful for Terracotta
server based deployments is <terracottaConfig url="${serverAndPort}"/ and specify a command line
switch of -Dserver36:9510
The attributes of <ehcache> are<ehcache>的属性如下:

* name - an optional name for the CacheManager.  The name is optional and primarily used
for documentation or to distinguish Terracotta clustered cache state.  With Terracotta
clustered caches, a combination of CacheManager name and cache name uniquely identify a
particular cache store in the Terracotta clustered memory.

(name是CacheManager的可选名称。这个名称起初主要是用于文档记录或辨别Terracotta集群状态。对于Terracotta集群的缓存,一组CacheManager名称和cache名称唯一的鉴定了一个特定的存储于Terracotta集群存储器的缓存


* updateCheck - an optional boolean flag specifying whether this CacheManager should check
for new versions of Ehcache over the Internet.  If not specified, updateCheck="true".

一个可选的boolean标识符,指定这个CacheManager是否通过Internet检查Ehcache的新版本。如果没有特别指明,updateCheck="true"


* dynamicConfig - an optional setting that can be used to disable dynamic configuration of caches
associated with this CacheManager.  By default this is set to true - i.e. dynamic configuration
is enabled.  Dynamically configurable caches can have their TTI, TTL and maximum disk and
in-memory capacity changed at runtime through the cache's configuration object.

一个可选设置,能够使与这个CacheManager相关联的动态配置失活。这个设置的默认值是true-例如,动态配置是激活的。动态配置的缓存通过缓存的配置对象让他们的TTI, TTL maximum disk in-memory capacity在运行时改变


* monitoring - an optional setting that determines whether the CacheManager should
automatically register the SampledCacheMBean with the system MBean server.
Currently, this monitoring is only useful when using Terracotta clustering and using the
Terracotta Developer Console. With the "autodetect" value, the presence of Terracotta clustering
will be detected and monitoring, via the Developer Console, will be enabled. Other allowed values
are "on" and "off".  The default is "autodetect". This setting does not perform any function when
used with JMX monitors.

一个可选的设置,决定CacheManager是否应该自动的用系统MBean服务器注册SampledCacheMBean。当下,这个监测。只有当使用Terracotta集群和使用Terracotta Developer Console时才有用。使用"autodetect"值,Terracotta集群的出现将被检测和监视,并通过Developer控制台激活。其他允许的值有:"on"  "off"。默认为"autodetect"。当使用JMX监测时,这个设置不会产生任何作用。


* maxBytesLocalHeap - optional setting that constraints the memory usage of the Caches managed by the CacheManager
to use at most the specified number of bytes of the local VM's heap.

可选设置, 设置由CacheManager管理的缓存,保存在java虚拟机堆内存的最大可用字节数(设置堆内存大小)


* maxBytesLocalOffHeap - optional setting that constraints the offHeap usage of the Caches managed by the CacheManager
to use at most the specified number of bytes of the local VM's offHeap memory.

(可选设置,设置由CacheManager管理的缓存,保存在非堆内存上的最大可使用的字节数)


* maxBytesLocalDisk - optional setting that constraints the disk usage of the Caches managed by the CacheManager

to use at most the specified number of bytes of the local disk.

(可选设置,设置由CacheManager管理的缓存,保存在本地磁盘上的最大可使用的字节数)


Here is an example of CacheManager level resource tuning, which will use up to 400M of heap and 2G of offHeap:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true" monitoring="autodetect"
         dynamicConfig="true" maxBytesLocalHeap="400M" maxBytesLocalOffHeap="2G">
-->
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd"
         updateCheck="true" monitoring="autodetect"
         dynamicConfig="true">  

</ehcache>  

其他注意事项:
一、指定可用内存
Ehcache规定我们在使用一个Cache时必须在CacheManager级别指定可用的内存大小或者是在Cache级别指定可用的内存大小或所允许存放的元素的最大数量。在CacheManager级别指定的内存大小是其内部所有Cache一起所能使用的内存的最大量。CacheManager级别指定内存大小是通过maxBytesLocalHeap来指定的,如:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
   maxBytesLocalHeap="500M">
</ehcache>
1.上面指定了我们的CacheManager所能使用的最大内存是500M
2.CacheManager级别指定了内存大小后我们在Cache上也可以指定其能使用的最大内存,但不能指定其所能存储元素的最大数量
另外,如果我们的CacheManager没有指定可用的内存大小,我们可以通过maxBytesLocalHeap在Cache级别指定可用的内存大小,或者通过maxEntriesLocalHeap在Cache级别指定允许储存元素的最大数量但是maxEntriesLocalHeap和maxBytesLocalHeap不能同时使用。如下在CacheManager级别使用maxBytesLocalHeap,然而在Cache级别使用maxEntriesLocalHeap是不行的。
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
   maxBytesLocalHeap="500M"><cache name="ttt" maxEntriesLocalHeap="10000"/>
</ehcache>



参考文章:
http://blog.csdn.net/lwx2615/article/details/5624388  
http://www.cnblogs.com/crazylqy/p/4238265.html




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值