--- /dev/null
+// $Id$
+/**
+* Copyright (C) 2014 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.cdm.api.cache;
+
+import java.util.UUID;
+
+import org.springframework.stereotype.Component;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+import net.sf.ehcache.config.CacheConfiguration;
+import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
+import eu.etaxonomy.cdm.model.common.CdmBase;
+
+/**
+ * @author cmathew
+ * @date 14 Oct 2014
+ *
+ */
+@Component
+public class CdmTransientEntityCacher<T extends CdmBase> extends CdmServiceCacher<T> {
+
+ private static final String ENTITY_CACHE_NAME = "entityCache";
+
+ public CdmTransientEntityCacher() {
+ // Create entity cache
+ getDefaultCacheManager().addCache(new Cache(getEntityCacheConfiguration()));
+ }
+ /**
+ * Returns the default cache configuration.
+ *
+ * @return
+ */
+ private CacheConfiguration getEntityCacheConfiguration() {
+ // For a better understanding on how to size caches, refer to
+ // http://ehcache.org/documentation/configuration/cache-size
+ return new CacheConfiguration(ENTITY_CACHE_NAME, 500)
+ .memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
+ .eternal(false)
+ // default ttl and tti set to 2 hours
+ .timeToLiveSeconds(60*60*2)
+ .timeToIdleSeconds(60*60*2);
+ // This is 2.6.9 API
+ //.maxEntriesLocalDisk(1000);
+ //.persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP));
+ }
+
+ /**
+ * Returns the cache corresponding to the cache id
+ *
+ * @param cacheId
+ * @return
+ */
+ private static Cache getCache() {
+ return getDefaultCacheManager().getCache(ENTITY_CACHE_NAME);
+ }
+
+
+ /**
+ * Puts the (Key,Value) pair of ({@link java.util.UUID}, {@link eu.etaxonomy.cdm.model.common.CdmBase}),
+ * in the cache corresponding to the given cache id
+ *
+ * @param cacheId
+ * @param uuid
+ * @param cdmEntity
+ */
+ public T put(String id, T cdmEntity) {
+ T cachedCdmEntity = getFromCache(cdmEntity.getUuid());
+ cachedCdmEntity = (cachedCdmEntity == null) ? getFromCache(id) : cachedCdmEntity;
+ if(cachedCdmEntity != null) {
+ return cachedCdmEntity;
+ } else {
+ getCache().put(new Element(id, cdmEntity));
+ return cdmEntity;
+ }
+ }
+
+ @Override
+ public T put(UUID uuid, T cdmEntity) {
+ T cachedCdmEntity = getFromCache(cdmEntity.getUuid());
+ cachedCdmEntity = (cachedCdmEntity == null) ? getFromCache(uuid) : cachedCdmEntity;
+ if(cachedCdmEntity != null) {
+ return cachedCdmEntity;
+ } else {
+ getCache().put(new Element(uuid, cdmEntity));
+ return cdmEntity;
+ }
+ }
+
+ public T put(Class<? extends CdmBase> clazz, int id, T cdmEntity) {
+ String cacheId = generateId(clazz,id);
+ return put(cacheId, cdmEntity);
+ }
+
+ /**
+ * Gets the cache element corresponding to the given {@link java.util.UUID}
+ * in the cache corresponding to the given cache id.
+ *
+ * @param uuid
+ * @return
+ */
+ @Override
+ protected Element getCacheElement(UUID uuid) {
+ return getCache().get(uuid);
+ }
+
+ private Element getCacheElement(String id) {
+ return getCache().get(id);
+ }
+
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID)
+ */
+ @Override
+ public T getFromCache(UUID uuid) {
+ Element e = getCacheElement(uuid);
+ e = (getCacheElement(uuid) == null) ? super.getCacheElement(uuid) : e;
+ if (e == null) {
+ return null;
+ } else {
+ return(T)e.getObjectValue();
+ }
+ }
+
+ public T getFromCache(String id) {
+ Element e = getCacheElement(id);
+ if (e == null) {
+ return null;
+ } else {
+ return (T) e.getObjectValue();
+ }
+ }
+
+
+ public T getFromCache(Class<? extends CdmBase> clazz, int id) {
+ String cacheId = generateId(clazz,id);
+ return getFromCache(cacheId);
+ }
+
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
+ */
+ @Override
+ public boolean exists(UUID uuid) {
+ return (getCacheElement(uuid) != null || super.getCacheElement(uuid) != null);
+ }
+
+ public boolean exists(String id) {
+ return (getCacheElement(id) != null);
+ }
+
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
+ */
+ @Override
+ public boolean existsAndIsNotNull(UUID uuid) {
+ return getFromCache(uuid) != null;
+ }
+
+ public boolean existsAndIsNotNull(String id) {
+ return getFromCache(id) != null;
+ }
+
+
+ private static String generateId(Class<? extends CdmBase> clazz, int id) {
+ return clazz.getName() + String.valueOf(id);
+ }
+
+}