Project

General

Profile

Download (3.05 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.
68
     *
69
     * @return
70
     */
71
    protected CacheConfiguration getDefaultCacheConfiguration() {
72

    
73
        CacheEventListenerFactoryConfiguration factory;
74

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

    
78
        CacheConfiguration cc = new CacheConfiguration(CdmCacher.DEFAULT_CACHE_NAME, 500)
79
                .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
80
                .eternal(false)
81
                // default ttl and tti set to 2 hours
82
                .timeToLiveSeconds(60*60*2)
83
                .timeToIdleSeconds(60*60*2)
84
                .statistics(true);
85

    
86
        return cc;
87
    }
88

    
89

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

    
98
}
(5-5/6)