# 7.6 Introduction to ehcache.xml
# 7.6 Introduction to ehcache.xml
The use of EhCache requires support from the ehcache.xml
configuration file. This configuration file contains many cache
nodes. Each cache
node will have a name
attribute, such as <cache name="blog" …>
. This name
attribute is required when retrieving values using CacheKit.
Other configuration options such as eternal
, overflowToDisk
, timeToIdleSeconds
, and timeToLiveSeconds
can be found in the EhCache official documentation.
Here is a brief explanation of these attributes:
eternal
: If set totrue
, the elements in the cache will never expire. If set tofalse
, the elements would expire based on the time specified intimeToIdleSeconds
andtimeToLiveSeconds
.overflowToDisk
: When set totrue
, it specifies that the cache can overflow to disk storage when it reaches the in-memory limit.timeToIdleSeconds
: This is the time to idle for an element before it expires, i.e., the maximum amount of time an element can exist in the cache without being accessed. The element expires after this time.timeToLiveSeconds
: This is the time to live for an element before it expires, i.e., the maximum time an element can exist in the cache, whether or not it has been accessed. The element expires after this time.
For a complete list of attributes and their detailed explanation, consult the official EhCache documentation.
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false" monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir"/>
<!--
Default Cache configuration. These settings will be applied to caches
created programmatically using CacheManager.add(String cacheName).
This element is optional, and using CacheManager.add(String cacheName) when
its not present will throw CacheException
The defaultCache has an implicit name "default" which is a reserved cache name.
-->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
diskSpoolBufferSizeMB="30"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
statistics="false">
<persistence strategy="localTempSwap"/>
</defaultCache>
<!--Predefined caches. Add your cache configuration settings here.
If you do not have a configuration for your cache a WARNING will be issued when the
CacheManager starts
The following attributes are required for defaultCache:
name - Sets the name of the cache. This is used to identify the cache. It must be unique.
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.
-->
<!-- Cache for login user information. Will be cleared after 30 minutes of inactivity, otherwise, it keeps the login state.
The expiration time refers to the expireAt in the session table. This cache is only used for login_log statistics
and performance improvement (no need to always read the database session table). -->
<cache name="loginAccount"
maxEntriesLocalHeap="90000"
eternal="false"
timeToIdleSeconds="1800"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
<!-- allAccounts cache for all accounts, used for joining other models to nickName, avatar -->
<cache name="allAccounts"
maxEntriesLocalHeap="90000"
eternal="true"
overflowToDisk="false"
/>
<!-- weixinPayResendNotify is used to prevent duplicate data processing caused by repeated asynchronous notifications in WeixinPayService -->
<cache name="weixinPayResendNotify"
maxEntriesLocalHeap="3000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
<!-- newsFeedPage cache -->
<cache name="newsFeedPage"
maxEntriesLocalHeap="90000"
eternal="true"
overflowToDisk="false"
/>
<!-- referMePage cache, allows idle time for one hour (3600 seconds) -->
<cache name="referMePage"
maxEntriesLocalHeap="3000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
<!-- remind cache, allows idle time for one hour (3600 seconds) -->
<cache name="remind"
maxEntriesLocalHeap="3000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
<!-- The key generation rule for pageViewIp is actionKey + ip, for example:
1: "/project" + ip
2: "/share" + ip
3: "/feedback" + ip
It is used to get the specific article id corresponding to the current ip. To avoid malicious brushing of rankings,
only update the visitCount when the id does not exist.
The visitCount field in xxx_page_view is used for article sorting. -->
<cache name="pageViewIp"
maxEntriesLocalHeap="90000"
eternal="false"
timeToIdleSeconds="60"
timeToLiveSeconds="60"
overflowToDisk="false"
diskPersistent="false"
/>
<cache name="projectPageView"
maxEntriesLocalHeap="90000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"
/>
<cache name="sharePageView"
maxEntriesLocalHeap="90000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"
/>
<cache name="feedbackPageView"
maxEntriesLocalHeap="90000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"
/>
<!-- index cache -->
<cache name="index"
maxEntriesLocalHeap="300"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="false"
/>
<!-- Here are three sample caches for configuration purposes only ============================================ -->
<!--
Sample caches. Following are some example caches. Remove these before use.
-->
<!--
Sample cache named sampleCache1
This cache contains a maximum in memory of 10000 elements, and will expire
an element if it is idle for more than 5 minutes and lives for more than
10 minutes.
If there are more than 10000 elements it will overflow to the
disk cache, which in this configuration will go to wherever java.io.tmp is
defined on your system. On a standard Linux system this will be /tmp"
-->
<cache name="sampleCache1"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000"
eternal="false"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
<persistence strategy="localTempSwap"/>
</cache>
<!--
Sample cache named sampleCache2
This cache has a maximum of 1000 elements in memory. There is no overflow to disk, so 1000
is also the maximum cache size. Note that when a cache is eternal, timeToLive and
timeToIdle are not used and do not need to be specified.
-->
<cache name="sampleCache2"
maxEntriesLocalHeap="1000"
eternal="true"
memoryStoreEvictionPolicy="FIFO"
/>
<!--
Sample cache named sampleCache3. This cache overflows to disk. The disk store is
persistent between cache and VM restarts. The disk expiry thread interval is set to 10
minutes, overriding the default of 2 minutes.
-->
<cache name="sampleCache3"
maxEntriesLocalHeap="500"
eternal="false"
overflowToDisk="true"
diskPersistent="true"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
diskExpiryThreadIntervalSeconds="1"
memoryStoreEvictionPolicy="LFU">
</cache>
</ehcache>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
ehcache.xml
file provides a comprehensive configuration for EhCache in a JFinal project. This file outlines various cache policies and strategies to handle different kinds of data.
Here's a brief summary of some of the noteworthy aspects:
Default Cache Configuration: The
defaultCache
block specifies the default settings for all caches unless overridden. These include maximum entries in the local heap and disk, eviction policies, time-to-live (TTL), and time-to-idle (TTI) settings.Custom Cache Configurations: You've defined several custom cache blocks like
loginAccount
,allAccounts
,weixinPayResendNotify
, etc. Each of these blocks has a specific configuration tailored to the data it will hold.TTL and TTI: You've set
timeToLiveSeconds
andtimeToIdleSeconds
for several caches. These settings control how long the data stays in the cache before it's considered stale and needs to be evicted.Overflow to Disk: Some caches are configured to overflow to disk when they reach a certain size in memory, while others are not. This helps in efficiently utilizing system resources.
Eternal: For some caches like
allAccounts
, you've seteternal="true"
, which means the data in these caches will not expire.Eviction Policy: The eviction policy like LRU (Least Recently Used), FIFO (First In First Out), or LFU (Least Frequently Used) specifies which entries to remove from the cache when it gets full.
Sample Caches: At the end, you've also included some sample cache configurations for reference.
Overall, this is a well-structured and thought-out configuration file that caters to various caching requirements. Just make sure to remove or comment out the sample cache configurations before deploying it to production to avoid any unintended side effects.