Search in sources :

Example 96 with Element

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

the class EntityGroupImpl method primAddMember.

/**
     * Adds the <code>IGroupMember</code> key to the appropriate member key cache by copying the
     * cache, adding to the copy, and then replacing the original with the copy. At this point,
     * <code>gm</code> does not yet have <code>this</code> in its containing group cache. That cache
     * entry is not added until update(), when changes are committed to the store.
     *
     * @param gm org.apereo.portal.groups.IGroupMember
     */
private void primAddMember(IGroupMember gm) throws GroupsException {
    final EntityIdentifier cacheKey = getUnderlyingEntityIdentifier();
    Element element = childrenCache.get(cacheKey);
    @SuppressWarnings("unchecked") final Set<IGroupMember> set = element != null ? (Set<IGroupMember>) element.getObjectValue() : buildChildrenSet();
    final Set<IGroupMember> children = new HashSet<>(set);
    children.add(gm);
    childrenCache.put(new Element(cacheKey, children));
}
Also used : Element(net.sf.ehcache.Element) EntityIdentifier(org.apereo.portal.EntityIdentifier) HashSet(java.util.HashSet)

Example 97 with Element

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

the class PortletCacheControlServiceImpl method getCachedPortletData.

/**
     * Get the cached portlet data for the key, cache and window. If there is {@link
     * CachedPortletData} in the cache it will only be returned if {@link
     * CachedPortletData#isExpired()} is false or {@link CachedPortletData#getEtag()} is not null.
     *
     * @param cacheKey The cache key
     * @param outputCache The cache
     * @param portletWindow The portlet window the lookup is for
     * @return The cache data for the portlet window
     */
@SuppressWarnings("unchecked")
protected <T extends Serializable> CachedPortletResultHolder<T> getCachedPortletData(Serializable cacheKey, Ehcache outputCache, IPortletWindow portletWindow) {
    final Element publicCacheElement = outputCache.get(cacheKey);
    if (publicCacheElement == null) {
        logger.debug("No cached output for key {}", cacheKey);
        return null;
    }
    final CachedPortletResultHolder<T> cachedPortletData = (CachedPortletResultHolder<T>) publicCacheElement.getObjectValue();
    if (publicCacheElement.isExpired() && cachedPortletData.getEtag() == null) {
        logger.debug("Cached output for key {} is expired", cacheKey);
        outputCache.remove(cacheKey);
        return null;
    }
    logger.debug("Returning cached output with key {} for {}", cacheKey, portletWindow);
    return (CachedPortletResultHolder<T>) publicCacheElement.getObjectValue();
}
Also used : Element(net.sf.ehcache.Element)

Example 98 with Element

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

the class PortletCacheControlServiceImpl method cacheElement.

/**
     * Construct an appropriate Cache {@link Element} for the cacheKey and data. The element's ttl
     * will be set depending on whether expiration or validation method is indicated from the
     * CacheControl and the cache's configuration.
     */
protected void cacheElement(Ehcache cache, Serializable cacheKey, CachedPortletResultHolder<?> data, CacheControl cacheControl) {
    // using validation method, ignore expirationTime and defer to cache configuration
    if (cacheControl.getETag() != null) {
        final Element element = new Element(cacheKey, data);
        cache.put(element);
        return;
    }
    // using expiration method, -1 for CacheControl#expirationTime means "forever" (e.g. ignore and defer to cache configuration)
    final int expirationTime = cacheControl.getExpirationTime();
    if (expirationTime == -1) {
        final Element element = new Element(cacheKey, data);
        cache.put(element);
        return;
    }
    // using expiration method with a positive expiration, set that value as the element's TTL if it is lower than the configured cache TTL
    final CacheConfiguration cacheConfiguration = cache.getCacheConfiguration();
    final Element element = new Element(cacheKey, data);
    final long cacheTTL = cacheConfiguration.getTimeToLiveSeconds();
    if (expirationTime < cacheTTL) {
        element.setTimeToLive(expirationTime);
    }
    cache.put(element);
}
Also used : Element(net.sf.ehcache.Element) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration)

Example 99 with Element

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

the class MapCacheProvider method putAll.

/* (non-Javadoc)
     * @see java.util.Map#putAll(java.util.Map)
     */
public void putAll(Map<? extends K, ? extends V> t) {
    for (final Map.Entry<? extends K, ? extends V> e : t.entrySet()) {
        final K key = e.getKey();
        final V value = e.getValue();
        this.cache.put(new Element(key, value));
    }
}
Also used : Element(net.sf.ehcache.Element) Map(java.util.Map)

Example 100 with Element

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

the class CachingResourceLoaderImpl method getResource.

/* (non-Javadoc)
     * @see org.apereo.portal.utils.cache.CachingResourceLoader#getResource(org.springframework.core.io.Resource, org.apereo.portal.utils.cache.ResourceBuilder, org.apereo.portal.utils.cache.ResourceLoaderOptions)
     */
@Override
public <T> CachedResource<T> getResource(Resource resource, Loader<T> builder, long checkInterval) throws IOException {
    if (Included.PLAIN == this.resourcesElementsProvider.getDefaultIncludedType()) {
        this.logger.trace("Resoure Aggregation Disabled, ignoring resource cache and loading '" + resource + "' directly");
        return this.loadResource(resource, builder);
    }
    //Look for the resource in the cache, since it has been wrapped with a SelfPopulatingCache it should never return null.
    final GetResourceArguments<T> arguments = new GetResourceArguments<T>(resource, builder);
    final Element element = this.entryFactory.getWithData(this.resourceCache, resource, arguments);
    CachedResource<T> cachedResource = (CachedResource<T>) element.getObjectValue();
    if (this.logger.isTraceEnabled()) {
        this.logger.trace("Found " + cachedResource + " in cache");
    }
    //Found it, now check if the last-load time is within the check interval
    final long lastCheckTime = cachedResource.getLastCheckTime();
    if (lastCheckTime + checkInterval >= System.currentTimeMillis()) {
        if (this.logger.isTraceEnabled()) {
            this.logger.trace(cachedResource + " is within checkInterval " + checkInterval + ", returning");
        }
        return cachedResource;
    }
    if (this.logger.isTraceEnabled()) {
        this.logger.trace(cachedResource + " is older than checkInterval " + checkInterval + ", checking for modification");
    }
    //If the resource has not been modified return the cached resource.
    final boolean resourceModified = this.checkIfModified(cachedResource);
    if (!resourceModified) {
        cachedResource.setLastCheckTime(System.currentTimeMillis());
        this.resourceCache.put(//do a cache put to notify the cache the object has been modified
        element);
        return cachedResource;
    }
    //The resource has been modified, reload it.
    cachedResource = this.loadResource(resource, builder);
    //Cache the loaded resource
    this.resourceCache.put(new Element(resource, cachedResource));
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Loaded and cached " + cachedResource);
    }
    return cachedResource;
}
Also used : Element(net.sf.ehcache.Element)

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