Search in sources :

Example 16 with Ehcache

use of net.sf.ehcache.Ehcache in project ORCID-Source by ORCID.

the class OrcidEhCacheFactoryBean method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    Ehcache existingCache = cacheManager.getEhcache(cacheName);
    String diskStorePath = cacheManager.getConfiguration().getDiskStoreConfiguration().getPath();
    LOGGER.debug("Cache manager disk store path = " + diskStorePath);
    if (existingCache == null) {
        CacheConfiguration config = createConfig();
        if (cacheEntryFactory != null) {
            this.cache = new SelfPopulatingCache(new Cache(config), cacheEntryFactory);
        } else {
            this.cache = new Cache(config);
        }
        cacheManager.addCache(this.cache);
    } else {
        this.cache = existingCache;
    }
}
Also used : SelfPopulatingCache(net.sf.ehcache.constructs.blocking.SelfPopulatingCache) Ehcache(net.sf.ehcache.Ehcache) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) SelfPopulatingCache(net.sf.ehcache.constructs.blocking.SelfPopulatingCache) Cache(net.sf.ehcache.Cache)

Example 17 with Ehcache

use of net.sf.ehcache.Ehcache in project uPortal by Jasig.

the class ExpiredElementEvictor method evictExpiredElements.

/** Evicts expired elements from the {@link CacheManager} */
public void evictExpiredElements() {
    long evictedTotal = 0;
    final long startTime;
    final String[] cacheNames;
    if (!evictLock.tryLock()) {
        //Lock is already held, skip eviction
        return;
    }
    try {
        startTime = System.nanoTime();
        cacheNames = this.cacheManager.getCacheNames();
        for (String cacheName : cacheNames) {
            final Ehcache cache = this.cacheManager.getEhcache(cacheName);
            if (null != cache) {
                final long preEvictSize = cache.getMemoryStoreSize();
                final long evictStart = System.nanoTime();
                cache.evictExpiredElements();
                if (logger.isDebugEnabled()) {
                    final long evicted = preEvictSize - cache.getMemoryStoreSize();
                    evictedTotal += evicted;
                    logger.debug("Evicted " + evicted + " elements from cache '" + cacheName + "' in " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - evictStart) + " ms");
                }
            } else if (logger.isDebugEnabled()) {
                logger.debug("No cache found with name " + cacheName);
            }
        }
    } finally {
        this.evictLock.unlock();
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Evicted " + evictedTotal + " elements from " + cacheNames.length + " caches in " + this.cacheManager.getName() + " in " + TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) + " ms");
    }
}
Also used : Ehcache(net.sf.ehcache.Ehcache)

Example 18 with Ehcache

use of net.sf.ehcache.Ehcache 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());
}
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 19 with Ehcache

use of net.sf.ehcache.Ehcache in project uPortal by Jasig.

the class CachingResourceLoaderImplTest method testUncachedLoadNoDigest.

@Test
public void testUncachedLoadNoDigest() 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 20 with Ehcache

use of net.sf.ehcache.Ehcache 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);
}
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) FileSystemResource(org.springframework.core.io.FileSystemResource) CachingResourceLoaderImpl(org.apereo.portal.utils.cache.resource.CachingResourceLoaderImpl) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Test(org.junit.Test)

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