96c94964cc2fb0698048ac580b0051a8d103a068
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / api / cache / CdmServiceCacher.java
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.CdmEntityCacheKey;
16 import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
17 import eu.etaxonomy.taxeditor.service.TermServiceRequestExecutor;
18 import eu.etaxonomy.taxeditor.session.CdmEntitySession;
19 import eu.etaxonomy.taxeditor.session.CdmEntitySessionManager;
20 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
21
22 /**
23 * Class which uses CDM services to cache cdm entities
24 *
25 * FIXME: Currently only handles terms entities. It would be
26 * interesting to have a generic method which finds the
27 * correct service to load / cache the entity.
28 *
29 * @author cmathew
30 *
31 * @param <T>
32 */
33 @Component
34 public class CdmServiceCacher extends CdmCacher {
35
36 @Autowired
37 ITermService termService;
38
39 @Autowired
40 private CdmEntitySessionManager cdmEntitySessionManager;
41
42 private CdmTransientEntityCacher cdmTransientEntityCacher;
43
44 private CacheLoader cacheLoader;
45
46 @Override
47 protected void setup() {
48 DefinedTermBase.setCacher(this);
49 CdmTransientEntityCacher.setDefaultCacher(this);
50 TermServiceRequestExecutor.setDefaultCacher(this);
51 cacheLoader = new CacheLoader(this);
52 }
53
54 @Override
55 protected CacheConfiguration getDefaultCacheConfiguration() {
56 // For a better understanding on how to size caches, refer to
57 // http://ehcache.org/documentation/configuration/cache-size
58
59 SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
60 sizeOfConfig.setMaxDepth(100);
61 sizeOfConfig.setMaxDepthExceededBehavior("abort");
62
63 return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
64 .eternal(true)
65 .statistics(true)
66 .sizeOfPolicy(sizeOfConfig)
67 .overflowToOffHeap(false);
68
69 }
70
71 @Override
72 protected CdmBase findByUuid(UUID uuid) {
73 CdmBase term = termService.findWithoutFlush(uuid);
74 return load(term);
75 }
76
77 /* (non-Javadoc)
78 * @see eu.etaxonomy.cdm.model.ICdmCacher#isCachable(eu.etaxonomy.cdm.model.common.CdmBase)
79 */
80 @Override
81 public boolean isCachable(CdmBase cdmEntity) {
82 if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
83 return true;
84 }
85 return false;
86 }
87
88 public void setActiveSession(ICdmEntitySession cdmEntitySession) {
89 if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
90 this.cdmTransientEntityCacher = ((CdmEntitySession) cdmEntitySession).getCacher();
91 } else {
92 this.cdmTransientEntityCacher = null;
93 }
94 }
95
96 @Override
97 public CdmBase getFromCache(CdmBase cdmBase) {
98 CdmBase cachedCdmEntity = null;
99 // first we check in the active session cache if the
100 if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
101 CdmEntityCacheKey key = CdmTransientEntityCacher.generateKey(cdmBase);
102 cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
103
104 }
105 if(cachedCdmEntity == null) {
106 cachedCdmEntity = super.getFromCache(cdmBase);
107 }
108 return cachedCdmEntity;
109 }
110
111
112 /* (non-Javadoc)
113 * @see eu.etaxonomy.cdm.api.cache.CdmCacher#load(eu.etaxonomy.cdm.model.common.CdmBase)
114 */
115 @Override
116 public CdmBase load(CdmBase cdmEntity) {
117 CdmBase cachedCdmEntity = getFromCache(cdmEntity.getUuid());
118
119 if(isCachable(cdmEntity)) {
120 cachedCdmEntity = cacheLoader.load(cdmEntity, true, false);
121 }
122 return cachedCdmEntity;
123 }
124
125
126 }