use of net.sf.ehcache.Element 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 net.sf.ehcache.Element 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 net.sf.ehcache.Element in project uPortal by Jasig.
the class CachingResourceLoaderImplTest method testCachedModifiedLoad.
@Test
public void testCachedModifiedLoad() throws Exception {
final Resource doc1Resouce = new FileSystemResource(doc1);
final CachingResourceLoaderImpl loader = new CachingResourceLoaderImpl();
final Ehcache cache = createMock(Ehcache.class);
final CachedResource<?> cachedResource = createMock(CachedResource.class);
final ResourcesElementsProvider elementsProvider = createMock(ResourcesElementsProvider.class);
expect(elementsProvider.getDefaultIncludedType()).andReturn(Included.AGGREGATED);
expect(cache.getInternalContext()).andReturn(null).anyTimes();
expect(cache.getCacheConfiguration()).andReturn(new CacheConfiguration());
expect(cache.get(doc1Resouce)).andReturn(new Element(doc1Resouce, cachedResource));
final long lastModified = doc1.lastModified();
expect(cachedResource.getResource()).andReturn(doc1Resouce);
expect(cachedResource.getLastCheckTime()).andReturn(lastModified - TimeUnit.MINUTES.toMillis(5));
expect(cachedResource.getLastLoadTime()).andReturn(lastModified - TimeUnit.MINUTES.toMillis(5));
cache.put(anyObject(Element.class));
expectLastCall();
replay(cache, cachedResource, elementsProvider);
loader.setResourceCache(cache);
loader.setResourcesElementsProvider(elementsProvider);
final CachedResource<String> cachedResource1 = loader.getResource(doc1Resouce, StringResourceBuilder.INSTANCE);
verify(cache, cachedResource, elementsProvider);
assertNotNull(cachedResource1);
final String expected = IOUtils.toString(new FileReader(doc1));
assertEquals(expected, cachedResource1.getCachedResource());
}
use of net.sf.ehcache.Element in project uPortal by Jasig.
the class CachingResourceLoaderImplTest method testCachedNotModified.
@Test
public void testCachedNotModified() throws Exception {
final Resource doc1Resouce = new FileSystemResource(doc1);
final CachingResourceLoaderImpl loader = new CachingResourceLoaderImpl();
final Ehcache cache = createMock(Ehcache.class);
final CachedResource<?> cachedResource = createMock(CachedResource.class);
final ResourcesElementsProvider elementsProvider = createMock(ResourcesElementsProvider.class);
expect(elementsProvider.getDefaultIncludedType()).andReturn(Included.AGGREGATED);
expect(cache.getInternalContext()).andReturn(null).anyTimes();
expect(cache.getCacheConfiguration()).andReturn(new CacheConfiguration());
final Element element = new Element("class path resource [CachingResourceLoaderImplTest_doc1.txt]", cachedResource);
expect(cache.get(doc1Resouce)).andReturn(element);
final long lastModified = doc1.lastModified();
expect(cachedResource.getResource()).andReturn(doc1Resouce);
expect(cachedResource.getLastCheckTime()).andReturn(0L);
expect(cachedResource.getLastLoadTime()).andReturn(lastModified + TimeUnit.MINUTES.toMillis(5));
expect(cachedResource.getAdditionalResources()).andReturn(Collections.EMPTY_MAP);
cachedResource.setLastCheckTime(anyLong());
cache.put(element);
expectLastCall();
replay(cache, cachedResource, elementsProvider);
loader.setResourceCache(cache);
loader.setResourcesElementsProvider(elementsProvider);
final CachedResource<String> cachedResource1 = loader.getResource(doc1Resouce, StringResourceBuilder.INSTANCE);
verify(cache, cachedResource, elementsProvider);
assertNotNull(cachedResource1);
assertTrue(cachedResource1 == cachedResource);
}
use of net.sf.ehcache.Element in project uPortal by Jasig.
the class CachingResourceLoaderImplTest method testCachedWithinInterval.
@Test
public void testCachedWithinInterval() throws Exception {
final Resource doc1Resouce = new FileSystemResource(doc1);
final CachingResourceLoaderImpl loader = new CachingResourceLoaderImpl();
final Ehcache cache = createMock(Ehcache.class);
final CachedResource<?> cachedResource = createMock(CachedResource.class);
final ResourcesElementsProvider elementsProvider = createMock(ResourcesElementsProvider.class);
expect(elementsProvider.getDefaultIncludedType()).andReturn(Included.AGGREGATED);
expect(cache.getInternalContext()).andReturn(null).anyTimes();
expect(cache.getCacheConfiguration()).andReturn(new CacheConfiguration());
expect(cache.get(doc1Resouce)).andReturn(new Element(doc1Resouce, cachedResource));
expect(cachedResource.getLastCheckTime()).andReturn(System.currentTimeMillis());
replay(cache, cachedResource, elementsProvider);
loader.setResourceCache(cache);
loader.setResourcesElementsProvider(elementsProvider);
final CachedResource<String> cachedResource1 = loader.getResource(doc1Resouce, StringResourceBuilder.INSTANCE);
verify(cache, cachedResource, elementsProvider);
assertNotNull(cachedResource1);
assertTrue(cachedResource1 == cachedResource);
}
Aggregations