Project

General

Profile

Download (2.94 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.api.cache;
2

    
3
import java.util.UUID;
4

    
5
import net.sf.ehcache.config.CacheConfiguration;
6
import net.sf.ehcache.config.SizeOfPolicyConfiguration;
7

    
8
import org.springframework.beans.factory.annotation.Autowired;
9
import org.springframework.stereotype.Component;
10

    
11
import eu.etaxonomy.cdm.api.service.ITermService;
12
import eu.etaxonomy.cdm.model.common.CdmBase;
13
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
14
import eu.etaxonomy.taxeditor.remoting.cache.CacheLoader;
15
import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
16
import eu.etaxonomy.taxeditor.service.TermServiceRequestExecutor;
17

    
18
/**
19
 * Class which uses CDM services to cache cdm entities
20
 *
21
 * FIXME: Currently only handles terms entities. It would be
22
 *        interesting to have a generic method which finds the
23
 *        correct service to load / cache the entity.
24
 *
25
 * @author cmathew
26
 *
27
 * @param <T>
28
 */
29
@Component
30
public class CdmServiceCacher extends CdmCacher{
31

    
32
    @Autowired
33
    ITermService termService;
34

    
35
    private CacheLoader cacheLoader;
36

    
37
    @Override
38
    protected void setup() {
39
        DefinedTermBase.setCacher(this);
40
        CdmTransientEntityCacher.setDefaultCacher(this);
41
        TermServiceRequestExecutor.setDefaultCacher(this);
42
        cacheLoader = new CacheLoader(this);
43
    }
44

    
45
    @Override
46
    protected CacheConfiguration getDefaultCacheConfiguration() {
47
        // For a better understanding on how to size caches, refer to
48
        // http://ehcache.org/documentation/configuration/cache-size
49

    
50
        SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
51
        sizeOfConfig.setMaxDepth(100);
52
        sizeOfConfig.setMaxDepthExceededBehavior("abort");
53

    
54
        return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
55
        .eternal(true)
56
        .statistics(true)
57
        .sizeOfPolicy(sizeOfConfig)
58
        .overflowToOffHeap(false);
59

    
60
    }
61

    
62
    @Override
63
    protected CdmBase findByUuid(UUID uuid) {
64
        CdmBase term = termService.findWithoutFlush(uuid);
65
        return load(term);
66
    }
67

    
68
    /* (non-Javadoc)
69
     * @see eu.etaxonomy.cdm.model.ICdmCacher#isCachable(eu.etaxonomy.cdm.model.common.CdmBase)
70
     */
71
    @Override
72
    public boolean isCachable(CdmBase cdmEntity) {
73
        if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
74
            return true;
75
        }
76
        return false;
77
    }
78

    
79

    
80
    /* (non-Javadoc)
81
     * @see eu.etaxonomy.cdm.api.cache.CdmCacher#load(eu.etaxonomy.cdm.model.common.CdmBase)
82
     */
83
    @Override
84
    public CdmBase load(CdmBase cdmEntity) {
85
        CdmBase cachedCdmEntity = getFromCache(cdmEntity);
86
        // NOTE : only when no cached cdm entity exists we
87
        // load the entire sub-graph, which means that the
88
        // sub-graph is never updated if the input
89
        // cdm entity graph is newer
90
        if(cachedCdmEntity == null && isCachable(cdmEntity)) {
91
            cachedCdmEntity =  cacheLoader.load(cdmEntity, true, false);
92
        }
93
        return cachedCdmEntity;
94
    }
95

    
96

    
97
}
    (1-1/1)