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 package fr.paris.lutece.portal.service.cache;
35
36 import fr.paris.lutece.portal.service.util.AppLogService;
37
38 import net.sf.ehcache.Cache;
39 import net.sf.ehcache.CacheException;
40 import net.sf.ehcache.Ehcache;
41 import net.sf.ehcache.Element;
42 import net.sf.ehcache.Statistics;
43 import net.sf.ehcache.event.CacheEventListener;
44
45 import java.util.ArrayList;
46 import java.util.List;
47
48 import org.apache.logging.log4j.LogManager;
49 import org.apache.logging.log4j.Logger;
50
51
52
53
54 public abstract class AbstractCacheableService implements CacheableService, CacheEventListener
55 {
56 private Cache _cache;
57 private boolean _bEnable;
58 private Logger _logger = LogManager.getLogger( "lutece.cache" );
59
60
61
62
63 public void initCache( )
64 {
65 initCache( getName( ) );
66 }
67
68
69
70
71
72
73
74 public void initCache( String strCacheName )
75 {
76 createCache( strCacheName );
77 _bEnable = true;
78 CacheService.registerCacheableService( this );
79 }
80
81
82
83
84
85
86
87 private void createCache( String strCacheName )
88 {
89 _cache = CacheService.getInstance( ).createCache( strCacheName );
90 _cache.getCacheEventNotificationService( ).registerListener( this );
91 }
92
93
94
95
96
97
98
99
100
101 public void putInCache( String strKey, Object object )
102 {
103 if ( ( _cache != null ) && isCacheEnable( ) )
104 {
105 Element element = new Element( strKey, object );
106 _cache.put( element );
107 }
108 }
109
110
111
112
113
114
115
116
117 public Object getFromCache( String strKey )
118 {
119 Object object = null;
120
121 if ( ( _cache != null ) && isCacheEnable( ) )
122 {
123 Element element = _cache.get( strKey );
124
125 if ( element != null )
126 {
127 object = element.getObjectValue( );
128 }
129 }
130
131 return object;
132 }
133
134
135
136
137
138
139 @Override
140 public boolean isCacheEnable( )
141 {
142 return _bEnable;
143 }
144
145
146
147
148 @Override
149 public void enableCache( boolean bEnable )
150 {
151 _bEnable = bEnable;
152
153 if ( ( !_bEnable ) && ( _cache != null ) )
154 {
155 _cache.removeAll( );
156 }
157
158 if ( ( _bEnable ) && ( _cache == null ) )
159 {
160 createCache( getName( ) );
161 }
162
163 CacheService.updateCacheStatus( this );
164 }
165
166
167
168
169 @Override
170 public void resetCache( )
171 {
172 try
173 {
174 if ( _cache != null )
175 {
176 _cache.removeAll( );
177 }
178 }
179 catch( CacheException | IllegalStateException e )
180 {
181 AppLogService.error( e.getMessage( ), e );
182 }
183 }
184
185
186
187
188
189
190 @Override
191 public int getCacheSize( )
192 {
193 return ( _cache != null ) ? _cache.getSize( ) : 0;
194 }
195
196
197
198
199
200
201 public Cache getCache( )
202 {
203 return _cache;
204 }
205
206
207
208
209 @Override
210 public List<String> getKeys( )
211 {
212 if ( _cache != null )
213 {
214 return _cache.getKeys( );
215 }
216
217 return new ArrayList<>( );
218 }
219
220
221
222
223 @Override
224 public int getMaxElements( )
225 {
226 return _cache.getCacheConfiguration( ).getMaxElementsInMemory( );
227 }
228
229
230
231
232 @Override
233 public long getTimeToLive( )
234 {
235 return _cache.getCacheConfiguration( ).getTimeToLiveSeconds( );
236 }
237
238
239
240
241 @Override
242 public long getMemorySize( )
243 {
244 return _cache.calculateInMemorySize( );
245 }
246
247
248
249
250 @Override
251 public String getInfos( )
252 {
253 return CacheService.getInfos( _cache );
254 }
255
256
257
258
259
260
261
262
263
264
265 public String getStatistics( )
266 {
267 if ( !( isCacheEnable( ) && _cache.getCacheConfiguration( ).getStatistics( ) ) )
268 {
269 return null;
270 }
271 Statistics stats = _cache.getStatistics( );
272 StringBuilder buidler = new StringBuilder( );
273 buidler.append( "name = " ).append( stats.getAssociatedCacheName( ) ).append( "\ncacheHits = " )
274 .append( stats.getCacheHits( ) ).append( "\nonDiskHits = " ).append( stats.getOnDiskHits( ) )
275 .append( "\noffHeapHits = " ).append( stats.getOffHeapHits( ) ).append( "\ninMemoryHits = " )
276 .append( stats.getInMemoryHits( ) ).append( "\nmisses = " ).append( stats.getCacheMisses( ) )
277 .append( "\nonDiskMisses = " ).append( stats.getOnDiskMisses( ) ).append( "\noffHeapMisses = " )
278 .append( stats.getOffHeapMisses( ) ).append( "\ninMemoryMisses = " )
279 .append( stats.getInMemoryMisses( ) ).append( "\nsize = " ).append( stats.getObjectCount( ) )
280 .append( "\naverageGetTime = " ).append( stats.getAverageGetTime( ) ).append( "\nevictionCount = " )
281 .append( stats.getEvictionCount( ) );
282 return buidler.toString( );
283 }
284
285
286
287
288
289
290
291 @Override
292 public Object clone( )
293 {
294 throw new UnsupportedOperationException( "This class shouldn't be cloned" );
295 }
296
297
298
299
300 @Override
301 public void notifyElementExpired( Ehcache cache, Element element )
302 {
303
304 _cache.remove( element.getKey( ) );
305 _logger.debug( "Object removed from the cache : {} - key : {}", cache.getName( ), element.getKey( ) );
306 }
307
308
309
310
311 @Override
312 public void notifyElementRemoved( Ehcache ehch, Element elmnt )
313 {
314
315 }
316
317
318
319
320 @Override
321 public void notifyElementEvicted( Ehcache ehch, Element elmnt )
322 {
323
324 }
325
326
327
328
329 @Override
330 public void notifyRemoveAll( Ehcache ehch )
331 {
332
333 }
334
335
336
337
338 @Override
339 public void notifyElementPut( Ehcache ehch, Element elmnt )
340 {
341
342 }
343
344
345
346
347 @Override
348 public void notifyElementUpdated( Ehcache ehch, Element elmnt )
349 {
350
351 }
352
353
354
355
356 @Override
357 public void dispose( )
358 {
359
360 }
361
362
363
364
365
366
367
368 public void removeKey( String strKey )
369 {
370 getCache( ).remove( strKey );
371 }
372 }