Revision 4b2b6fc7
Added by Andreas Müller almost 4 years ago
cdmlib-cache/src/main/java/eu/etaxonomy/cdm/cache/CdmTransientEntityCacher.java | ||
---|---|---|
20 | 20 |
|
21 | 21 |
import org.apache.log4j.Logger; |
22 | 22 |
|
23 |
import eu.etaxonomy.cdm.api.cache.CdmCacher; |
|
23 |
import eu.etaxonomy.cdm.api.cache.CdmCacherBase;
|
|
24 | 24 |
import eu.etaxonomy.cdm.model.ICdmCacher; |
25 | 25 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
26 | 26 |
import eu.etaxonomy.cdm.persistence.dto.MergeResult; |
... | ... | |
60 | 60 |
private final Cache cache; |
61 | 61 |
|
62 | 62 |
//permanent cache which is usually used to cache terms permanently |
63 |
private static CdmCacher permanentCache; |
|
63 |
private static CdmCacherBase permanentCache;
|
|
64 | 64 |
|
65 | 65 |
private final CacheLoader cacheLoader; |
66 | 66 |
|
... | ... | |
106 | 106 |
return new CdmEntityCacheKey<T>(entityClass, cdmBase.getId()); |
107 | 107 |
} |
108 | 108 |
|
109 |
public static void setPermanentCacher(CdmCacher permanentCacher) { |
|
109 |
public static void setPermanentCacher(CdmCacherBase permanentCacher) {
|
|
110 | 110 |
permanentCache = permanentCacher; |
111 | 111 |
} |
112 | 112 |
|
cdmlib-cache/src/main/java/eu/etaxonomy/cdm/cache/EntityCacherDebugResult.java | ||
---|---|---|
23 | 23 |
import org.hibernate.proxy.LazyInitializer; |
24 | 24 |
import org.springframework.util.ReflectionUtils; |
25 | 25 |
|
26 |
import eu.etaxonomy.cdm.api.cache.CdmCacher; |
|
26 |
import eu.etaxonomy.cdm.api.cache.CdmCacherBase;
|
|
27 | 27 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
28 | 28 |
import net.sf.ehcache.Cache; |
29 | 29 |
import net.sf.ehcache.CacheManager; |
... | ... | |
212 | 212 |
} |
213 | 213 |
|
214 | 214 |
private String getCachesContainingEntity(CdmBase cdmEntity) { |
215 |
Cache defaultCache = CacheManager.create().getCache(CdmCacher.DEFAULT_CACHE_NAME); |
|
215 |
Cache defaultCache = CacheManager.create().getCache(CdmCacherBase.DEFAULT_CACHE_NAME);
|
|
216 | 216 |
String caches = ""; |
217 | 217 |
Element dce = defaultCache.get(cdmEntity.getUuid()); |
218 | 218 |
if(dce != null && dce.getObjectValue() == cdmEntity) { |
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/cache/CdmCacher.java | ||
---|---|---|
1 |
package eu.etaxonomy.cdm.api.cache; |
|
2 |
|
|
3 |
import java.util.UUID; |
|
4 |
|
|
5 |
import org.apache.log4j.Logger; |
|
6 |
import org.springframework.beans.factory.annotation.Autowired; |
|
7 |
|
|
8 |
import eu.etaxonomy.cdm.model.ICdmUuidCacher; |
|
9 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
|
10 |
import net.sf.ehcache.Cache; |
|
11 |
import net.sf.ehcache.CacheManager; |
|
12 |
import net.sf.ehcache.Element; |
|
13 |
import net.sf.ehcache.config.CacheConfiguration; |
|
14 |
import net.sf.ehcache.config.CacheConfiguration.CacheEventListenerFactoryConfiguration; |
|
15 |
import net.sf.ehcache.store.MemoryStoreEvictionPolicy; |
|
16 |
|
|
17 |
/** |
|
18 |
* CDM Entity Cacher class based on EhCache using UUID as key. |
|
19 |
* The cacher allows null values to be cached. |
|
20 |
* |
|
21 |
* @author cmathew |
|
22 |
*/ |
|
23 |
public abstract class CdmCacher implements ICdmUuidCacher { |
|
24 |
|
|
25 |
public static final Logger logger = Logger.getLogger(CdmCacher.class); |
|
26 |
|
|
27 |
@Autowired |
|
28 |
public CacheManager cacheManager; |
|
29 |
|
|
30 |
public static final String DEFAULT_CACHE_NAME = "cdmDefaultCache"; //TODO compare with CacheConfiguration where the name for the default cache is 'default', Why another name here? |
|
31 |
|
|
32 |
/** |
|
33 |
* Constructor which initializes a singleton {@link net.sf.ehcache.CacheManager} |
|
34 |
*/ |
|
35 |
public CdmCacher() { |
|
36 |
init(); |
|
37 |
} |
|
38 |
|
|
39 |
/** |
|
40 |
* Initializes an empty singleton {@link net.sf.ehcache.CacheManager} and |
|
41 |
* sets itself as the cacher object in specific CDM Entity objects. |
|
42 |
*/ |
|
43 |
private void init() { |
|
44 |
setup(); |
|
45 |
} |
|
46 |
|
|
47 |
protected abstract void setup(); |
|
48 |
|
|
49 |
/** |
|
50 |
* Returns the singleton default cache manager. |
|
51 |
*/ |
|
52 |
public void addCacheManager(CacheManager cacheManager) { |
|
53 |
|
|
54 |
if(this.cacheManager == null){ |
|
55 |
this.cacheManager = cacheManager; |
|
56 |
} else { |
|
57 |
logger.error("There is already a CacheManager configured."); |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* Returns the default cache configuration. |
|
63 |
*/ |
|
64 |
protected CacheConfiguration getDefaultCacheConfiguration() { |
|
65 |
CacheEventListenerFactoryConfiguration factory; |
|
66 |
// For a better understanding on how to size caches, refer to |
|
67 |
// http://ehcache.org/documentation/configuration/cache-size |
|
68 |
|
|
69 |
CacheConfiguration cc = new CacheConfiguration(DEFAULT_CACHE_NAME, 500) |
|
70 |
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU) |
|
71 |
.eternal(false) |
|
72 |
// default ttl and tti set to 2 hours |
|
73 |
.timeToLiveSeconds(60*60*2) |
|
74 |
.timeToIdleSeconds(60*60*2) |
|
75 |
.statistics(true); |
|
76 |
|
|
77 |
return cc; |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* Returns the default cache |
|
82 |
* |
|
83 |
* @return |
|
84 |
*/ |
|
85 |
public Cache getDefaultCache() { |
|
86 |
Cache defaultCache = cacheManager.getCache(DEFAULT_CACHE_NAME); |
|
87 |
if(defaultCache == null) { |
|
88 |
// Create default cache |
|
89 |
cacheManager.addCache(DEFAULT_CACHE_NAME); |
|
90 |
defaultCache = cacheManager.getCache(DEFAULT_CACHE_NAME); |
|
91 |
//FIXME write test to check if default config as defined in EhCacheConfiguration is being used |
|
92 |
} |
|
93 |
return defaultCache; |
|
94 |
} |
|
95 |
|
|
96 |
@Override |
|
97 |
public void dispose(){ |
|
98 |
cacheManager.getCache(DEFAULT_CACHE_NAME).dispose(); |
|
99 |
} |
|
100 |
|
|
101 |
/** |
|
102 |
* Gets the cache element corresponding to the given {@link java.util.UUID} |
|
103 |
*/ |
|
104 |
public Element getCacheElement(UUID uuid) { |
|
105 |
return getDefaultCache().get(uuid); |
|
106 |
} |
|
107 |
|
|
108 |
@Override |
|
109 |
public void put(UUID uuid, CdmBase cdmEntity) { |
|
110 |
CdmBase cachedCdmEntity = getFromCache(uuid); |
|
111 |
if(cachedCdmEntity == null) { |
|
112 |
getDefaultCache().put(new Element(uuid, cdmEntity)); |
|
113 |
} |
|
114 |
} |
|
115 |
|
|
116 |
@Override |
|
117 |
public CdmBase load(UUID uuid) { |
|
118 |
Element e = getCacheElement(uuid); |
|
119 |
|
|
120 |
CdmBase cdmEntity; |
|
121 |
if (e == null) { |
|
122 |
// nothing in the cache for "key" (or expired) ... re-load the entity |
|
123 |
cdmEntity = findByUuid(uuid); |
|
124 |
// currently default cache is a non-null cache |
|
125 |
// We would like to have the possibility to put null values in the cache, |
|
126 |
// but we need to first distinguish between real null values and null values |
|
127 |
// returned by the service is the type of class does not match |
|
128 |
if(cdmEntity != null) { |
|
129 |
put(uuid, cdmEntity); |
|
130 |
} |
|
131 |
} else { |
|
132 |
// there is a valid element in the cache, however getObjectValue() may be null |
|
133 |
cdmEntity = (CdmBase)e.getObjectValue(); |
|
134 |
} |
|
135 |
return cdmEntity; |
|
136 |
} |
|
137 |
|
|
138 |
@Override |
|
139 |
public CdmBase getFromCache(UUID uuid) { |
|
140 |
Element e = getCacheElement(uuid); |
|
141 |
if (e == null) { |
|
142 |
return null; |
|
143 |
} else { |
|
144 |
return(CdmBase)e.getObjectValue(); |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
@Override |
|
149 |
public <T extends CdmBase> T getFromCache(T cdmBase) { |
|
150 |
return (T)getFromCache(cdmBase.getUuid()); |
|
151 |
} |
|
152 |
|
|
153 |
@Override |
|
154 |
public void putToCache(CdmBase cdmEntity) { |
|
155 |
if(cdmEntity != null) { |
|
156 |
put(cdmEntity.getUuid(), cdmEntity); |
|
157 |
} |
|
158 |
} |
|
159 |
|
|
160 |
@Override |
|
161 |
public abstract <T extends CdmBase> T load(T cdmEntity); |
|
162 |
|
|
163 |
|
|
164 |
@Override |
|
165 |
public boolean exists(UUID uuid) { |
|
166 |
return getCacheElement(uuid) != null; |
|
167 |
} |
|
168 |
|
|
169 |
@Override |
|
170 |
public boolean exists(CdmBase cdmBase) { |
|
171 |
if(cdmBase != null) { |
|
172 |
return exists(cdmBase.getUuid()); |
|
173 |
} |
|
174 |
return false; |
|
175 |
} |
|
176 |
|
|
177 |
@Override |
|
178 |
public boolean existsAndIsNotNull(UUID uuid) { |
|
179 |
Element e = getCacheElement(uuid); |
|
180 |
if (e != null) { |
|
181 |
return e.getObjectValue() != null; |
|
182 |
} |
|
183 |
return false; |
|
184 |
} |
|
185 |
|
|
186 |
/** |
|
187 |
* Finds CDM Entity by uuid |
|
188 |
*/ |
|
189 |
protected abstract CdmBase findByUuid(UUID uuid); |
|
190 |
|
|
191 |
} |
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/cache/CdmCacherBase.java | ||
---|---|---|
1 |
package eu.etaxonomy.cdm.api.cache; |
|
2 |
|
|
3 |
import java.util.UUID; |
|
4 |
|
|
5 |
import org.apache.log4j.Logger; |
|
6 |
import org.springframework.beans.factory.annotation.Autowired; |
|
7 |
|
|
8 |
import eu.etaxonomy.cdm.model.ICdmUuidCacher; |
|
9 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
|
10 |
import net.sf.ehcache.Cache; |
|
11 |
import net.sf.ehcache.CacheManager; |
|
12 |
import net.sf.ehcache.Element; |
|
13 |
import net.sf.ehcache.config.CacheConfiguration; |
|
14 |
import net.sf.ehcache.config.CacheConfiguration.CacheEventListenerFactoryConfiguration; |
|
15 |
import net.sf.ehcache.store.MemoryStoreEvictionPolicy; |
|
16 |
|
|
17 |
/** |
|
18 |
* CDM Entity Cacher class based on EhCache using UUID as key. |
|
19 |
* The cacher allows null values to be cached. |
|
20 |
* |
|
21 |
* @author cmathew |
|
22 |
*/ |
|
23 |
public abstract class CdmCacherBase implements ICdmUuidCacher { |
|
24 |
|
|
25 |
public static final Logger logger = Logger.getLogger(CdmCacherBase.class); |
|
26 |
|
|
27 |
@Autowired |
|
28 |
public CacheManager cacheManager; |
|
29 |
|
|
30 |
public static final String DEFAULT_CACHE_NAME = "cdmDefaultCache"; //TODO compare with CacheConfiguration where the name for the default cache is 'default', Why another name here? |
|
31 |
|
|
32 |
/** |
|
33 |
* Constructor which initializes a singleton {@link net.sf.ehcache.CacheManager} |
|
34 |
*/ |
|
35 |
public CdmCacherBase() { |
|
36 |
init(); |
|
37 |
} |
|
38 |
|
|
39 |
/** |
|
40 |
* Initializes an empty singleton {@link net.sf.ehcache.CacheManager} and |
|
41 |
* sets itself as the cacher object in specific CDM Entity objects. |
|
42 |
*/ |
|
43 |
private void init() { |
|
44 |
setup(); |
|
45 |
} |
|
46 |
|
|
47 |
protected abstract void setup(); |
|
48 |
|
|
49 |
/** |
|
50 |
* Returns the singleton default cache manager. |
|
51 |
*/ |
|
52 |
public void addCacheManager(CacheManager cacheManager) { |
|
53 |
|
|
54 |
if(this.cacheManager == null){ |
|
55 |
this.cacheManager = cacheManager; |
|
56 |
} else { |
|
57 |
logger.error("There is already a CacheManager configured."); |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* Returns the default cache configuration. |
|
63 |
*/ |
|
64 |
protected CacheConfiguration getDefaultCacheConfiguration() { |
|
65 |
CacheEventListenerFactoryConfiguration factory; |
|
66 |
// For a better understanding on how to size caches, refer to |
|
67 |
// http://ehcache.org/documentation/configuration/cache-size |
|
68 |
|
|
69 |
CacheConfiguration cc = new CacheConfiguration(DEFAULT_CACHE_NAME, 500) |
|
70 |
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU) |
|
71 |
.eternal(false) |
|
72 |
// default ttl and tti set to 2 hours |
|
73 |
.timeToLiveSeconds(60*60*2) |
|
74 |
.timeToIdleSeconds(60*60*2) |
|
75 |
.statistics(true); |
|
76 |
|
|
77 |
return cc; |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* Returns the default cache |
|
82 |
* |
|
83 |
* @return |
|
84 |
*/ |
|
85 |
public Cache getDefaultCache() { |
|
86 |
Cache defaultCache = cacheManager.getCache(DEFAULT_CACHE_NAME); |
|
87 |
if(defaultCache == null) { |
|
88 |
// Create default cache |
|
89 |
cacheManager.addCache(DEFAULT_CACHE_NAME); |
|
90 |
defaultCache = cacheManager.getCache(DEFAULT_CACHE_NAME); |
|
91 |
//FIXME write test to check if default config as defined in EhCacheConfiguration is being used |
|
92 |
} |
|
93 |
return defaultCache; |
|
94 |
} |
|
95 |
|
|
96 |
@Override |
|
97 |
public void dispose(){ |
|
98 |
cacheManager.getCache(DEFAULT_CACHE_NAME).dispose(); |
|
99 |
} |
|
100 |
|
|
101 |
/** |
|
102 |
* Gets the cache element corresponding to the given {@link java.util.UUID} |
|
103 |
*/ |
|
104 |
public Element getCacheElement(UUID uuid) { |
|
105 |
return getDefaultCache().get(uuid); |
|
106 |
} |
|
107 |
|
|
108 |
@Override |
|
109 |
public void put(UUID uuid, CdmBase cdmEntity) { |
|
110 |
CdmBase cachedCdmEntity = getFromCache(uuid); |
|
111 |
if(cachedCdmEntity == null) { |
|
112 |
getDefaultCache().put(new Element(uuid, cdmEntity)); |
|
113 |
} |
|
114 |
} |
|
115 |
|
|
116 |
@Override |
|
117 |
public CdmBase load(UUID uuid) { |
|
118 |
Element e = getCacheElement(uuid); |
|
119 |
|
|
120 |
CdmBase cdmEntity; |
|
121 |
if (e == null) { |
|
122 |
// nothing in the cache for "key" (or expired) ... re-load the entity |
|
123 |
cdmEntity = findByUuid(uuid); |
|
124 |
// currently default cache is a non-null cache |
|
125 |
// We would like to have the possibility to put null values in the cache, |
|
126 |
// but we need to first distinguish between real null values and null values |
|
127 |
// returned by the service is the type of class does not match |
|
128 |
if(cdmEntity != null) { |
|
129 |
put(uuid, cdmEntity); |
|
130 |
} |
|
131 |
} else { |
|
132 |
// there is a valid element in the cache, however getObjectValue() may be null |
|
133 |
cdmEntity = (CdmBase)e.getObjectValue(); |
|
134 |
} |
|
135 |
return cdmEntity; |
|
136 |
} |
|
137 |
|
|
138 |
@Override |
|
139 |
public CdmBase getFromCache(UUID uuid) { |
|
140 |
Element e = getCacheElement(uuid); |
|
141 |
if (e == null) { |
|
142 |
return null; |
|
143 |
} else { |
|
144 |
return(CdmBase)e.getObjectValue(); |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
@Override |
|
149 |
public <T extends CdmBase> T getFromCache(T cdmBase) { |
|
150 |
return (T)getFromCache(cdmBase.getUuid()); |
|
151 |
} |
|
152 |
|
|
153 |
@Override |
|
154 |
public void putToCache(CdmBase cdmEntity) { |
|
155 |
if(cdmEntity != null) { |
|
156 |
put(cdmEntity.getUuid(), cdmEntity); |
|
157 |
} |
|
158 |
} |
|
159 |
|
|
160 |
@Override |
|
161 |
public abstract <T extends CdmBase> T load(T cdmEntity); |
|
162 |
|
|
163 |
|
|
164 |
@Override |
|
165 |
public boolean exists(UUID uuid) { |
|
166 |
return getCacheElement(uuid) != null; |
|
167 |
} |
|
168 |
|
|
169 |
@Override |
|
170 |
public boolean exists(CdmBase cdmBase) { |
|
171 |
if(cdmBase != null) { |
|
172 |
return exists(cdmBase.getUuid()); |
|
173 |
} |
|
174 |
return false; |
|
175 |
} |
|
176 |
|
|
177 |
@Override |
|
178 |
public boolean existsAndIsNotNull(UUID uuid) { |
|
179 |
Element e = getCacheElement(uuid); |
|
180 |
if (e != null) { |
|
181 |
return e.getObjectValue() != null; |
|
182 |
} |
|
183 |
return false; |
|
184 |
} |
|
185 |
|
|
186 |
/** |
|
187 |
* Finds CDM Entity by uuid |
|
188 |
*/ |
|
189 |
protected abstract CdmBase findByUuid(UUID uuid); |
|
190 |
|
|
191 |
@Override |
|
192 |
public boolean ignoreRecursiveLoad(CdmBase cdmBase){ |
|
193 |
return false; |
|
194 |
} |
|
195 |
} |
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/cache/CdmTermCacher.java | ||
---|---|---|
15 | 15 |
* @author cmathew |
16 | 16 |
*/ |
17 | 17 |
@Component |
18 |
public class CdmTermCacher extends CdmCacher { |
|
18 |
public class CdmTermCacher extends CdmCacherBase {
|
|
19 | 19 |
|
20 | 20 |
@Autowired |
21 | 21 |
ITermService termService; |
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/config/EhCacheConfiguration.java | ||
---|---|---|
14 | 14 |
import org.springframework.context.annotation.Bean; |
15 | 15 |
import org.springframework.context.annotation.Configuration; |
16 | 16 |
|
17 |
import eu.etaxonomy.cdm.api.cache.CdmCacher; |
|
17 |
import eu.etaxonomy.cdm.api.cache.CdmCacherBase;
|
|
18 | 18 |
import net.sf.ehcache.CacheManager; |
19 | 19 |
import net.sf.ehcache.config.CacheConfiguration; |
20 | 20 |
import net.sf.ehcache.config.CacheConfiguration.CacheEventListenerFactoryConfiguration; |
... | ... | |
65 | 65 |
|
66 | 66 |
/** |
67 | 67 |
* Returns the default cache configuration for the cache |
68 |
* named {@link CdmCacher#DEFAULT_CACHE_NAME "cdmDefaultCache"} |
|
68 |
* named {@link CdmCacherBase#DEFAULT_CACHE_NAME "cdmDefaultCache"}
|
|
69 | 69 |
* |
70 | 70 |
* @return |
71 | 71 |
*/ |
... | ... | |
76 | 76 |
// For a better understanding on how to size caches, refer to |
77 | 77 |
// http://ehcache.org/documentation/configuration/cache-size |
78 | 78 |
|
79 |
CacheConfiguration cc = new CacheConfiguration(CdmCacher.DEFAULT_CACHE_NAME, 500) |
|
79 |
CacheConfiguration cc = new CacheConfiguration(CdmCacherBase.DEFAULT_CACHE_NAME, 500)
|
|
80 | 80 |
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU) |
81 | 81 |
.maxEntriesLocalHeap(10) // avoid ehache consuming too much heap |
82 | 82 |
.eternal(false) |
cdmlib-services/src/main/java/eu/etaxonomy/cdm/api/service/IService.java | ||
---|---|---|
132 | 132 |
* <p> |
133 | 133 |
* <b>WARNING:</b>This method should <em>ONLY</em> be used when it is absolutely |
134 | 134 |
* necessary and safe to ensure that the hibernate session is not flushed before a read |
135 |
* query. A use case for this is the {@link eu.etaxonomy.cdm.api.cache.CdmCacher CdmCacher}, |
|
136 |
* (ticket #4276) where a call to {@link eu.etaxonomy.cdm.api.cache.CdmCacher#load(UUID) load} |
|
135 |
* query. A use case for this is the {@link eu.etaxonomy.cdm.api.cache.CdmCacherBase CdmCacher},
|
|
136 |
* (ticket #4276) where a call to {@link eu.etaxonomy.cdm.api.cache.CdmCacherBase#load(UUID) load}
|
|
137 | 137 |
* the CDM Entity using the standard {@link #find(UUID) find} method results in recursion |
138 | 138 |
* due to the fact that the {@link #find(UUID) find} method triggers a hibernate session |
139 | 139 |
* flush which eventually could call {@link eu.etaxonomy.cdm.model.name.NonViralName#getNameCache getNameCache}, |
140 | 140 |
* which in turn (in the event that name cache is null) eventually calls the |
141 |
* {@link eu.etaxonomy.cdm.api.cache.CdmCacher#load(UUID uuid) load} again. |
|
141 |
* {@link eu.etaxonomy.cdm.api.cache.CdmCacherBase#load(UUID uuid) load} again.
|
|
142 | 142 |
* Apart from these kind of exceptional circumstances, the standard {@link #find(UUID) find} |
143 | 143 |
* method should always be used to ensure that the persistence layer is always in sync with the |
144 | 144 |
* underlying database. |
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/cache/CdmCacherBaseTest.java | ||
---|---|---|
1 |
package eu.etaxonomy.cdm.api.cache; |
|
2 |
|
|
3 |
import java.io.FileNotFoundException; |
|
4 |
|
|
5 |
import org.apache.log4j.Logger; |
|
6 |
import org.junit.Assert; |
|
7 |
import org.junit.Ignore; |
|
8 |
import org.junit.Test; |
|
9 |
import org.unitils.spring.annotation.SpringBeanByType; |
|
10 |
|
|
11 |
import eu.etaxonomy.cdm.api.service.IReferenceService; |
|
12 |
import eu.etaxonomy.cdm.api.service.ITaxonService; |
|
13 |
import eu.etaxonomy.cdm.model.common.Language; |
|
14 |
import eu.etaxonomy.cdm.model.name.INonViralName; |
|
15 |
import eu.etaxonomy.cdm.model.name.Rank; |
|
16 |
import eu.etaxonomy.cdm.model.name.TaxonNameFactory; |
|
17 |
import eu.etaxonomy.cdm.model.reference.Reference; |
|
18 |
import eu.etaxonomy.cdm.model.reference.ReferenceFactory; |
|
19 |
import eu.etaxonomy.cdm.model.taxon.Taxon; |
|
20 |
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest; |
|
21 |
|
|
22 |
@Ignore |
|
23 |
public class CdmCacherBaseTest extends CdmIntegrationTest { |
|
24 |
private static final Logger logger = Logger.getLogger(CdmCacherBaseTest.class); |
|
25 |
|
|
26 |
@SpringBeanByType |
|
27 |
private CdmCacherBase cdmCacherBase; |
|
28 |
|
|
29 |
@SpringBeanByType |
|
30 |
private IReferenceService referenceService; |
|
31 |
|
|
32 |
@SpringBeanByType |
|
33 |
private ITaxonService taxonService; |
|
34 |
|
|
35 |
@Test |
|
36 |
public void testLanguageCache() { |
|
37 |
Language defaultLanguage = Language.getDefaultLanguage(); |
|
38 |
|
|
39 |
Language defaultLanguageInCache = (Language)cdmCacherBase.getFromCache(defaultLanguage.getUuid()); |
|
40 |
Assert.assertEquals("Loaded Language Term should match Language Term in Cache",defaultLanguage,defaultLanguageInCache); |
|
41 |
|
|
42 |
Language language = Language.getLanguageFromUuid(Language.uuidFrench); |
|
43 |
Language languageInCache = (Language)cdmCacherBase.getFromCache(language.getUuid()); |
|
44 |
Assert.assertEquals("Loaded Language Term should match Language Term in Cache",language,languageInCache); |
|
45 |
|
|
46 |
// Following test is just to make sure no exception is raised when saving a taxon corresponding |
|
47 |
// to a taxon name with no name cache to begin with |
|
48 |
Reference sec = ReferenceFactory.newDatabase(); |
|
49 |
referenceService.save(sec); |
|
50 |
Taxon taxon = Taxon.NewInstance(TaxonNameFactory.NewNonViralInstance(Rank.SERIES()), sec); |
|
51 |
taxon.setTitleCache("Tax" + "CdmCacher", true); |
|
52 |
taxonService.save(taxon); |
|
53 |
INonViralName nvn = taxon.getName(); |
|
54 |
String nameCache = nvn.getNameCache(); |
|
55 |
logger.warn("name cache : " + nameCache); |
|
56 |
} |
|
57 |
|
|
58 |
@Override |
|
59 |
public void createTestDataSet() throws FileNotFoundException {} |
|
60 |
} |
cdmlib-services/src/test/java/eu/etaxonomy/cdm/api/cache/CdmCacherTest.java | ||
---|---|---|
1 |
package eu.etaxonomy.cdm.api.cache; |
|
2 |
|
|
3 |
import java.io.FileNotFoundException; |
|
4 |
|
|
5 |
import org.apache.log4j.Logger; |
|
6 |
import org.junit.Assert; |
|
7 |
import org.junit.Ignore; |
|
8 |
import org.junit.Test; |
|
9 |
import org.unitils.spring.annotation.SpringBeanByType; |
|
10 |
|
|
11 |
import eu.etaxonomy.cdm.api.service.IReferenceService; |
|
12 |
import eu.etaxonomy.cdm.api.service.ITaxonService; |
|
13 |
import eu.etaxonomy.cdm.model.common.Language; |
|
14 |
import eu.etaxonomy.cdm.model.name.INonViralName; |
|
15 |
import eu.etaxonomy.cdm.model.name.Rank; |
|
16 |
import eu.etaxonomy.cdm.model.name.TaxonNameFactory; |
|
17 |
import eu.etaxonomy.cdm.model.reference.Reference; |
|
18 |
import eu.etaxonomy.cdm.model.reference.ReferenceFactory; |
|
19 |
import eu.etaxonomy.cdm.model.taxon.Taxon; |
|
20 |
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest; |
|
21 |
|
|
22 |
@Ignore |
|
23 |
public class CdmCacherTest extends CdmIntegrationTest { |
|
24 |
private static final Logger logger = Logger.getLogger(CdmCacherTest.class); |
|
25 |
|
|
26 |
@SpringBeanByType |
|
27 |
private CdmCacher cdmCacher; |
|
28 |
|
|
29 |
@SpringBeanByType |
|
30 |
private IReferenceService referenceService; |
|
31 |
|
|
32 |
@SpringBeanByType |
|
33 |
private ITaxonService taxonService; |
|
34 |
|
|
35 |
@Test |
|
36 |
public void testLanguageCache() { |
|
37 |
Language defaultLanguage = Language.getDefaultLanguage(); |
|
38 |
|
|
39 |
Language defaultLanguageInCache = (Language)cdmCacher.getFromCache(defaultLanguage.getUuid()); |
|
40 |
Assert.assertEquals("Loaded Language Term should match Language Term in Cache",defaultLanguage,defaultLanguageInCache); |
|
41 |
|
|
42 |
Language language = Language.getLanguageFromUuid(Language.uuidFrench); |
|
43 |
Language languageInCache = (Language)cdmCacher.getFromCache(language.getUuid()); |
|
44 |
Assert.assertEquals("Loaded Language Term should match Language Term in Cache",language,languageInCache); |
|
45 |
|
|
46 |
// Following test is just to make sure no exception is raised when saving a taxon corresponding |
|
47 |
// to a taxon name with no name cache to begin with |
|
48 |
Reference sec = ReferenceFactory.newDatabase(); |
|
49 |
referenceService.save(sec); |
|
50 |
Taxon taxon = Taxon.NewInstance(TaxonNameFactory.NewNonViralInstance(Rank.SERIES()), sec); |
|
51 |
taxon.setTitleCache("Tax" + "CdmCacher", true); |
|
52 |
taxonService.save(taxon); |
|
53 |
INonViralName nvn = taxon.getName(); |
|
54 |
String nameCache = nvn.getNameCache(); |
|
55 |
logger.warn("name cache : " + nameCache); |
|
56 |
} |
|
57 |
|
|
58 |
@Override |
|
59 |
public void createTestDataSet() throws FileNotFoundException {} |
|
60 |
} |
Also available in: Unified diff
rename CdmCacher to CdmCacherBase