Project

General

Profile

Download (3.16 KB) Statistics
| Branch: | Tag: | Revision:
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.log4j.Logger;
12
import org.springframework.beans.factory.DisposableBean;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.context.annotation.Bean;
15
import org.springframework.context.annotation.Configuration;
16

    
17
import eu.etaxonomy.cdm.api.cache.CdmCacherBase;
18
import net.sf.ehcache.CacheManager;
19
import net.sf.ehcache.config.CacheConfiguration;
20
import net.sf.ehcache.config.CacheConfiguration.CacheEventListenerFactoryConfiguration;
21
import net.sf.ehcache.config.DiskStoreConfiguration;
22
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
23

    
24
/**
25
 * @author a.kohlbecker
26
 * @since Feb 1, 2017
27
 */
28
@Configuration
29
// @EnableCaching // for future use
30
public class EhCacheConfiguration implements DisposableBean {
31

    
32
    public static final Logger logger = Logger.getLogger(EhCacheConfiguration.class);
33

    
34
    @Autowired(required = false)
35
    public DiskStoreConfiguration diskStoreConfiguration = null;
36

    
37
    /**
38
     * The DiskStoreConfiguration can either be autowired or set explicite
39
     * @param diskStoreConfiguration the diskStoreConfiguration to set
40
     */
41
    public void setDiskStoreConfiguration(DiskStoreConfiguration diskStoreConfiguration) {
42
        this.diskStoreConfiguration = diskStoreConfiguration;
43
    }
44

    
45
    private CacheManager cacheManager = null;
46

    
47
    @Bean
48
    public CacheManager cacheManager(){
49

    
50
        net.sf.ehcache.config.Configuration conf = new net.sf.ehcache.config.Configuration();
51
        if(diskStoreConfiguration != null){
52
            logger.debug("creating CacheManager with disk store");
53
            conf.addDiskStore(diskStoreConfiguration);
54
        }
55
        conf.addDefaultCache(getDefaultCacheConfiguration());
56

    
57
        // creates a singleton
58
        cacheManager = CacheManager.create(conf);
59
        logger.debug("CacheManager created");
60
        return cacheManager;
61
    }
62

    
63
    /**
64
     * Returns the default cache configuration for the cache
65
     * named {@link CdmCacherBase#DEFAULT_CACHE_NAME "cdmDefaultCache"}
66
     */
67
    protected CacheConfiguration getDefaultCacheConfiguration() {
68

    
69
        CacheEventListenerFactoryConfiguration factory;
70

    
71
        // For a better understanding on how to size caches, refer to
72
        // http://ehcache.org/documentation/configuration/cache-size
73

    
74
        CacheConfiguration cc = new CacheConfiguration(CdmCacherBase.DEFAULT_CACHE_NAME, 500)
75
                .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
76
                .maxEntriesLocalHeap(10) // avoid ehache consuming too much heap
77
                .eternal(false)
78
                // default ttl and tti set to 2 hours
79
                .timeToLiveSeconds(60*60*2)
80
                .timeToIdleSeconds(60*60*2);
81

    
82
        return cc;
83
    }
84

    
85
    @Override
86
    public void destroy() {
87
        if (cacheManager != null) {
88
            logger.info("Shutting down EhCache CacheManager");
89
            this.cacheManager.shutdown();
90
        }
91
    }
92
}
(7-7/9)