Revision 933e5ac3
Added by Cherian Mathew about 9 years ago
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/cache/CdmCacher.java | ||
---|---|---|
7 | 7 |
import net.sf.ehcache.Element; |
8 | 8 |
import net.sf.ehcache.config.CacheConfiguration; |
9 | 9 |
import net.sf.ehcache.store.MemoryStoreEvictionPolicy; |
10 |
import eu.etaxonomy.cdm.model.ICdmCacher; |
|
10 |
import eu.etaxonomy.cdm.model.ICdmUuidCacher;
|
|
11 | 11 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
12 | 12 |
|
13 | 13 |
/** |
... | ... | |
19 | 19 |
* @param <T> |
20 | 20 |
*/ |
21 | 21 |
|
22 |
public abstract class CdmCacher<T extends CdmBase> implements ICdmCacher<T> {
|
|
22 |
public abstract class CdmCacher implements ICdmUuidCacher {
|
|
23 | 23 |
|
24 | 24 |
|
25 | 25 |
|
26 |
private static final String DEFAULT_CACHE_NAME = "defaultCache";
|
|
26 |
public static final String DEFAULT_CACHE_NAME = "defaultCache";
|
|
27 | 27 |
|
28 | 28 |
/** |
29 | 29 |
* Constructor which initialises a singleton {@link net.sf.ehcache.CacheManager} |
... | ... | |
64 | 64 |
* @return |
65 | 65 |
*/ |
66 | 66 |
private CacheConfiguration getDefaultCacheConfiguration() { |
67 |
// This is 2.6.9 API |
|
68 |
// SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration(); |
|
69 |
// sizeOfConfig.setMaxDepth(10000); |
|
70 |
// sizeOfConfig.setMaxDepthExceededBehavior("abort"); |
|
67 | 71 |
// For a better understanding on how to size caches, refer to |
68 | 72 |
// http://ehcache.org/documentation/configuration/cache-size |
69 | 73 |
return new CacheConfiguration(DEFAULT_CACHE_NAME, 500) |
70 | 74 |
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU) |
71 | 75 |
.eternal(false) |
76 |
|
|
72 | 77 |
// default ttl and tti set to 2 hours |
73 | 78 |
.timeToLiveSeconds(60*60*2) |
74 |
.timeToIdleSeconds(60*60*2); |
|
79 |
.timeToIdleSeconds(60*60*2) |
|
80 |
.statistics(true); |
|
75 | 81 |
// This is 2.6.9 API |
76 | 82 |
//.maxEntriesLocalDisk(1000); |
77 | 83 |
//.persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP)); |
... | ... | |
101 | 107 |
* @see eu.etaxonomy.cdm.model.ICdmCacher#put(java.util.UUID, eu.etaxonomy.cdm.model.common.CdmBase) |
102 | 108 |
*/ |
103 | 109 |
@Override |
104 |
public T put(UUID uuid, T cdmEntity) {
|
|
105 |
T cachedCdmEntity = getFromCache(uuid);
|
|
110 |
public CdmBase put(UUID uuid, CdmBase cdmEntity) {
|
|
111 |
CdmBase cachedCdmEntity = getFromCache(uuid);
|
|
106 | 112 |
if(getFromCache(uuid) == null) { |
107 | 113 |
getDefaultCache().put(new Element(uuid, cdmEntity)); |
108 | 114 |
return cdmEntity; |
... | ... | |
111 | 117 |
} |
112 | 118 |
} |
113 | 119 |
|
120 |
|
|
121 |
|
|
114 | 122 |
/* (non-Javadoc) |
115 | 123 |
* @see eu.etaxonomy.cdm.remote.cache.ICdmCacher#load(java.util.UUID) |
116 | 124 |
*/ |
117 | 125 |
@Override |
118 |
public T load(UUID uuid) {
|
|
126 |
public CdmBase load(UUID uuid) {
|
|
119 | 127 |
Element e = getCacheElement(uuid); |
120 |
T cdmEntity;
|
|
128 |
CdmBase cdmEntity;
|
|
121 | 129 |
if (e == null) { |
122 |
|
|
123 | 130 |
// nothing in the cache for "key" (or expired) ... re-load the entity |
124 | 131 |
cdmEntity = findByUuid(uuid); |
125 |
// default cache is a non-null cache |
|
132 |
// currently default cache is a non-null cache |
|
133 |
// We would like to have the possibility to put null values in the cache, |
|
134 |
// but we need to first distinguish between real null values and null values |
|
135 |
// returned by the service is the type of class does not match |
|
126 | 136 |
if(cdmEntity != null) { |
127 | 137 |
put(uuid, cdmEntity); |
128 | 138 |
} |
129 | 139 |
} else { |
130 | 140 |
// there is a valid element in the cache, however getObjectValue() may be null |
131 |
cdmEntity = (T)e.getValue();
|
|
141 |
cdmEntity = (CdmBase)e.getValue();
|
|
132 | 142 |
} |
133 | 143 |
return cdmEntity; |
134 | 144 |
} |
... | ... | |
138 | 148 |
* @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID) |
139 | 149 |
*/ |
140 | 150 |
@Override |
141 |
public T getFromCache(UUID uuid) {
|
|
151 |
public CdmBase getFromCache(UUID uuid) {
|
|
142 | 152 |
Element e = getCacheElement(uuid); |
143 | 153 |
if (e == null) { |
144 | 154 |
return null; |
145 | 155 |
} else { |
146 |
return(T)e.getObjectValue();
|
|
156 |
return(CdmBase)e.getObjectValue();
|
|
147 | 157 |
} |
148 | 158 |
} |
149 | 159 |
|
150 | 160 |
|
161 |
/* (non-Javadoc) |
|
162 |
* @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(eu.etaxonomy.cdm.model.common.CdmBase) |
|
163 |
*/ |
|
164 |
@Override |
|
165 |
public CdmBase getFromCache(CdmBase cdmBase) { |
|
166 |
return getFromCache(cdmBase.getUuid()); |
|
167 |
} |
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
/* (non-Javadoc) |
|
172 |
* @see eu.etaxonomy.cdm.model.ICdmCacher#put(eu.etaxonomy.cdm.model.common.CdmBase) |
|
173 |
*/ |
|
174 |
@Override |
|
175 |
public void put(CdmBase cdmEntity) { |
|
176 |
if(cdmEntity != null) { |
|
177 |
put(cdmEntity.getUuid(), cdmEntity); |
|
178 |
} |
|
179 |
} |
|
180 |
|
|
181 |
/* (non-Javadoc) |
|
182 |
* @see eu.etaxonomy.cdm.model.ICdmCacher#load(eu.etaxonomy.cdm.model.common.CdmBase) |
|
183 |
*/ |
|
184 |
@Override |
|
185 |
public CdmBase load(CdmBase cdmEntity) { |
|
186 |
CdmBase cachedCdmEntity = getFromCache(cdmEntity); |
|
187 |
if(cdmEntity != null && isCachable(cdmEntity)) { |
|
188 |
put(cdmEntity); |
|
189 |
return cdmEntity; |
|
190 |
} |
|
191 |
return null; |
|
192 |
} |
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
151 | 197 |
/* (non-Javadoc) |
152 | 198 |
* @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID) |
153 | 199 |
*/ |
... | ... | |
163 | 209 |
@Override |
164 | 210 |
public boolean existsAndIsNotNull(UUID uuid) { |
165 | 211 |
Element e = getCacheElement(uuid); |
166 |
T cdmEntity;
|
|
212 |
CdmBase cdmEntity;
|
|
167 | 213 |
if (e != null) { |
168 |
return (T)e.getObjectValue() != null;
|
|
214 |
return e.getObjectValue() != null; |
|
169 | 215 |
} |
170 | 216 |
return false; |
171 | 217 |
} |
... | ... | |
176 | 222 |
* @param uuid |
177 | 223 |
* @return |
178 | 224 |
*/ |
179 |
protected abstract T findByUuid(UUID uuid);
|
|
225 |
protected abstract CdmBase findByUuid(UUID uuid);
|
|
180 | 226 |
|
181 | 227 |
|
182 | 228 |
|
Also available in: Unified diff
ICdmCacher, ICdmUuidCacher, CdmCacher, CdmTermCacher : refactored and added load method
Language, NameRelationshipType, Rank : now uses cacher only if termap is null or empty and only when class matches the specific term type
MockCdmCacher: not required anymore
DefaultTermInitializer : removed acall to mock cacher
MatchStrategyConfigurator : moved from editor to cdmlib
CdmDataChangeEvent : added constructor
CommonServiceImpl, ICommonService : added call to findMatchingStrategy with enum instead of object
DescriptionServiceImpl, IDescriptionService, PolytomousKeyNodeServiceImpl, IPolytomousKeyNodeService, TaxonNodeServiceImpl, ITaxonNodeService, TaxonServiceImpl: added uuid methods