664c2cf6065b6c204e71444d8c1638eb1bcd3839
[taxeditor.git] / eu.etaxonomy.taxeditor.remoting / src / main / java / eu / etaxonomy / cdm / api / cache / CdmTransientEntityCacher.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.api.cache;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.UUID;
16
17 import net.sf.ehcache.Cache;
18 import net.sf.ehcache.Element;
19 import net.sf.ehcache.config.CacheConfiguration;
20 import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
21
22 import org.apache.log4j.Logger;
23 import org.springframework.beans.factory.annotation.Autowired;
24
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26
27 /**
28 * @author cmathew
29 * @date 14 Oct 2014
30 *
31 */
32
33 public class CdmTransientEntityCacher extends CdmServiceCacher {
34
35 private static final Logger logger = Logger.getLogger(CdmTransientEntityCacher.class);
36
37 @Autowired
38 private CdmEntityCachingUtils cdmEntityCachingUtils;
39
40 private String cacheId;
41
42 private Cache cache;
43
44 private Cache cdmlibModelCache;
45
46 private static boolean isRecursiveEnabled;
47
48 public CdmTransientEntityCacher(String cacheId) {
49 this.cacheId = cacheId;
50 // Create entity cache
51 cache = new Cache(getEntityCacheConfiguration(cacheId));
52 getDefaultCacheManager().addCache(cache);
53
54 cdmlibModelCache = CdmRemoteCacheManager.getInstance().getCdmModelGetMethodsCache();
55
56 }
57
58 public CdmTransientEntityCacher(Object obj) {
59 this(obj.getClass().getName() + String.valueOf(obj.hashCode()));
60 }
61
62 /**
63 * Returns the default cache configuration.
64 *
65 * @return
66 */
67 private CacheConfiguration getEntityCacheConfiguration(String cacheId) {
68 // For a better understanding on how to size caches, refer to
69 // http://ehcache.org/documentation/configuration/cache-size
70 return new CacheConfiguration(cacheId, 500)
71 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
72 .eternal(false)
73 // default ttl and tti set to 2 hours
74 .timeToLiveSeconds(60*60*2)
75 .timeToIdleSeconds(60*60*2);
76
77 // This is 2.6.9 API
78 //.maxEntriesLocalDisk(1000);
79 //.persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP));
80 }
81
82
83 /**
84 * Returns the cache corresponding to the cache id
85 *
86 * @param cacheId
87 * @return
88 */
89 private Cache getCache() {
90 return getDefaultCacheManager().getCache(cacheId);
91 }
92
93
94 /**
95 * Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
96 * in the cache corresponding to the given cache id
97 *
98 * @param cacheId
99 * @param uuid
100 * @param cdmEntity
101 */
102 public CdmBase load(CdmBase cdmEntity, boolean recursive) {
103 CdmEntityCacheKey id = new CdmEntityCacheKey(cdmEntity);
104 // first look in 'this' cache
105 CdmBase cachedCdmEntity = getFromCache(id);
106
107 if(cachedCdmEntity == null) {
108 // then try the permanent (uuid-based) cache
109 cachedCdmEntity = getFromCache(cdmEntity.getUuid());
110 }
111 if(isRecursiveEnabled && recursive) {
112 String className = cdmEntity.getClass().getName();
113 CdmModelGetMethodFromClass cmgmfc = getFromCdmlibModelCache(className);
114 if(cmgmfc != null) {
115 List<String> getMethods = cmgmfc.getGetMethods();
116 }
117 }
118 if(cachedCdmEntity != null) {
119 return cachedCdmEntity;
120 } else {
121 getCache().put(new Element(id, cdmEntity));
122 return cdmEntity;
123 }
124 }
125
126 public void put(CdmBase cdmEntity) {
127 CdmEntityCacheKey id = new CdmEntityCacheKey(cdmEntity);
128 Element cachedCdmEntityElement = getCacheElement(id);
129
130 if(cachedCdmEntityElement == null) {
131 cachedCdmEntityElement = getCacheElement(cdmEntity.getUuid());
132 if(cachedCdmEntityElement != null) {
133 logger.info("Cdm Entity with id : " + cdmEntity.getId() + " already exists in permanent cache. Ignoring put.");
134 return;
135 }
136 }
137
138 getCache().put(new Element(id, cdmEntity));
139 }
140
141
142 private Element getCacheElement(CdmEntityCacheKey key) {
143 return getCache().get(key);
144 }
145
146 public CdmModelGetMethodFromClass getFromCdmlibModelCache(String className) {
147 Element e = cdmlibModelCache.get(className);
148 if (e == null) {
149 return null;
150 } else {
151 return (CdmModelGetMethodFromClass) e.getObjectValue();
152 }
153 }
154
155 public CdmBase getFromCache(CdmEntityCacheKey id) {
156 Element e = getCacheElement(id);
157 if (e == null) {
158 return null;
159 } else {
160 return (CdmBase) e.getObjectValue();
161 }
162 }
163
164 public CdmBase getFromCache(Class<? extends CdmBase> clazz, int id) {
165 CdmEntityCacheKey cacheId = generateKey(clazz,id);
166 return getFromCache(cacheId);
167 }
168
169 public CdmBase getFromCache(CdmBase cdmBase) {
170 CdmEntityCacheKey cacheId = generateKey(cdmBase);
171 return getFromCache(cacheId);
172 }
173
174 public List<CdmBase> getAllEntities() {
175 List<CdmBase> entities = new ArrayList<CdmBase>();
176 Map<String, CdmBase> elementsMap = getCache().getAllWithLoader(getCache().getKeys(), null);
177 for (Map.Entry<String, CdmBase> entry : elementsMap.entrySet()) {
178 entities.add(entry.getValue());
179 }
180 return entities;
181 }
182
183
184
185
186 /* (non-Javadoc)
187 * @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
188 */
189 @Override
190 public boolean exists(UUID uuid) {
191 return (getCacheElement(uuid) != null || super.getCacheElement(uuid) != null);
192 }
193
194 public boolean exists(CdmEntityCacheKey key) {
195 return (getCacheElement(key) != null);
196 }
197
198
199 /* (non-Javadoc)
200 * @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
201 */
202 @Override
203 public boolean existsAndIsNotNull(UUID uuid) {
204 return getFromCache(uuid) != null;
205 }
206
207 public boolean existsAndIsNotNull(CdmEntityCacheKey id) {
208 return getFromCache(id) != null;
209 }
210
211
212 public static CdmEntityCacheKey generateKey(Class<? extends CdmBase> clazz, int id) {
213 return new CdmEntityCacheKey(clazz, id);
214 }
215
216
217 public static CdmEntityCacheKey generateKey(CdmBase cdmBase) {
218 Class<? extends CdmBase> entityClass = cdmBase.getClass();
219 int id = cdmBase.getId();
220 return new CdmEntityCacheKey(entityClass, id);
221 }
222
223 public static boolean isRecursiveEnabled() {
224 return isRecursiveEnabled;
225 }
226
227 public static void setRecursiveEnabled(boolean ire) {
228 isRecursiveEnabled = ire;
229 }
230
231 }