use of net.sf.ehcache.Ehcache in project uPortal by Jasig.
the class CachingResourceLoaderImplTest method testUncachedLoad.
@Test
public void testUncachedLoad() throws Exception {
final Resource doc1Resouce = new FileSystemResource(doc1);
final CachingResourceLoaderImpl loader = new CachingResourceLoaderImpl();
final Ehcache cache = createMock(Ehcache.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(null);
expect(cache.getQuiet(doc1Resouce)).andReturn(null);
cache.put(anyObject(Element.class));
expectLastCall();
replay(cache, elementsProvider);
loader.setResourceCache(cache);
loader.setResourcesElementsProvider(elementsProvider);
final CachedResource<String> cachedResource1 = loader.getResource(doc1Resouce, StringResourceBuilder.INSTANCE);
verify(cache, elementsProvider);
assertNotNull(cachedResource1);
final String expected = IOUtils.toString(new FileReader(doc1));
assertEquals(expected, cachedResource1.getCachedResource());
}
use of net.sf.ehcache.Ehcache in project uPortal by Jasig.
the class CachingStAXPipelineComponentTest method testCacheHit.
@Test
public void testCacheHit() {
final MockHttpServletRequest mockReq = new MockHttpServletRequest();
final MockHttpServletResponse mockRes = new MockHttpServletResponse();
final CacheKey cacheKey = CacheKey.build("testCacheKey");
final CachedEventReader<XMLEvent> eventReader = new CachedEventReader<XMLEvent>(Collections.EMPTY_LIST, Collections.EMPTY_MAP);
final Element cacheElement = new Element(cacheKey, eventReader);
final Ehcache cache = createMock(Ehcache.class);
final StAXPipelineComponent targetComponent = createMock(StAXPipelineComponent.class);
final ResourcesElementsProvider elementsProvider = createMock(ResourcesElementsProvider.class);
expect(elementsProvider.getDefaultIncludedType()).andReturn(Included.AGGREGATED);
expect(targetComponent.getCacheKey(mockReq, mockRes)).andReturn(cacheKey);
expect(cache.get(cacheKey)).andReturn(cacheElement);
replay(cache, targetComponent, elementsProvider);
final CachingStAXPipelineComponent cachingComponent = new CachingStAXPipelineComponent();
cachingComponent.setCache(cache);
cachingComponent.setWrappedComponent(targetComponent);
cachingComponent.setResourcesElementsProvider(elementsProvider);
final PipelineEventReader<XMLEventReader, XMLEvent> actualEventReader = cachingComponent.getEventReader(mockReq, mockRes);
Assert.assertNotNull(actualEventReader);
Assert.assertNotNull(actualEventReader.getEventReader());
Assert.assertFalse(actualEventReader.getEventReader().hasNext());
verify(cache, targetComponent, elementsProvider);
}
use of net.sf.ehcache.Ehcache in project uPortal by Jasig.
the class EhcacheManagerBeanConfigurer method postProcessBeanFactory.
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
final String[] cacheNames = this.cacheManager.getCacheNames();
for (final String cacheName : cacheNames) {
final Ehcache ehcache = this.cacheManager.getEhcache(cacheName);
this.logger.debug("Registering Ehcache '" + cacheName + "' with bean factory");
if (beanFactory.containsBean(cacheName)) {
if (skipDuplicates) {
continue;
}
throw new BeanCreationException("Duplicate Ehcache " + cacheName + " from CacheManager " + cacheManager.getName());
}
try {
beanFactory.registerSingleton(cacheName, ehcache);
} catch (Exception e) {
throw new BeanCreationException("Failed to register Ehcache " + cacheName + " from CacheManager " + cacheManager.getName(), e);
}
}
this.logger.debug("Registered " + cacheNames.length + " Ehcaches with bean factory");
}
use of net.sf.ehcache.Ehcache in project uPortal by Jasig.
the class TagTrackingCacheEventListener method purgeCacheEntries.
/** Remove all cache entries with keys that have the specified tag */
@Override
public int purgeCacheEntries(CacheEntryTag tag) {
final String tagType = tag.getTagType();
final Set<Ehcache> caches = taggedCaches.getIfPresent(tagType);
//Tag exists in cache(s)
if (caches == null || caches.isEmpty()) {
return 0;
}
int purgeCount = 0;
//Iterate over each cache to remove the tagged entries
for (final Ehcache cache : caches) {
final String cacheName = cache.getName();
//See if there are any tagged cache keys for the cache
final LoadingCache<CacheEntryTag, Set<Object>> cacheKeys = taggedCacheKeys.getIfPresent(cacheName);
if (cacheKeys != null) {
//Remove all cache keys from the cache
final Set<Object> taggedKeys = cacheKeys.asMap().remove(tag);
if (taggedKeys != null) {
final int keyCount = taggedKeys.size();
purgeCount += keyCount;
logger.debug("Removing {} keys from {} for tag {}", keyCount, cacheName, tag);
cache.removeAll(taggedKeys);
}
}
}
return purgeCount;
}
Aggregations