1
|
package eu.etaxonomy.cdm.api.cache;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.util.UUID;
|
5
|
|
6
|
import org.springframework.stereotype.Component;
|
7
|
|
8
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
9
|
import eu.etaxonomy.cdm.api.config.EhCacheConfiguration;
|
10
|
import eu.etaxonomy.cdm.cache.CacheLoader;
|
11
|
import eu.etaxonomy.cdm.cache.CdmEntityCacheKey;
|
12
|
import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
|
13
|
import eu.etaxonomy.cdm.config.ConfigFileUtil;
|
14
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
15
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
16
|
import eu.etaxonomy.cdm.model.term.TermBase;
|
17
|
import eu.etaxonomy.taxeditor.service.TermServiceRequestExecutor;
|
18
|
import eu.etaxonomy.taxeditor.session.CdmEntitySession;
|
19
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
20
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManager;
|
21
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionManagerObserver;
|
22
|
import net.sf.ehcache.config.CacheConfiguration;
|
23
|
import net.sf.ehcache.config.DiskStoreConfiguration;
|
24
|
import net.sf.ehcache.config.SizeOfPolicyConfiguration;
|
25
|
|
26
|
/**
|
27
|
* Class which uses CDM services to cache cdm entities
|
28
|
*
|
29
|
* FIXME: Currently only handles term entities. It would be
|
30
|
* interesting to have a generic method which finds the
|
31
|
* correct service to load / cache the entity.
|
32
|
*
|
33
|
* @author cmathew
|
34
|
*/
|
35
|
@Component //FIXME This indicates that the CdmServiceCacher is initialized as Spring Component but it seems only to be instantiated directly
|
36
|
public class CdmServiceCacher extends CdmCacher implements ICdmEntitySessionManagerObserver {
|
37
|
|
38
|
|
39
|
private ICdmEntitySessionManager cdmEntitySessionManager;
|
40
|
|
41
|
private CdmTransientEntityCacher cdmTransientEntityCacher;
|
42
|
|
43
|
private CacheLoader cacheLoader;
|
44
|
|
45
|
@Override
|
46
|
protected void setup() {
|
47
|
|
48
|
setUpCacheManager();
|
49
|
|
50
|
DefinedTermBase.setCacher(this);
|
51
|
CdmTransientEntityCacher.setPermanentCacher(this);
|
52
|
TermServiceRequestExecutor.setDefaultCacher(this);
|
53
|
|
54
|
cacheLoader = new CacheLoader(this);
|
55
|
}
|
56
|
|
57
|
private void setUpCacheManager() {
|
58
|
|
59
|
EhCacheConfiguration cacheConfig = new EhCacheConfiguration();
|
60
|
|
61
|
DiskStoreConfiguration diskStoreConfiguration = new DiskStoreConfiguration();
|
62
|
File ehcacheFolder;
|
63
|
try {
|
64
|
ehcacheFolder = ConfigFileUtil.getCdmHomeSubDir("taxeditor-ehcache");
|
65
|
} catch (Exception e){
|
66
|
ehcacheFolder = ConfigFileUtil.getCdmHomeSubDirFallback("taxeditor-ehcache");
|
67
|
}
|
68
|
// FIXME use subfolder per taxeditor version to allow running multiple installations in parallel
|
69
|
// String taxEditorVersion = ..;
|
70
|
// File ehcacheFolder = new File(ehcacheFolder, taxEditorVersion);
|
71
|
diskStoreConfiguration.setPath(ehcacheFolder.getAbsolutePath());
|
72
|
|
73
|
cacheConfig.setDiskStoreConfiguration(diskStoreConfiguration);
|
74
|
addCacheManager(cacheConfig.cacheManager());
|
75
|
|
76
|
}
|
77
|
|
78
|
|
79
|
@Override
|
80
|
protected CacheConfiguration getDefaultCacheConfiguration() {
|
81
|
// For a better understanding on how to size caches, refer to
|
82
|
// http://ehcache.org/documentation/configuration/cache-size
|
83
|
|
84
|
SizeOfPolicyConfiguration sizeOfConfig = new SizeOfPolicyConfiguration();
|
85
|
sizeOfConfig.setMaxDepth(100);
|
86
|
sizeOfConfig.setMaxDepthExceededBehavior("abort");
|
87
|
|
88
|
return new CacheConfiguration(DEFAULT_CACHE_NAME, 0)
|
89
|
.eternal(true)
|
90
|
.statistics(true)
|
91
|
.sizeOfPolicy(sizeOfConfig)
|
92
|
.overflowToOffHeap(false);
|
93
|
|
94
|
}
|
95
|
|
96
|
@Override
|
97
|
protected CdmBase findByUuid(UUID uuid) {
|
98
|
CdmBase term = CdmApplicationState.getCurrentAppConfig().getTermService().findWithoutFlush(uuid);
|
99
|
return load(term);
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
public boolean isCachable(CdmBase cdmEntity) {
|
104
|
// if(cdmEntity != null && cdmEntity instanceof DefinedTermBase) {
|
105
|
if(cdmEntity != null && cdmEntity instanceof TermBase) {
|
106
|
return true;
|
107
|
}
|
108
|
return false;
|
109
|
}
|
110
|
|
111
|
|
112
|
public void setCdmEntitySessionManager(ICdmEntitySessionManager cdmEntitySessionManager) {
|
113
|
this.cdmEntitySessionManager = cdmEntitySessionManager;
|
114
|
if(cdmEntitySessionManager != null) {
|
115
|
cdmEntitySessionManager.addSessionObserver(this);
|
116
|
}
|
117
|
}
|
118
|
|
119
|
|
120
|
public CdmTransientEntityCacher getCurrentCacher() {
|
121
|
ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
|
122
|
if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
|
123
|
return ((CdmEntitySession) cdmEntitySession).getCacher();
|
124
|
}
|
125
|
return null;
|
126
|
}
|
127
|
|
128
|
@Override
|
129
|
public <T extends CdmBase> T getFromCache(T cdmBase) {
|
130
|
T cachedCdmEntity = null;
|
131
|
// first we check in the active session cache if the
|
132
|
// entity has been loaded there
|
133
|
// FIXME:Remoting do we really need the cdmTransientEntityCacher
|
134
|
// here. Is it not guarenteed that all every entity which 'isCachable'
|
135
|
// by this cacher is cached only in this cacher ?
|
136
|
if(!isCachable(cdmBase) && cdmTransientEntityCacher != null) {
|
137
|
CdmEntityCacheKey<T> key = CdmTransientEntityCacher.generateKey(cdmBase);
|
138
|
cachedCdmEntity = cdmTransientEntityCacher.getFromCache(key);
|
139
|
|
140
|
}
|
141
|
if(cachedCdmEntity == null) {
|
142
|
cachedCdmEntity = super.getFromCache(cdmBase);
|
143
|
}
|
144
|
return cachedCdmEntity;
|
145
|
}
|
146
|
|
147
|
@Override
|
148
|
public <T extends CdmBase> T load(T cdmEntity) {
|
149
|
T cachedCdmEntity = (T)getFromCache(cdmEntity.getUuid());
|
150
|
|
151
|
if(isCachable(cdmEntity) && cachedCdmEntity == null) {
|
152
|
cachedCdmEntity = cacheLoader.load(cdmEntity, false, true);
|
153
|
}
|
154
|
return cachedCdmEntity;
|
155
|
}
|
156
|
|
157
|
@Override
|
158
|
public void changed() {
|
159
|
ICdmEntitySession cdmEntitySession = cdmEntitySessionManager.getActiveSession();
|
160
|
if(cdmEntitySession != null && cdmEntitySession instanceof CdmEntitySession) {
|
161
|
this.cdmTransientEntityCacher = ((CdmEntitySession) cdmEntitySession).getCacher();
|
162
|
} else {
|
163
|
this.cdmTransientEntityCacher = null;
|
164
|
}
|
165
|
}
|
166
|
}
|