Search in sources :

Example 91 with Element

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

the class StatisticsCacheManagerImpl method setLatestStatisticsTimeline.

@Override
public synchronized void setLatestStatisticsTimeline() {
    LOG.info("Getting the latest statistics timeline map");
    Map<StatisticsEnum, StatisticsTimeline> latestStatisticsTimelineMap = new HashMap<StatisticsEnum, StatisticsTimeline>();
    for (StatisticsEnum type : StatisticsEnum.values()) {
        StatisticsTimeline statisticsTimeline = statisticsManagerReadOnly.getStatisticsTimelineModel(type);
        latestStatisticsTimelineMap.put(type, statisticsTimeline);
    }
    if (statisticsCache.get(CACHE_TIMELINE_KEY) == null) {
        statisticsCache.put(new Element(CACHE_TIMELINE_KEY, latestStatisticsTimelineMap));
    } else {
        statisticsCache.replace(new Element(CACHE_TIMELINE_KEY, latestStatisticsTimelineMap));
    }
}
Also used : HashMap(java.util.HashMap) StatisticsEnum(org.orcid.core.utils.statistics.StatisticsEnum) Element(net.sf.ehcache.Element) StatisticsTimeline(org.orcid.jaxb.model.statistics.StatisticsTimeline)

Example 92 with Element

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

the class LayoutCachingService method cacheLayout.

@Override
public void cacheLayout(IPerson owner, IUserProfile profile, DistributedUserLayout layout) {
    final CacheKey cacheKey = this.getCacheKey(owner, profile);
    this.layoutCache.put(new Element(cacheKey, layout));
}
Also used : Element(net.sf.ehcache.Element) CacheKey(org.apereo.portal.utils.cache.CacheKey)

Example 93 with Element

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

the class CachingPipelineComponent method getEventReader.

/* (non-Javadoc)
     * @see org.apereo.portal.rendering.PipelineComponent#getEventReader(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
@SuppressWarnings("unchecked")
@Override
public final PipelineEventReader<R, E> getEventReader(HttpServletRequest request, HttpServletResponse response) {
    if (Included.PLAIN == this.resourcesElementsProvider.getDefaultIncludedType()) {
        this.logger.trace("{} - Resoure Aggregation Disabled, ignoring event cache and returning parent event reader directly", this.beanName);
        return this.wrappedComponent.getEventReader(request, response);
    }
    //Get the key for this request from the target component and see if there is a cache entry
    final CacheKey cacheKey = this.wrappedComponent.getCacheKey(request, response);
    Element element = this.cache.get(cacheKey);
    CachedEventReader<E> cachedEventReader = null;
    if (element != null) {
        cachedEventReader = (CachedEventReader<E>) element.getObjectValue();
    }
    //If there was a cached reader return it immediately
    if (cachedEventReader == null) {
        //No cached data for key, call target component to get events and an updated cache key
        logger.debug("{} - No cached events found for key {}, calling parent", this.beanName, cacheKey);
        final PipelineEventReader<R, E> pipelineEventReader = this.wrappedComponent.getEventReader(request, response);
        //Copy the events from the reader into a buffer to be cached
        final List<E> eventCache = new LinkedList<E>();
        for (final E event : pipelineEventReader) {
            //TODO add de-duplication logic here
            eventCache.add(event);
        }
        final Map<String, String> outputProperties = pipelineEventReader.getOutputProperties();
        cachedEventReader = new CachedEventReader<E>(eventCache, new LinkedHashMap<String, String>(outputProperties));
        //Cache the buffer
        element = new Element(cacheKey, cachedEventReader);
        this.cache.put(element);
        logger.debug("{} - Cached {} events for key {}", this.beanName, eventCache.size(), cacheKey);
    } else {
        logger.debug("{} - Found cached events for key {}", this.beanName, cacheKey);
    }
    final List<E> eventCache = cachedEventReader.getEventCache();
    final Map<String, String> outputProperties = cachedEventReader.getOutputProperties();
    final R eventReader = this.createEventReader(eventCache.listIterator());
    return new PipelineEventReaderImpl<R, E>(eventReader, outputProperties);
}
Also used : PipelineEventReaderImpl(org.apereo.portal.rendering.PipelineEventReaderImpl) Element(net.sf.ehcache.Element) CacheKey(org.apereo.portal.utils.cache.CacheKey) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap)

Example 94 with Element

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

the class GroupsCacheAuthenticationListenerTest method testUserAuthenticated.

@Test
public void testUserAuthenticated() {
    final IPerson person = PersonFactory.createPerson();
    person.setAttribute(IPerson.USERNAME, "mock.person");
    final IEntityGroup group = new MockEntityGroup("mock.group", IPerson.class);
    final CacheManager cacheManager = CacheManager.getInstance();
    final Cache parentGroupsCache = new Cache("parentGroupsCache", 100, false, false, 0, 0);
    cacheManager.addCache(parentGroupsCache);
    parentGroupsCache.put(new Element(person.getEntityIdentifier(), Collections.singleton(group)));
    final Cache childrenCache = new Cache("childrenCache", 100, false, false, 0, 0);
    cacheManager.addCache(childrenCache);
    childrenCache.put(new Element(group.getUnderlyingEntityIdentifier(), new Object()));
    Assert.assertEquals(parentGroupsCache.getSize(), 1);
    Assert.assertEquals(childrenCache.getSize(), 1);
    final LocalGroupsCacheAuthenticationListener listener = new LocalGroupsCacheAuthenticationListener();
    listener.setParentGroupsCache(parentGroupsCache);
    listener.setChildrenCache(childrenCache);
    listener.userAuthenticated(person);
    Assert.assertEquals(parentGroupsCache.getSize(), 0);
    Assert.assertEquals(childrenCache.getSize(), 0);
}
Also used : IPerson(org.apereo.portal.security.IPerson) Element(net.sf.ehcache.Element) CacheManager(net.sf.ehcache.CacheManager) Cache(net.sf.ehcache.Cache) Test(org.junit.Test)

Example 95 with Element

use of net.sf.ehcache.Element 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)

Aggregations

Element (net.sf.ehcache.Element)114 Test (org.junit.Test)21 CacheKey (org.apereo.portal.utils.cache.CacheKey)8 ArrayList (java.util.ArrayList)7 Date (java.util.Date)7 Cache (net.sf.ehcache.Cache)7 HashSet (java.util.HashSet)6 CacheException (net.sf.ehcache.CacheException)6 MalformedURLException (java.net.MalformedURLException)5 ConfigurationException (javax.naming.ConfigurationException)5 Ehcache (net.sf.ehcache.Ehcache)5 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)5 EntityIdentifier (org.apereo.portal.EntityIdentifier)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 URISyntaxException (java.net.URISyntaxException)4 SQLException (java.sql.SQLException)4 EntityExistsException (javax.persistence.EntityExistsException)4 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 BaseCacheTest (org.apache.camel.component.BaseCacheTest)4