Search in sources :

Example 1 with CacheKey

use of org.apereo.portal.utils.cache.CacheKey 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();
}
Also used : SimpleCacheEntryTag(org.apereo.portal.utils.cache.SimpleCacheEntryTag) Element(net.sf.ehcache.Element) Deque(java.util.Deque) CacheKey(org.apereo.portal.utils.cache.CacheKey)

Example 2 with CacheKey

use of org.apereo.portal.utils.cache.CacheKey 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));
}
Also used : SimpleCacheEntryTag(org.apereo.portal.utils.cache.SimpleCacheEntryTag) Element(net.sf.ehcache.Element) Deque(java.util.Deque) CacheKey(org.apereo.portal.utils.cache.CacheKey)

Example 3 with CacheKey

use of org.apereo.portal.utils.cache.CacheKey in project uPortal by Jasig.

the class JsonWrapperFilteringCharacterPipelineComponentTest method testGetCacheKeyNull.

@Test(expected = IllegalStateException.class)
public void testGetCacheKeyNull() {
    CacheKey cacheKey = jsonWrapperFilteringCharacterPipelineComponent.getCacheKey(req, res);
    Assert.assertNull(cacheKey);
}
Also used : CacheKey(org.apereo.portal.utils.cache.CacheKey) Test(org.junit.Test)

Example 4 with CacheKey

use of org.apereo.portal.utils.cache.CacheKey in project uPortal by Jasig.

the class JpaAggregatedGroupLookupDao method getGroupMapping.

@OpenEntityManager(unitName = BaseAggrEventsJpaDao.PERSISTENCE_UNIT_NAME)
@Override
public AggregatedGroupMapping getGroupMapping(final String groupService, final String groupName) {
    final CacheKey key = CacheKey.build(this.getClass().getName(), groupService, groupName);
    AggregatedGroupMapping groupMapping = this.entityManagerCache.get(BaseAggrEventsJpaDao.PERSISTENCE_UNIT_NAME, key);
    if (groupMapping != null) {
        return groupMapping;
    }
    final NaturalIdQuery<AggregatedGroupMappingImpl> query = this.createNaturalIdQuery(AggregatedGroupMappingImpl.class);
    query.using(AggregatedGroupMappingImpl_.groupService, groupService);
    query.using(AggregatedGroupMappingImpl_.groupName, groupName);
    groupMapping = query.load();
    if (groupMapping != null) {
        this.entityManagerCache.put(BaseAggrEventsJpaDao.PERSISTENCE_UNIT_NAME, key, groupMapping);
        return groupMapping;
    }
    return this.getTransactionOperations().execute(new TransactionCallback<AggregatedGroupMapping>() {

        @Override
        public AggregatedGroupMapping doInTransaction(TransactionStatus status) {
            final AggregatedGroupMappingImpl aggregatedGroupMapping = new AggregatedGroupMappingImpl(groupService, groupName);
            getEntityManager().persist(aggregatedGroupMapping);
            logger.debug("Created {}", aggregatedGroupMapping);
            entityManagerCache.put(BaseAggrEventsJpaDao.PERSISTENCE_UNIT_NAME, key, aggregatedGroupMapping);
            return aggregatedGroupMapping;
        }
    });
}
Also used : TransactionStatus(org.springframework.transaction.TransactionStatus) CacheKey(org.apereo.portal.utils.cache.CacheKey) OpenEntityManager(org.apereo.portal.jpa.OpenEntityManager)

Example 5 with CacheKey

use of org.apereo.portal.utils.cache.CacheKey in project uPortal by Jasig.

the class JpaEventSessionDao method getEventSession.

@AggrEventsTransactional
@Override
public EventSession getEventSession(PortalEvent event) {
    final String eventSessionId = event.getEventSessionId();
    final CacheKey key = CacheKey.build(EVENT_SESSION_CACHE_SOURCE, eventSessionId);
    EventSessionImpl eventSession = this.entityManagerCache.get(BaseAggrEventsJpaDao.PERSISTENCE_UNIT_NAME, key);
    if (eventSession != null) {
        return eventSession;
    }
    final NaturalIdQuery<EventSessionImpl> naturalIdQuery = this.createNaturalIdQuery(EventSessionImpl.class);
    naturalIdQuery.using(EventSessionImpl_.eventSessionId, eventSessionId);
    eventSession = naturalIdQuery.load();
    if (eventSession == null) {
        // No event session, somehow we missed the login event. Look at the groups the user is
        // currently a member of
        final Set<AggregatedGroupMapping> groupMappings = this.getGroupsForEvent(event);
        final DateTime eventDate = event.getTimestampAsDate();
        eventSession = new EventSessionImpl(eventSessionId, eventDate, groupMappings);
        this.getEntityManager().persist(eventSession);
        this.entityManagerCache.put(BaseAggrEventsJpaDao.PERSISTENCE_UNIT_NAME, key, eventSession);
    }
    return eventSession;
}
Also used : AggregatedGroupMapping(org.apereo.portal.events.aggr.groups.AggregatedGroupMapping) CacheKey(org.apereo.portal.utils.cache.CacheKey) DateTime(org.joda.time.DateTime)

Aggregations

CacheKey (org.apereo.portal.utils.cache.CacheKey)23 Element (net.sf.ehcache.Element)9 Test (org.junit.Test)6 Ehcache (net.sf.ehcache.Ehcache)4 ResourcesElementsProvider (org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)4 OpenEntityManager (org.apereo.portal.jpa.OpenEntityManager)3 PipelineEventReaderImpl (org.apereo.portal.rendering.PipelineEventReaderImpl)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3 Deque (java.util.Deque)2 XMLEventReader (javax.xml.stream.XMLEventReader)2 XMLEvent (javax.xml.stream.events.XMLEvent)2 CharacterEventReader (org.apereo.portal.character.stream.CharacterEventReader)2 CharacterEvent (org.apereo.portal.character.stream.events.CharacterEvent)2 CharacterPipelineComponent (org.apereo.portal.rendering.CharacterPipelineComponent)2 StAXPipelineComponent (org.apereo.portal.rendering.StAXPipelineComponent)2 SimpleCacheEntryTag (org.apereo.portal.utils.cache.SimpleCacheEntryTag)2 Serializable (java.io.Serializable)1 LinkedHashMap (java.util.LinkedHashMap)1