1 |
9f24d9a0
|
Andreas Müller
|
package eu.etaxonomy.cdm.model.common.init;
|
2 |
078fa200
|
Cherian Mathew
|
|
3 |
c20a343c
|
Andreas Müller
|
import java.util.Set;
|
4 |
078fa200
|
Cherian Mathew
|
import java.util.UUID;
|
5 |
|
|
|
6 |
|
|
import net.sf.ehcache.Cache;
|
7 |
|
|
import net.sf.ehcache.CacheManager;
|
8 |
|
|
import net.sf.ehcache.Element;
|
9 |
|
|
import net.sf.ehcache.config.CacheConfiguration;
|
10 |
|
|
import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
|
11 |
|
|
|
12 |
9f24d9a0
|
Andreas Müller
|
import eu.etaxonomy.cdm.model.ICdmCacher;
|
13 |
078fa200
|
Cherian Mathew
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
14 |
|
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
15 |
|
|
import eu.etaxonomy.cdm.model.common.Language;
|
16 |
|
|
|
17 |
|
|
/**
|
18 |
|
|
* Since cdmlib-model cannot access CdmCacher we need to create a mock class
|
19 |
9aa01c88
|
Cherian Mathew
|
* for the tests.
|
20 |
03aae089
|
Cherian Mathew
|
*
|
21 |
9aa01c88
|
Cherian Mathew
|
* NOTES:
|
22 |
|
|
* - All terms are put into the cache in the constructor
|
23 |
|
|
* - The number of elements allowed in the cache is set to a big number - 10000
|
24 |
03aae089
|
Cherian Mathew
|
*
|
25 |
9aa01c88
|
Cherian Mathew
|
* FIXME : Once the CDMCacher is externalised this class should just subclass it.
|
26 |
03aae089
|
Cherian Mathew
|
*
|
27 |
078fa200
|
Cherian Mathew
|
* @author cmathew
|
28 |
|
|
*
|
29 |
|
|
*/
|
30 |
|
|
public class MockCdmCacher implements ICdmCacher {
|
31 |
|
|
|
32 |
|
|
private static final String DEFAULT_CACHE_NAME = "defaultCache";
|
33 |
03aae089
|
Cherian Mathew
|
|
34 |
078fa200
|
Cherian Mathew
|
private static final String DEFAULT_CACHE_MGR_NAME = "defaultCacheManager";
|
35 |
|
|
/**
|
36 |
|
|
* Constructor which initialises a singleton {@link net.sf.ehcache.CacheManager}
|
37 |
03aae089
|
Cherian Mathew
|
*
|
38 |
078fa200
|
Cherian Mathew
|
*/
|
39 |
|
|
public MockCdmCacher() {
|
40 |
|
|
init();
|
41 |
|
|
}
|
42 |
03aae089
|
Cherian Mathew
|
|
43 |
078fa200
|
Cherian Mathew
|
/**
|
44 |
|
|
* Initialises an empty singleton {@link net.sf.ehcache.CacheManager} and
|
45 |
03aae089
|
Cherian Mathew
|
* sets itself as the cacher object in specific CDM Entity objects.
|
46 |
|
|
*
|
47 |
078fa200
|
Cherian Mathew
|
*/
|
48 |
03aae089
|
Cherian Mathew
|
private void init() {
|
49 |
078fa200
|
Cherian Mathew
|
// Remove all caches
|
50 |
|
|
getDefaultCacheManager().removalAll();
|
51 |
|
|
// Create default cache
|
52 |
|
|
getDefaultCacheManager().addCache(new Cache(getDefaultCacheConfiguration()));
|
53 |
c20a343c
|
Andreas Müller
|
Set<Language> langList = Language.DEFAULT().getVocabulary().getTerms();
|
54 |
|
|
for (Language lang : langList){
|
55 |
|
|
put(lang.getUuid(),lang);
|
56 |
|
|
}
|
57 |
078fa200
|
Cherian Mathew
|
// We start first only with DefinedTermBase
|
58 |
|
|
DefinedTermBase.setCacher(this);
|
59 |
03aae089
|
Cherian Mathew
|
|
60 |
078fa200
|
Cherian Mathew
|
}
|
61 |
03aae089
|
Cherian Mathew
|
|
62 |
078fa200
|
Cherian Mathew
|
/**
|
63 |
|
|
* Returns the singleton default cache manager.
|
64 |
03aae089
|
Cherian Mathew
|
*
|
65 |
078fa200
|
Cherian Mathew
|
* @return
|
66 |
|
|
*/
|
67 |
|
|
private static CacheManager getDefaultCacheManager() {
|
68 |
|
|
// this ensures a singleton cache manager
|
69 |
|
|
return CacheManager.create();
|
70 |
|
|
}
|
71 |
03aae089
|
Cherian Mathew
|
|
72 |
078fa200
|
Cherian Mathew
|
/**
|
73 |
|
|
* Returns the default cache configuration.
|
74 |
03aae089
|
Cherian Mathew
|
*
|
75 |
078fa200
|
Cherian Mathew
|
* @return
|
76 |
|
|
*/
|
77 |
|
|
private CacheConfiguration getDefaultCacheConfiguration() {
|
78 |
9aa01c88
|
Cherian Mathew
|
return new CacheConfiguration(DEFAULT_CACHE_NAME, 10000)
|
79 |
078fa200
|
Cherian Mathew
|
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
|
80 |
|
|
.eternal(false)
|
81 |
|
|
// default ttl and tti set to 2 hours
|
82 |
|
|
.timeToLiveSeconds(60*60*2)
|
83 |
4d160610
|
Cherian Mathew
|
.timeToIdleSeconds(60*60*2);
|
84 |
03aae089
|
Cherian Mathew
|
|
85 |
078fa200
|
Cherian Mathew
|
}
|
86 |
03aae089
|
Cherian Mathew
|
|
87 |
078fa200
|
Cherian Mathew
|
/**
|
88 |
|
|
* Returns the default cache
|
89 |
03aae089
|
Cherian Mathew
|
*
|
90 |
078fa200
|
Cherian Mathew
|
* @return
|
91 |
|
|
*/
|
92 |
|
|
private static Cache getDefaultCache() {
|
93 |
|
|
return getDefaultCacheManager().getCache(DEFAULT_CACHE_NAME);
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
/**
|
97 |
|
|
* Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
|
98 |
|
|
* in the cache
|
99 |
03aae089
|
Cherian Mathew
|
*
|
100 |
078fa200
|
Cherian Mathew
|
* @param uuid
|
101 |
|
|
* @param cdmEntity
|
102 |
03aae089
|
Cherian Mathew
|
* @return
|
103 |
078fa200
|
Cherian Mathew
|
*/
|
104 |
03aae089
|
Cherian Mathew
|
@Override
|
105 |
|
|
public CdmBase put(UUID uuid, CdmBase cdmEntity) {
|
106 |
|
|
CdmBase cachedCdmEntity = getFromCache(uuid);
|
107 |
|
|
if(getFromCache(uuid) == null) {
|
108 |
|
|
getDefaultCache().put(new Element(uuid, cdmEntity));
|
109 |
|
|
return cdmEntity;
|
110 |
|
|
} else {
|
111 |
|
|
return cachedCdmEntity;
|
112 |
|
|
}
|
113 |
078fa200
|
Cherian Mathew
|
}
|
114 |
03aae089
|
Cherian Mathew
|
|
115 |
078fa200
|
Cherian Mathew
|
/**
|
116 |
|
|
* Gets the cache element corresponding to the given {@link java.util.UUID}
|
117 |
03aae089
|
Cherian Mathew
|
*
|
118 |
078fa200
|
Cherian Mathew
|
* @param uuid
|
119 |
|
|
* @return
|
120 |
|
|
*/
|
121 |
|
|
private Element getCacheElement(UUID uuid) {
|
122 |
|
|
return getDefaultCache().get(uuid);
|
123 |
|
|
}
|
124 |
03aae089
|
Cherian Mathew
|
|
125 |
078fa200
|
Cherian Mathew
|
/**
|
126 |
|
|
* Get CDM Entity for given {@link java.util.UUID} from the cache
|
127 |
03aae089
|
Cherian Mathew
|
*
|
128 |
078fa200
|
Cherian Mathew
|
* @param uuid
|
129 |
|
|
* @return
|
130 |
|
|
*/
|
131 |
|
|
private CdmBase getCdmEntity(UUID uuid) {
|
132 |
03aae089
|
Cherian Mathew
|
return (CdmBase)getDefaultCache().get(uuid).getObjectValue();
|
133 |
078fa200
|
Cherian Mathew
|
}
|
134 |
03aae089
|
Cherian Mathew
|
|
135 |
078fa200
|
Cherian Mathew
|
/* (non-Javadoc)
|
136 |
|
|
* @see eu.etaxonomy.cdm.remote.cache.ICdmCacher#load(java.util.UUID)
|
137 |
|
|
*/
|
138 |
03aae089
|
Cherian Mathew
|
|
139 |
|
|
@Override
|
140 |
|
|
public CdmBase load(UUID uuid) {
|
141 |
|
|
Element e = getCacheElement(uuid);
|
142 |
078fa200
|
Cherian Mathew
|
CdmBase cdmEntity;
|
143 |
|
|
if (e == null) {
|
144 |
|
|
return null;
|
145 |
|
|
} else {
|
146 |
|
|
return (CdmBase)e.getObjectValue();
|
147 |
03aae089
|
Cherian Mathew
|
}
|
148 |
078fa200
|
Cherian Mathew
|
}
|
149 |
03aae089
|
Cherian Mathew
|
|
150 |
078fa200
|
Cherian Mathew
|
|
151 |
|
|
/* (non-Javadoc)
|
152 |
|
|
* @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID)
|
153 |
|
|
*/
|
154 |
03aae089
|
Cherian Mathew
|
@Override
|
155 |
|
|
public CdmBase getFromCache(UUID uuid) {
|
156 |
|
|
Element e = getCacheElement(uuid);
|
157 |
078fa200
|
Cherian Mathew
|
CdmBase cdmEntity;
|
158 |
|
|
if (e == null) {
|
159 |
|
|
return null;
|
160 |
|
|
} else {
|
161 |
|
|
return(CdmBase)e.getObjectValue();
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
03aae089
|
Cherian Mathew
|
|
165 |
078fa200
|
Cherian Mathew
|
|
166 |
|
|
/* (non-Javadoc)
|
167 |
|
|
* @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
|
168 |
|
|
*/
|
169 |
03aae089
|
Cherian Mathew
|
@Override
|
170 |
|
|
public boolean exists(UUID uuid) {
|
171 |
078fa200
|
Cherian Mathew
|
return getCacheElement(uuid) != null;
|
172 |
|
|
}
|
173 |
03aae089
|
Cherian Mathew
|
|
174 |
078fa200
|
Cherian Mathew
|
|
175 |
|
|
/* (non-Javadoc)
|
176 |
|
|
* @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
|
177 |
|
|
*/
|
178 |
03aae089
|
Cherian Mathew
|
@Override
|
179 |
|
|
public boolean existsAndIsNotNull(UUID uuid) {
|
180 |
|
|
Element e = getCacheElement(uuid);
|
181 |
078fa200
|
Cherian Mathew
|
CdmBase cdmEntity;
|
182 |
|
|
if (e != null) {
|
183 |
03aae089
|
Cherian Mathew
|
return (CdmBase)e.getObjectValue() != null;
|
184 |
|
|
}
|
185 |
078fa200
|
Cherian Mathew
|
return false;
|
186 |
|
|
}
|
187 |
03aae089
|
Cherian Mathew
|
|
188 |
078fa200
|
Cherian Mathew
|
}
|