Project

General

Profile

Download (3.21 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.CdmCacher;
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
 *
26
 * @author a.kohlbecker
27
 * @since Feb 1, 2017
28
 *
29
 */
30
@Configuration
31
// @EnableCaching // for future use
32
public class EhCacheConfiguration implements DisposableBean {
33

    
34
    public static final Logger logger = Logger.getLogger(EhCacheConfiguration.class);
35

    
36
    @Autowired(required = false)
37
    public DiskStoreConfiguration diskStoreConfiguration = null;
38

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

    
47
    private CacheManager cacheManager = null;
48

    
49
    @Bean
50
    public CacheManager cacheManager(){
51

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

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

    
65

    
66
    /**
67
     * Returns the default cache configuration for the cache
68
     * named {@link CdmCacher#DEFAULT_CACHE_NAME "cdmDefaultCache"}
69
     *
70
     * @return
71
     */
72
    protected CacheConfiguration getDefaultCacheConfiguration() {
73

    
74
        CacheEventListenerFactoryConfiguration factory;
75

    
76
        // For a better understanding on how to size caches, refer to
77
        // http://ehcache.org/documentation/configuration/cache-size
78

    
79
        CacheConfiguration cc = new CacheConfiguration(CdmCacher.DEFAULT_CACHE_NAME, 500)
80
                .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
81
                .maxEntriesLocalHeap(10) // avoid ehache consuming too much heap
82
                .eternal(false)
83
                // default ttl and tti set to 2 hours
84
                .timeToLiveSeconds(60*60*2)
85
                .timeToIdleSeconds(60*60*2)
86
                .statistics(true);
87

    
88
        return cc;
89
    }
90

    
91

    
92
    @Override
93
    public void destroy() {
94
        if (cacheManager != null) {
95
            logger.info("Shutting down EhCache CacheManager");
96
            this.cacheManager.shutdown();
97
        }
98
    }
99

    
100
}
(5-5/6)