3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.api
.cache
;
12 import java
.util
.UUID
;
14 import org
.springframework
.stereotype
.Component
;
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
;
28 public class CdmTransientEntityCacher
<T
extends CdmBase
> extends CdmServiceCacher
<T
> {
30 private static final String ENTITY_CACHE_NAME
= "entityCache";
32 public CdmTransientEntityCacher() {
33 // Create entity cache
34 getDefaultCacheManager().addCache(new Cache(getEntityCacheConfiguration()));
37 * Returns the default cache configuration.
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
)
47 // default ttl and tti set to 2 hours
48 .timeToLiveSeconds(60*60*2)
49 .timeToIdleSeconds(60*60*2);
51 //.maxEntriesLocalDisk(1000);
52 //.persistence(new PersistenceConfiguration().strategy(Strategy.LOCALTEMPSWAP));
56 * Returns the cache corresponding to the cache id
61 private static Cache
getCache() {
62 return getDefaultCacheManager().getCache(ENTITY_CACHE_NAME
);
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
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
;
80 getCache().put(new Element(id
, cdmEntity
));
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
;
92 getCache().put(new Element(uuid
, cdmEntity
));
97 public T
put(Class
<?
extends CdmBase
> clazz
, int id
, T cdmEntity
) {
98 String cacheId
= generateId(clazz
,id
);
99 return put(cacheId
, cdmEntity
);
103 * Gets the cache element corresponding to the given {@link java.util.UUID}
104 * in the cache corresponding to the given cache id.
110 protected Element
getCacheElement(UUID uuid
) {
111 return getCache().get(uuid
);
114 private Element
getCacheElement(String id
) {
115 return getCache().get(id
);
120 * @see eu.etaxonomy.cdm.model.ICdmCacher#getFromCache(java.util.UUID)
123 public T
getFromCache(UUID uuid
) {
124 Element e
= getCacheElement(uuid
);
125 e
= (getCacheElement(uuid
) == null) ?
super.getCacheElement(uuid
) : e
;
129 return(T
)e
.getObjectValue();
133 public T
getFromCache(String id
) {
134 Element e
= getCacheElement(id
);
138 return (T
) e
.getObjectValue();
143 public T
getFromCache(Class
<?
extends CdmBase
> clazz
, int id
) {
144 String cacheId
= generateId(clazz
,id
);
145 return getFromCache(cacheId
);
150 * @see eu.etaxonomy.cdm.model.ICdmCacher#exists(java.util.UUID)
153 public boolean exists(UUID uuid
) {
154 return (getCacheElement(uuid
) != null || super.getCacheElement(uuid
) != null);
157 public boolean exists(String id
) {
158 return (getCacheElement(id
) != null);
163 * @see eu.etaxonomy.cdm.model.ICdmCacher#existsAndIsNotNull(java.util.UUID)
166 public boolean existsAndIsNotNull(UUID uuid
) {
167 return getFromCache(uuid
) != null;
170 public boolean existsAndIsNotNull(String id
) {
171 return getFromCache(id
) != null;
175 private static String
generateId(Class
<?
extends CdmBase
> clazz
, int id
) {
176 return clazz
.getName() + String
.valueOf(id
);