1
|
package eu.etaxonomy.cdm.api.cache;
|
2
|
|
3
|
import java.util.UUID;
|
4
|
|
5
|
import org.springframework.stereotype.Component;
|
6
|
|
7
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
8
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
9
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
10
|
import eu.etaxonomy.cdm.model.common.TermBase;
|
11
|
import eu.etaxonomy.taxeditor.remoting.cache.CacheLoader;
|
12
|
import eu.etaxonomy.taxeditor.remoting.cache.CdmEntityCacheKey;
|
13
|
import eu.etaxonomy.taxeditor.remoting.cache.CdmTransientEntityCacher;
|
14
|
import eu.etaxonomy.taxeditor.service.TermServiceRequestExecutor;
|
15
|
import eu.etaxonomy.taxeditor.session.CdmEntitySession;
|
16
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
17
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
|
18
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver;
|
19
|
import net.sf.ehcache.config.CacheConfiguration;
|
20
|
import net.sf.ehcache.config.SizeOfPolicyConfiguration;
|
21
|
|
22
|
/**
|
23
|
* Class which uses CDM services to cache cdm entities
|
24
|
*
|
25
|
* FIXME: Currently only handles term 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 implements ICdmEntitySessionManagerObserver {
|
35
|
|
36
|
|
37
|
|
38
|
private ICdmEntitySessionManager cdmEntitySessionManager;
|
39
|
|
40
|
private CdmTransientEntityCacher cdmTransientEntityCacher;
|
41
|
|
42
|
private CacheLoader cacheLoader;
|
43
|
|
44
|
@Override
|
45
|
protected void setup() {
|
46
|
DefinedTermBase.setCacher(this);
|
47
|
CdmTransientEntityCacher.setDefaultCacher(this);
|
48
|
TermServiceRequestExecutor.setDefaultCacher(this);
|
49
|
|
50
|
cacheLoader = new CacheLoader(this);
|
51
|
}
|
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 = CdmApplicationState.getCurrentAppConfig().getTermService().findWithoutFlush(uuid);
|
74
|
return load(term);
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
public boolean isCachable(CdmBase cdmEntity) {
|
79
|
// if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
|
80
|
if(cdmEntity != null && cdmEntity instanceof TermBase) {
|
81
|
return true;
|
82
|
}
|
83
|
return false;
|
84
|
}
|
85
|
|
86
|
|
87
|
public void setCdmEntitySessionManager(ICdmEntitySessionManager cdmEntitySessionManager) {
|
88
|
this.cdmEntitySessionManager = cdmEntitySessionManager;
|
89
|
if(cdmEntitySessionManager != null) {
|
90
|
cdmEntitySessionManager.addSessionObserver(this);
|
91
|
}
|
92
|
}
|
93
|
|
94
|
|
95
|
public CdmTransientEntityCacher getCurrentCacher() {
|
96
|
ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
|
97
|
if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
|
98
|
return ((CdmEntitySession) cdmEntitySession).getCacher();
|
99
|
}
|
100
|
return null;
|
101
|
}
|
102
|
|
103
|
@Override
|
104
|
public CdmBase getFromCache(CdmBase cdmBase) {
|
105
|
CdmBase cachedCdmEntity = null;
|
106
|
// first we check in the active session cache if the
|
107
|
// entity has been loaded there
|
108
|
// FIXME:Remoting do we really need the cdmTransientEntityCacher
|
109
|
// here. Is it not guarenteed that all every entity which 'isCachable'
|
110
|
// by this cacher is cached only in this cacher ?
|
111
|
if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
|
112
|
CdmEntityCacheKey key = CdmTransientEntityCacher.generateKey(cdmBase);
|
113
|
cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
|
114
|
|
115
|
}
|
116
|
if(cachedCdmEntity == null) {
|
117
|
cachedCdmEntity = super.getFromCache(cdmBase);
|
118
|
}
|
119
|
return cachedCdmEntity;
|
120
|
}
|
121
|
|
122
|
|
123
|
/* (non-Javadoc)
|
124
|
* @see eu.etaxonomy.cdm.api.cache.CdmCacher#load(eu.etaxonomy.cdm.model.common.CdmBase)
|
125
|
*/
|
126
|
@Override
|
127
|
public CdmBase load(CdmBase cdmEntity) {
|
128
|
CdmBase cachedCdmEntity = getFromCache(cdmEntity.getUuid());
|
129
|
|
130
|
if(isCachable(cdmEntity) && cachedCdmEntity == null) {
|
131
|
cachedCdmEntity = cacheLoader.load(cdmEntity, false, true);
|
132
|
}
|
133
|
return cachedCdmEntity;
|
134
|
}
|
135
|
|
136
|
/* (non-Javadoc)
|
137
|
* @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver#changed()
|
138
|
*/
|
139
|
@Override
|
140
|
public void changed() {
|
141
|
ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
|
142
|
if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
|
143
|
this.cdmTransientEntityCacher = ((CdmEntitySession) cdmEntitySession).getCacher();
|
144
|
} else {
|
145
|
this.cdmTransientEntityCacher = null;
|
146
|
}
|
147
|
}
|
148
|
}
|