cleanup
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / config / EhCacheConfiguration.java
1 /**
2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.api.config;
10
11 import org.apache.logging.log4j.LogManager;
12 import org.apache.logging.log4j.Logger;
13 import org.springframework.beans.factory.DisposableBean;
14 import org.springframework.beans.factory.annotation.Autowired;
15 import org.springframework.context.annotation.Bean;
16 import org.springframework.context.annotation.Configuration;
17
18 import eu.etaxonomy.cdm.api.cache.CdmCacherBase;
19 import net.sf.ehcache.CacheManager;
20 import net.sf.ehcache.config.CacheConfiguration;
21 import net.sf.ehcache.config.CacheConfiguration.CacheEventListenerFactoryConfiguration;
22 import net.sf.ehcache.config.DiskStoreConfiguration;
23 import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
24
25 /**
26 * @author a.kohlbecker
27 * @since Feb 1, 2017
28 */
29 @Configuration
30 // @EnableCaching // for future use
31 public class EhCacheConfiguration implements DisposableBean {
32
33 public static final Logger logger = LogManager.getLogger();
34
35 @Autowired(required = false)
36 public DiskStoreConfiguration diskStoreConfiguration = null;
37
38 /**
39 * The DiskStoreConfiguration can either be autowired or set explicite
40 * @param diskStoreConfiguration the diskStoreConfiguration to set
41 */
42 public void setDiskStoreConfiguration(DiskStoreConfiguration diskStoreConfiguration) {
43 this.diskStoreConfiguration = diskStoreConfiguration;
44 }
45
46 private CacheManager cacheManager = null;
47
48 @Bean
49 public CacheManager cacheManager(){
50
51 net.sf.ehcache.config.Configuration conf = new net.sf.ehcache.config.Configuration();
52 if(diskStoreConfiguration != null){
53 logger.debug("creating CacheManager with disk store");
54 conf.addDiskStore(diskStoreConfiguration);
55 }
56 conf.addDefaultCache(getDefaultCacheConfiguration());
57
58 // creates a singleton
59 cacheManager = CacheManager.create(conf);
60 logger.debug("CacheManager created");
61 return cacheManager;
62 }
63
64 /**
65 * Returns the default cache configuration for the cache
66 * named {@link CdmCacherBase#DEFAULT_CACHE_NAME "cdmDefaultCache"}
67 */
68 protected CacheConfiguration getDefaultCacheConfiguration() {
69
70 CacheEventListenerFactoryConfiguration factory;
71
72 // For a better understanding on how to size caches, refer to
73 // http://ehcache.org/documentation/configuration/cache-size
74
75 CacheConfiguration cc = new CacheConfiguration(CdmCacherBase.DEFAULT_CACHE_NAME, 500)
76 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
77 .maxEntriesLocalHeap(10) // avoid ehache consuming too much heap
78 .eternal(false)
79 // default ttl and tti set to 2 hours
80 .timeToLiveSeconds(60*60*2)
81 .timeToIdleSeconds(60*60*2);
82
83 return cc;
84 }
85
86 @Override
87 public void destroy() {
88 if (cacheManager != null) {
89 logger.info("Shutting down EhCache CacheManager");
90 this.cacheManager.shutdown();
91 }
92 }
93 }