Project

General

Profile

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

    
3
import java.util.UUID;
4

    
5
import eu.etaxonomy.cdm.model.ICdmUuidCacher;
6
import eu.etaxonomy.cdm.model.common.CdmBase;
7
import net.sf.ehcache.Cache;
8
import net.sf.ehcache.CacheManager;
9
import net.sf.ehcache.Element;
10
import net.sf.ehcache.config.CacheConfiguration;
11
import net.sf.ehcache.config.CacheConfiguration.CacheEventListenerFactoryConfiguration;
12
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
13

    
14
/**
15
 * CDM Entity Cacher class based on EhCache.
16
 * The cacher allows null values to be cached.
17
 *
18
 * @author cmathew
19
 *
20
 * @param <T>
21
 */
22

    
23
public abstract class CdmCacher implements ICdmUuidCacher {
24

    
25

    
26

    
27
    public static final String DEFAULT_CACHE_NAME = "defaultCache";
28

    
29
    /**
30
     * Constructor which initialises a singleton {@link net.sf.ehcache.CacheManager}
31
     *
32
     */
33
    public CdmCacher() {
34
        init();
35
    }
36

    
37
    /**
38
     * Initialises an empty singleton {@link net.sf.ehcache.CacheManager} and
39
     * sets itself as the cacher object in specific CDM Entity objects.
40
     *
41
     */
42
    private void init() {
43
        setup();
44
    }
45

    
46
    protected abstract void setup();
47

    
48
    /**
49
     * Returns the singleton default cache manager.
50
     *
51
     * @return
52
     */
53
    public static CacheManager getDefaultCacheManager() {
54
        // this ensures a singleton cache manager
55
        return CacheManager.create();
56
    }
57

    
58
    /**
59
     * Returns the default cache configuration.
60
     *
61
     * @return
62
     */
63
    protected CacheConfiguration getDefaultCacheConfiguration() {
64
        CacheEventListenerFactoryConfiguration factory;
65
        // For a better understanding on how to size caches, refer to
66
        // http://ehcache.org/documentation/configuration/cache-size
67
        return new CacheConfiguration(DEFAULT_CACHE_NAME, 500)
68
                .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
69
                .eternal(false)
70
                // default ttl and tti set to 2 hours
71
                .timeToLiveSeconds(60*60*2)
72
                .timeToIdleSeconds(60*60*2)
73
                .statistics(true);
74

    
75
    }
76

    
77

    
78

    
79
    /**
80
     * Returns the default cache
81
     *
82
     * @return
83
     */
84
    public Cache getDefaultCache() {
85
        Cache defaultCache = getDefaultCacheManager().getCache(DEFAULT_CACHE_NAME);
86
        if(defaultCache == null) {
87
            defaultCache = new Cache(getDefaultCacheConfiguration());
88
            // Create default cache
89
            getDefaultCacheManager().addCache(defaultCache);
90
        }
91
        return defaultCache;
92
    }
93

    
94

    
95
    /**
96
     * Gets the cache element corresponding to the given {@link java.util.UUID}
97
     *
98
     * @param uuid
99
     * @return
100
     */
101
    public Element getCacheElement(UUID uuid) {
102
        return getDefaultCache().get(uuid);
103
    }
104

    
105
    @Override
106
    public  void put(UUID uuid, CdmBase cdmEntity) {
107
        CdmBase cachedCdmEntity = getFromCache(uuid);
108
        if(cachedCdmEntity == null) {
109
            getDefaultCache().put(new Element(uuid, cdmEntity));
110
        }
111
    }
112

    
113
    @Override
114
    public CdmBase load(UUID uuid) {
115
        Element e = getCacheElement(uuid);
116

    
117
        CdmBase cdmEntity;
118
        if (e == null) {
119
            // nothing in the cache for "key" (or expired) ... re-load the entity
120
            cdmEntity = findByUuid(uuid);
121
            // currently default cache is a non-null cache
122
            // We would like to have the possibility to put null values in the cache,
123
            // but we need to first distinguish between real null values and null values
124
            // returned by the service is the type of class does not match
125
            if(cdmEntity != null) {
126
                put(uuid, cdmEntity);
127
            }
128
        } else {
129
            // there is a valid element in the cache, however getObjectValue() may be null
130
            cdmEntity = (CdmBase)e.getObjectValue();
131
        }
132
        return cdmEntity;
133
    }
134

    
135

    
136
    @Override
137
    public  CdmBase getFromCache(UUID uuid) {
138
        Element e = getCacheElement(uuid);
139
        if (e == null) {
140
            return null;
141
        } else {
142
            return(CdmBase)e.getObjectValue();
143
        }
144
    }
145

    
146
    @Override
147
    public CdmBase getFromCache(CdmBase cdmBase) {
148
        return getFromCache(cdmBase.getUuid());
149
    }
150

    
151

    
152
    @Override
153
    public void put(CdmBase cdmEntity) {
154
        if(cdmEntity != null) {
155
            put(cdmEntity.getUuid(), cdmEntity);
156
        }
157
    }
158

    
159
    @Override
160
    public abstract CdmBase load(CdmBase cdmEntity);
161

    
162

    
163
    @Override
164
    public boolean exists(UUID uuid) {
165
        return getCacheElement(uuid) != null;
166
    }
167

    
168
    @Override
169
    public boolean exists(CdmBase cdmBase) {
170
        if(cdmBase != null) {
171
            return exists(cdmBase.getUuid());
172
        }
173
        return false;
174

    
175
    }
176

    
177
    @Override
178
    public boolean existsAndIsNotNull(UUID uuid) {
179
        Element e = getCacheElement(uuid);
180
        CdmBase cdmEntity;
181
        if (e != null) {
182
            return e.getObjectValue() != null;
183
        }
184
        return false;
185
    }
186

    
187
    /**
188
     * Finds CDM Entity by uuid
189
     *
190
     * @param uuid
191
     * @return
192
     */
193
    protected abstract CdmBase findByUuid(UUID uuid);
194

    
195
}
(1-1/2)