- extended DnaQuality details view
[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.UUID;
13
14 import org.springframework.stereotype.Component;
15
16 import net.sf.ehcache.Cache;
17 import net.sf.ehcache.Element;
18 import net.sf.ehcache.config.CacheConfiguration;
19 import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
20 import eu.etaxonomy.cdm.model.common.CdmBase;
21
22 /**
23 * @author cmathew
24 * @date 14 Oct 2014
25 *
26 */
27 @Component
28 public class CdmTransientEntityCacher<T extends CdmBase> extends CdmServiceCacher<T> {
29
30 private static final String ENTITY_CACHE_NAME = "entityCache";
31
32 public CdmTransientEntityCacher() {
33 // Create entity cache
34 getDefaultCacheManager().addCache(new Cache(getEntityCacheConfiguration()));
35 }
36 /**
37 * Returns the default cache configuration.
38 *
39 * @return
40 */
41 private CacheConfiguration getEntityCacheConfiguration() {
42 // For a better understanding on how to size caches, refer to
43 // http://ehcache.org/documentation/configuration/cache-size
44 return new CacheConfiguration(ENTITY_CACHE_NAME, 500)
45 .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
46 .eternal(false)
47 // default ttl and tti set to 2 hours
48 .timeToLiveSeconds(60*60*2)
49 .timeToIdleSeconds(60*60*2);
50 // This is 2.6.9 API
51 //.maxEntriesLocalDisk(1000);
52 //.persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP));
53 }
54
55 /**
56 * Returns the cache corresponding to the cache id
57 *
58 * @param cacheId
59 * @return
60 */
61 private static Cache getCache() {
62 return getDefaultCacheManager().getCache(ENTITY_CACHE_NAME);
63 }
64
65
66 /**
67 * Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
68 * in the cache corresponding to the given cache id
69 *
70 * @param cacheId
71 * @param uuid
72 * @param cdmEntity
73 */
74 public T put(String id, T cdmEntity) {
75 T cachedCdmEntity = getFromCache(cdmEntity.getUuid());
76 cachedCdmEntity = (cachedCdmEntity == null) ? getFromCache(id) : cachedCdmEntity;
77 if(cachedCdmEntity != null) {
78 return cachedCdmEntity;
79 } else {
80 getCache().put(new Element(id, cdmEntity));
81 return cdmEntity;
82 }
83 }
84
85 @Override
86 public T put(UUID uuid, T cdmEntity) {
87 T cachedCdmEntity = getFromCache(cdmEntity.getUuid());
88 cachedCdmEntity = (cachedCdmEntity == null) ? getFromCache(uuid) : cachedCdmEntity;
89 if(cachedCdmEntity != null) {
90 return cachedCdmEntity;
91 } else {
92 getCache().put(new Element(uuid, cdmEntity));
93 return cdmEntity;
94 }
95 }
96
97 public T put(Class<? extends CdmBase> clazz, int id, T cdmEntity) {
98 String cacheId = generateId(clazz,id);
99 return put(cacheId, cdmEntity);
100 }
101
102 /**
103 * Gets the cache element corresponding to the given {@link java.util.UUID}
104 * in the cache corresponding to the given cache id.
105 *
106 * @param uuid
107 * @return
108 */
109 @Override
110 protected Element getCacheElement(UUID uuid) {
111 return getCache().get(uuid);
112 }
113
114 private Element getCacheElement(String id) {
115 return getCache().get(id);
116 }
117
118
119 /* (non-Javadoc)
120 * @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID)
121 */
122 @Override
123 public T getFromCache(UUID uuid) {
124 Element e = getCacheElement(uuid);
125 e = (getCacheElement(uuid) == null) ? super.getCacheElement(uuid) : e;
126 if (e == null) {
127 return null;
128 } else {
129 return(T)e.getObjectValue();
130 }
131 }
132
133 public T getFromCache(String id) {
134 Element e = getCacheElement(id);
135 if (e == null) {
136 return null;
137 } else {
138 return (T) e.getObjectValue();
139 }
140 }
141
142
143 public T getFromCache(Class<? extends CdmBase> clazz, int id) {
144 String cacheId = generateId(clazz,id);
145 return getFromCache(cacheId);
146 }
147
148
149 /* (non-Javadoc)
150 * @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
151 */
152 @Override
153 public boolean exists(UUID uuid) {
154 return (getCacheElement(uuid) != null || super.getCacheElement(uuid) != null);
155 }
156
157 public boolean exists(String id) {
158 return (getCacheElement(id) != null);
159 }
160
161
162 /* (non-Javadoc)
163 * @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
164 */
165 @Override
166 public boolean existsAndIsNotNull(UUID uuid) {
167 return getFromCache(uuid) != null;
168 }
169
170 public boolean existsAndIsNotNull(String id) {
171 return getFromCache(id) != null;
172 }
173
174
175 private static String generateId(Class<? extends CdmBase> clazz, int id) {
176 return clazz.getName() + String.valueOf(id);
177 }
178
179 }