Repository navigation
Expand file tree
/
Copy pathEh.java
More file actions
321 lines (296 loc) · 7.79 KB
/
Copy pathEh.java
File metadata and controls
321 lines (296 loc) · 7.79 KB
1
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jooby.ehcache;
import static java.util.Objects.requireNonNull;
import java.util.Map.Entry;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.jooby.Env;
import org.jooby.Jooby;
import org.jooby.internal.ehcache.CacheConfigurationBuilder;
import org.jooby.internal.ehcache.ConfigurationBuilder;
import com.google.inject.Binder;
import com.google.inject.name.Names;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigObject;
import com.typesafe.config.ConfigValue;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.config.Configuration;
/**
* <h1>ehcache module</h1>
* <p>
* Provides advanced cache features via <a href="http://ehcache.org">Ehcache</a>
* </p>
*
* <h2>exposes</h2>
* <ul>
* <li>A {@link CacheManager}</li>
* <li>Direct access to {@link Ehcache} instances</li>
* </ul>
*
* <h2>usage</h2>
*
* <pre>
* {
* use(new Eh());
*
* get("/", req {@literal ->} {
* CacheManager cm = req.require(CacheManager.class);
* // work with cm
*
* Ehcache ehcache = req.require(Ehcache.class);
* // work with ehcache
* });
* }
* </pre>
* <p>
* <a href="http://ehcache.org">Ehcache</a> can be fully configured from your <code>.conf</code>
* file and/or programmatically but for the time being there is no support for <code>xml</code>.
* </p>
*
* <h2>caches</h2>
* <p>
* Caches are configured via <code>.conf</code> like almost everything in {@link Jooby}.
* </p>
*
* <pre>
* ehcache.cache.mycache {
* eternal = true
* }
* </pre>
*
* Later, we can access to <code>mycache</code> with:
*
* <pre>
* {
* get("/", req {@literal ->} {
* Ehcache mycache = req.require(Ehcache.class);
* });
* }
* </pre>
*
* <h3>multiple caches</h3>
* <p>
* Multiple caches are also possible:
* </p>
*
* <pre>
* ehcache.cache.cache1 {
* maxEntriesLocalHeap = 100
* eternal = true
* }
*
* ehcache.cache.cache2 {
* maxEntriesLocalHeap = 100
* eternal = true
* }
* </pre>
*
* Later, we can access to our caches with:
*
* <pre>
* {
* get("/", req {@literal ->} {
* Ehcache cache1 = req.require("cache1", Ehcache.class);
* // ..
* Ehcache cache2 = req.require("cache2", Ehcache.class);
* // ..
* });
* }
* </pre>
*
* <h3>cache inheritance</h3>
* <p>
* Previous examples, show how to configure two or more caches, but it is also possible to inherit
* cache configuration using the <code>default</code> cache:
* </p>
*
* <pre>
*
* ehcache.cache.default {
* maxEntriesLocalHeap = 100
* eternal = true
* }
*
* ehcache.cache.cache1 {
* eternal = false
* }
*
* ehcache.cache.cache2 {
* maxEntriesLocalHeap = 1000
* }
* </pre>
*
* <p>
* Here <code>cache1</code> and <code>cache2</code> will inherited their properties from the
* <code>default</code> cache.
*
* Please note the <code>default</code> cache works as a template and isn't a real/usable cache.
* </p>
*
* <h2>session store</h2>
* <p>
* This module provides an {@link EhSessionStore}. In order to use the {@link EhSessionStore} all
* you have to do is define a <code>session</code> cache:
* </p>
*
* <pre>
* ehcache.cache.session {
* # cache will expire after 30 minutes of inactivity
* timeToIdle = 30m
* }
* </pre>
*
* And then register the {@link EhSessionStore}:
*
* <pre>
* {
* session(EhSessionStore.class);
* }
* </pre>
*
* <h2>configuration</h2>
* <p>
* Configuration is done in one of two ways: 1) via <code>.conf</code>; or 2)
* <code>programmatically:</code>.
* </p>
*
* <h3>via .conf file</h3>
*
* <pre>
*
* ehcache {
*
* defaultTransactionTimeout = 1m
*
* dynamicConfig = true
*
* maxBytesLocalDisk = 1k
*
* maxBytesLocalHeap = 1k
*
* maxBytesLocalOffHeap = 1m
*
* monitor = off
*
* # just one event listener
* cacheManagerEventListenerFactory {
* class = MyCacheEventListenerFactory
* p1 = "v1"
* p2 = true
* }
*
* # or multiple event listeners
* cacheManagerEventListenerFactory {
* listener1 {
* class = MyCacheEventListenerFactory1
* p1 = "v1"
* p2 = true
* }
* listener2 {
* class = MyCacheEventListenerFactory2
* }
* }
*
* diskStore.path = ${application.tmpdir}${file.separator}ehcache
*
* # etc...
* }
*
* </pre>
*
* <h3>programmatically</h3>
*
* <pre>
* {
* use(new Eh().doWith(conf {@literal ->} {
* conf.setDefaultTransactionTimeoutInSeconds(120);
* // etc...
* }));
* }
* </pre>
*
* @author edgar
* @since 0.6.0
*/
public class Eh implements Jooby.Module {
/**
* Configure callback.
*/
private BiConsumer<Configuration, Config> configurer;
@Override
public void configure(final Env env, final Config config, final Binder binder) {
Config ehcache = config.getConfig("ehcache");
Config caches = ehcache.getConfig("cache");
Config defcache = caches.getConfig("default");
Configuration ehconfig = new ConfigurationBuilder().build(ehcache);
ehconfig.setDefaultCacheConfiguration(new CacheConfigurationBuilder("default").build(defcache));
Config userCaches = caches.withoutPath("default");
for (Entry<String, ConfigValue> userCache : userCaches.root().entrySet()) {
ConfigValue value = userCache.getValue();
String cname = userCache.getKey();
Config ccache = ConfigFactory.empty();
if (value instanceof ConfigObject) {
ccache = ((ConfigObject) value).toConfig();
}
ehconfig.addCache(new CacheConfigurationBuilder(cname).build(ccache.withFallback(defcache)));
}
if (configurer != null) {
configurer.accept(ehconfig, config);
}
CacheManager cm = CacheManager.newInstance(ehconfig);
binder.bind(CacheManager.class).toInstance(cm);
env.onStop(cm::shutdown);
String[] names = cm.getCacheNames();
if (names.length == 1) {
// just one cache, bind it without name
binder.bind(Ehcache.class).toInstance(cm.getEhcache(names[0]));
}
// now bind each cache using it's name
for (String name : cm.getCacheNames()) {
binder.bind(Ehcache.class).annotatedWith(Names.named(name)).toInstance(cm.getEhcache(name));
}
}
/**
* Configure callback to manipulate a {@link Configuration} programmatically.
*
* @param configurer A configure callback.
* @return This {@link Eh} cache.
*/
public Eh doWith(final BiConsumer<Configuration, Config> configurer) {
this.configurer = requireNonNull(configurer, "Configurer callback is required.");
return this;
}
/**
* Configure callback to manipulate a {@link Configuration} programmatically.
*
* @param configurer A configure callback.
* @return This {@link Eh} cache.
*/
public Eh doWith(final Consumer<Configuration> configurer) {
requireNonNull(configurer, "Configurer callback is required.");
return doWith((ehconf, conf) -> configurer.accept(ehconf));
}
@Override
public Config config() {
return ConfigFactory.parseResources(getClass(), "ehcache.conf");
}
}