Search in sources :

Example 36 with Ehcache

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());
}
Also used : ResourcesElementsProvider(org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider) Element(net.sf.ehcache.Element) LoadedResource(org.apereo.portal.utils.cache.resource.LoadedResource) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) CachedResource(org.apereo.portal.utils.cache.resource.CachedResource) Ehcache(net.sf.ehcache.Ehcache) FileReader(java.io.FileReader) FileSystemResource(org.springframework.core.io.FileSystemResource) CachingResourceLoaderImpl(org.apereo.portal.utils.cache.resource.CachingResourceLoaderImpl) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Test(org.junit.Test)

Example 37 with Ehcache

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);
}
Also used : StAXPipelineComponent(org.apereo.portal.rendering.StAXPipelineComponent) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Element(net.sf.ehcache.Element) ResourcesElementsProvider(org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider) XMLEvent(javax.xml.stream.events.XMLEvent) Ehcache(net.sf.ehcache.Ehcache) XMLEventReader(javax.xml.stream.XMLEventReader) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) CacheKey(org.apereo.portal.utils.cache.CacheKey) Test(org.junit.Test)

Example 38 with Ehcache

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");
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) Ehcache(net.sf.ehcache.Ehcache) BeanCreationException(org.springframework.beans.factory.BeanCreationException) BeansException(org.springframework.beans.BeansException)

Example 39 with Ehcache

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;
}
Also used : Set(java.util.Set) Ehcache(net.sf.ehcache.Ehcache)

Aggregations

Ehcache (net.sf.ehcache.Ehcache)39 Test (org.junit.Test)14 ResourcesElementsProvider (org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider)9 Element (net.sf.ehcache.Element)8 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)7 CacheException (net.sf.ehcache.CacheException)5 CacheManager (net.sf.ehcache.CacheManager)5 CachedResource (org.apereo.portal.utils.cache.resource.CachedResource)5 CachingResourceLoaderImpl (org.apereo.portal.utils.cache.resource.CachingResourceLoaderImpl)5 LoadedResource (org.apereo.portal.utils.cache.resource.LoadedResource)5 FileSystemResource (org.springframework.core.io.FileSystemResource)5 Resource (org.springframework.core.io.Resource)5 CacheKey (org.apereo.portal.utils.cache.CacheKey)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 Test (org.testng.annotations.Test)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 FileReader (java.io.FileReader)3 InputStream (java.io.InputStream)3