2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.api
.config
;
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
;
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
;
26 * @author a.kohlbecker
30 // @EnableCaching // for future use
31 public class EhCacheConfiguration
implements DisposableBean
{
33 public static final Logger logger
= LogManager
.getLogger();
35 @Autowired(required
= false)
36 public DiskStoreConfiguration diskStoreConfiguration
= null;
39 * The DiskStoreConfiguration can either be autowired or set explicite
40 * @param diskStoreConfiguration the diskStoreConfiguration to set
42 public void setDiskStoreConfiguration(DiskStoreConfiguration diskStoreConfiguration
) {
43 this.diskStoreConfiguration
= diskStoreConfiguration
;
46 private CacheManager cacheManager
= null;
49 public CacheManager
cacheManager(){
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
);
56 conf
.addDefaultCache(getDefaultCacheConfiguration());
58 // creates a singleton
59 cacheManager
= CacheManager
.create(conf
);
60 logger
.debug("CacheManager created");
65 * Returns the default cache configuration for the cache
66 * named {@link CdmCacherBase#DEFAULT_CACHE_NAME "cdmDefaultCache"}
68 protected CacheConfiguration
getDefaultCacheConfiguration() {
70 CacheEventListenerFactoryConfiguration factory
;
72 // For a better understanding on how to size caches, refer to
73 // http://ehcache.org/documentation/configuration/cache-size
75 CacheConfiguration cc
= new CacheConfiguration(CdmCacherBase
.DEFAULT_CACHE_NAME
, 500)
76 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy
.LFU
)
77 .maxEntriesLocalHeap(10) // avoid ehache consuming too much heap
79 // default ttl and tti set to 2 hours
80 .timeToLiveSeconds(60*60*2)
81 .timeToIdleSeconds(60*60*2);
87 public void destroy() {
88 if (cacheManager
!= null) {
89 logger
.info("Shutting down EhCache CacheManager");
90 this.cacheManager
.shutdown();