use of org.apereo.portal.utils.cache.SimpleCacheEntryTag in project uPortal by Jasig.
the class EntityManagerCacheImpl method get.
@Override
public <T> T get(String persistenceUnitName, Serializable key) {
final Map<String, Deque<String>> currentEntityManagers = CURRENT_ENTITY_MANAGER_SESSIONS.get();
if (currentEntityManagers == null) {
logger.error("There is no currentEntityManagers Map in the ThreadLocal, no EntityManager scoped caching will be done. persistenceUnitName=" + persistenceUnitName + ", key=" + key, new Throwable());
return null;
}
final Deque<String> entityManagerIds = currentEntityManagers.get(persistenceUnitName);
if (entityManagerIds == null || entityManagerIds.isEmpty()) {
logger.error("Cannot access cache for persistent unit " + persistenceUnitName + ", it has no active EntityManager, no EntityManager scoped caching will be done. key=" + key, new Throwable());
return null;
}
final String entityManagerId = entityManagerIds.getFirst();
final SimpleCacheEntryTag entityManagerIdTag = new SimpleCacheEntryTag(CACHE_TAG, entityManagerId);
final CacheKey cacheKey = CacheKey.buildTagged(CACHE_KEY_SOURCE, entityManagerIdTag, entityManagerId, key);
final Element element = this.contentCache.get(cacheKey);
if (element == null) {
return null;
}
return (T) element.getObjectValue();
}
use of org.apereo.portal.utils.cache.SimpleCacheEntryTag in project uPortal by Jasig.
the class EntityManagerCacheImpl method put.
@Override
public void put(String persistenceUnitName, Serializable key, Object value) {
final Map<String, Deque<String>> currentEntityManagers = CURRENT_ENTITY_MANAGER_SESSIONS.get();
if (currentEntityManagers == null) {
logger.error("There is no currentEntityManagers Map in the ThreadLocal, no EntityManager scoped caching will be done. persistenceUnitName=" + persistenceUnitName + ", key=" + key, new Throwable());
return;
}
final Deque<String> entityManagerIds = currentEntityManagers.get(persistenceUnitName);
if (entityManagerIds == null || entityManagerIds.isEmpty()) {
logger.error("Cannot access cache for persistent unit " + persistenceUnitName + ", it has no active EntityManager, no EntityManager scoped caching will be done. key=" + key, new Throwable());
return;
}
final String entityManagerId = entityManagerIds.getFirst();
final SimpleCacheEntryTag entityManagerIdTag = new SimpleCacheEntryTag(CACHE_TAG, entityManagerId);
final CacheKey cacheKey = CacheKey.buildTagged(CACHE_KEY_SOURCE, entityManagerIdTag, entityManagerId, key);
this.contentCache.put(new Element(cacheKey, value));
}
use of org.apereo.portal.utils.cache.SimpleCacheEntryTag in project uPortal by Jasig.
the class EntityManagerCacheImpl method onApplicationEvent.
@Override
public void onApplicationEvent(AbstractEntityManagerEvent event) {
final String persistenceUnitName = event.getPersistenceUnitName();
final AtomicInteger counter = OPEN_EM_COUNTER.getUnchecked(persistenceUnitName);
final String entityManagerId = event.getEntityManagerId();
if (event instanceof EntityManagerCreatedEvent) {
final int count = counter.incrementAndGet();
if (logger.isTraceEnabled()) {
logger.trace("CREATE " + count + " - " + entityManagerId + " " + Thread.currentThread().getName());
}
// Get/Create the currentEntityManagers Map
Map<String, Deque<String>> currentEntityManagers = CURRENT_ENTITY_MANAGER_SESSIONS.get();
if (currentEntityManagers == null) {
currentEntityManagers = new HashMap<String, Deque<String>>();
CURRENT_ENTITY_MANAGER_SESSIONS.set(currentEntityManagers);
}
// Get/Create the Deque of current entityManagerIds
Deque<String> entityManagerIds = currentEntityManagers.get(persistenceUnitName);
if (entityManagerIds == null) {
entityManagerIds = new LinkedList<String>();
currentEntityManagers.put(persistenceUnitName, entityManagerIds);
}
// Set the current EMiD for this PU
entityManagerIds.offerFirst(entityManagerId);
} else if (event instanceof EntityManagerClosingEvent) {
final int count = counter.decrementAndGet();
if (logger.isTraceEnabled()) {
logger.trace("CLOSE " + count + " - " + entityManagerId + " " + Thread.currentThread().getName());
}
// Purge any cached data related to this entity manager
taggedCacheEntryPurger.purgeCacheEntries(new SimpleCacheEntryTag(CACHE_TAG, entityManagerId));
// Get the currentEntityManagers Map
final Map<String, Deque<String>> currentEntityManagers = CURRENT_ENTITY_MANAGER_SESSIONS.get();
if (currentEntityManagers == null || currentEntityManagers.isEmpty()) {
logger.error("Closing " + entityManagerId + " but there is no currentEntityManagers Map for this Thread", new Throwable());
}
// Get the Deque of current entityManagerIds
final Deque<String> entityManagerIds = currentEntityManagers.get(persistenceUnitName);
if (entityManagerIds == null || entityManagerIds.isEmpty()) {
logger.error("Closing " + entityManagerId + " but there is no entityManagerIds Deque for this Thread", new Throwable());
}
// Get the current EMiD for this PU
final String currentEntityManagerId = entityManagerIds.getFirst();
if (!currentEntityManagerId.equals(entityManagerId)) {
logger.error("Closing " + entityManagerId + " but the current EntityManagerId is " + currentEntityManagerId, new Throwable());
}
// Remove the current EMiD
entityManagerIds.removeFirst();
// If nothing else is tracked for this PU remove the deque
if (entityManagerIds.isEmpty()) {
currentEntityManagers.remove(persistenceUnitName);
}
// If nothing else is tracked for this thread remove the local
if (currentEntityManagers.isEmpty()) {
CURRENT_ENTITY_MANAGER_SESSIONS.remove();
}
}
}
Aggregations