Search in sources :

Example 41 with Ehcache

use of net.sf.ehcache.Ehcache in project camel by apache.

the class CacheValidate method isValid.

public boolean isValid(CacheManager cacheManager, String cacheName, String key) {
    LOG.trace("Cache Name: {}", cacheName);
    if (!cacheManager.cacheExists(cacheName)) {
        LOG.debug("No existing Cache found with name: {}" + ". Please ensure a cache is first instantiated using a Cache Consumer or Cache Producer." + " Replacement will not be performed since the cache {} does not presently exist", cacheName, cacheName);
        return false;
    }
    LOG.debug("Found an existing cache: {}", cacheName);
    if (LOG.isTraceEnabled()) {
        LOG.trace("Cache {} currently contains {} elements", cacheName, cacheManager.getCache(cacheName).getSize());
    }
    Ehcache cache = cacheManager.getCache(cacheName);
    if (!cache.isKeyInCache(key)) {
        LOG.debug("No Key with name: {} presently exists in the cache. It is also possible that the key may have expired in the cache." + " Replacement will not be performed until an appropriate key/value pair is added to (or) found in the cache.", key);
        return false;
    }
    return true;
}
Also used : Ehcache(net.sf.ehcache.Ehcache)

Example 42 with Ehcache

use of net.sf.ehcache.Ehcache in project camel by apache.

the class CacheEndpoint method initializeCache.

/**
     * Returns {@link Cache} instance or create new one if not exists.
     * 
     * @return {@link Cache}
     */
public Ehcache initializeCache() {
    CacheManager cacheManager = getCacheManagerFactory().getInstance();
    Ehcache cache;
    if (cacheManager.cacheExists(config.getCacheName())) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Found an existing cache: {}", config.getCacheName());
            LOG.trace("Cache {} currently contains {} elements", config.getCacheName(), cacheManager.getEhcache(config.getCacheName()).getSize());
        }
        cache = cacheManager.getEhcache(config.getCacheName());
    } else {
        cache = new Cache(config.getCacheName(), config.getMaxElementsInMemory(), config.getMemoryStoreEvictionPolicy(), config.isOverflowToDisk(), config.getDiskStorePath(), config.isEternal(), config.getTimeToLiveSeconds(), config.getTimeToIdleSeconds(), config.isDiskPersistent(), config.getDiskExpiryThreadIntervalSeconds(), null);
        for (CacheEventListener listener : config.getEventListenerRegistry().getEventListeners()) {
            cache.getCacheEventNotificationService().registerListener(listener);
        }
        for (CacheLoaderWrapper loader : config.getCacheLoaderRegistry().getCacheLoaders()) {
            loader.init(cache);
            cache.registerCacheLoader(loader);
        }
        cacheManager.addCache(cache);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Added a new cache: " + cache.getName());
        }
    }
    return cache;
}
Also used : CacheEventListener(net.sf.ehcache.event.CacheEventListener) CacheManager(net.sf.ehcache.CacheManager) Ehcache(net.sf.ehcache.Ehcache) Cache(net.sf.ehcache.Cache)

Example 43 with Ehcache

use of net.sf.ehcache.Ehcache in project cas by apereo.

the class EhcacheTicketRegistryConfiguration method ticketRegistry.

@Autowired
@RefreshScope
@Bean
public TicketRegistry ticketRegistry(@Qualifier("ehcacheTicketCacheManager") final CacheManager manager, @Qualifier("ticketCatalog") final TicketCatalog ticketCatalog) {
    final EncryptionRandomizedSigningJwtCryptographyProperties crypto = casProperties.getTicket().getRegistry().getEhcache().getCrypto();
    final Collection<TicketDefinition> definitions = ticketCatalog.findAll();
    definitions.forEach(t -> {
        final Ehcache ehcache = buildCache(t);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Created Ehcache cache [{}] for [{}]", ehcache.getName(), t);
            final CacheConfiguration config = ehcache.getCacheConfiguration();
            LOGGER.debug("TicketCache.maxEntriesLocalHeap=[{}]", config.getMaxEntriesLocalHeap());
            LOGGER.debug("TicketCache.maxEntriesLocalDisk=[{}]", config.getMaxEntriesLocalDisk());
            LOGGER.debug("TicketCache.maxEntriesInCache=[{}]", config.getMaxEntriesInCache());
            LOGGER.debug("TicketCache.persistenceConfiguration=[{}]", config.getPersistenceConfiguration().getStrategy());
            LOGGER.debug("TicketCache.synchronousWrites=[{}]", config.getPersistenceConfiguration().getSynchronousWrites());
            LOGGER.debug("TicketCache.timeToLive=[{}]", config.getTimeToLiveSeconds());
            LOGGER.debug("TicketCache.timeToIdle=[{}]", config.getTimeToIdleSeconds());
            LOGGER.debug("TicketCache.cacheManager=[{}]", ehcache.getCacheManager().getName());
        }
        manager.addDecoratedCacheIfAbsent(ehcache);
    });
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("The following caches are available: [{}]", (Object[]) manager.getCacheNames());
    }
    return new EhCacheTicketRegistry(ticketCatalog, manager, CoreTicketUtils.newTicketRegistryCipherExecutor(crypto, "ehcache"));
}
Also used : EhCacheTicketRegistry(org.apereo.cas.ticket.registry.EhCacheTicketRegistry) TicketDefinition(org.apereo.cas.ticket.TicketDefinition) Ehcache(net.sf.ehcache.Ehcache) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) EncryptionRandomizedSigningJwtCryptographyProperties(org.apereo.cas.configuration.model.core.util.EncryptionRandomizedSigningJwtCryptographyProperties) RefreshScope(org.springframework.cloud.context.config.annotation.RefreshScope) Autowired(org.springframework.beans.factory.annotation.Autowired) EhCacheFactoryBean(org.springframework.cache.ehcache.EhCacheFactoryBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) EhCacheManagerFactoryBean(org.springframework.cache.ehcache.EhCacheManagerFactoryBean) Bean(org.springframework.context.annotation.Bean)

Example 44 with Ehcache

use of net.sf.ehcache.Ehcache in project cas by apereo.

the class EhCacheTicketRegistry method addTicket.

@Override
public void addTicket(final Ticket ticketToAdd) {
    final TicketDefinition metadata = this.ticketCatalog.find(ticketToAdd);
    final Ticket ticket = encodeTicket(ticketToAdd);
    final Element element = new Element(ticket.getId(), ticket);
    int idleValue = ticketToAdd.getExpirationPolicy().getTimeToIdle().intValue();
    if (idleValue <= 0) {
        idleValue = ticketToAdd.getExpirationPolicy().getTimeToLive().intValue();
    }
    if (idleValue <= 0) {
        idleValue = Integer.MAX_VALUE;
    }
    element.setTimeToIdle(idleValue);
    int aliveValue = ticketToAdd.getExpirationPolicy().getTimeToLive().intValue();
    if (aliveValue <= 0) {
        aliveValue = Integer.MAX_VALUE;
    }
    element.setTimeToLive(aliveValue);
    final Ehcache cache = getTicketCacheFor(metadata);
    LOGGER.debug("Adding ticket [{}] to the cache [{}] to live [{}] seconds and stay idle for [{}] seconds", ticket.getId(), cache.getName(), aliveValue, idleValue);
    cache.put(element);
}
Also used : Ticket(org.apereo.cas.ticket.Ticket) Element(net.sf.ehcache.Element) TicketDefinition(org.apereo.cas.ticket.TicketDefinition) Ehcache(net.sf.ehcache.Ehcache)

Example 45 with Ehcache

use of net.sf.ehcache.Ehcache in project cas by apereo.

the class EhCacheTicketRegistry method getTicket.

@Override
public Ticket getTicket(final String ticketIdToGet) {
    if (StringUtils.isBlank(ticketIdToGet)) {
        return null;
    }
    final TicketDefinition metadata = this.ticketCatalog.find(ticketIdToGet);
    if (metadata == null) {
        LOGGER.warn("Ticket [{}] is not registered in the catalog and is unrecognized", ticketIdToGet);
        return null;
    }
    final String ticketId = encodeTicketId(ticketIdToGet);
    if (StringUtils.isBlank(ticketId)) {
        return null;
    }
    final Ehcache ehcache = getTicketCacheFor(metadata);
    final Element element = ehcache.get(ticketId);
    if (element == null) {
        LOGGER.debug("No ticket by id [{}] is found in the registry", ticketId);
        return null;
    }
    final Ticket ticket = decodeTicket((Ticket) element.getObjectValue());
    final CacheConfiguration config = new CacheConfiguration();
    config.setTimeToIdleSeconds(ticket.getExpirationPolicy().getTimeToIdle());
    config.setTimeToLiveSeconds(ticket.getExpirationPolicy().getTimeToLive());
    if (element.isExpired(config) || ticket.isExpired()) {
        ehcache.remove(element);
        LOGGER.debug("Ticket [{}] has expired and is now evicted from the cache", ticket.getId());
        return null;
    }
    return ticket;
}
Also used : Ticket(org.apereo.cas.ticket.Ticket) Element(net.sf.ehcache.Element) TicketDefinition(org.apereo.cas.ticket.TicketDefinition) Ehcache(net.sf.ehcache.Ehcache) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration)

Aggregations

Ehcache (net.sf.ehcache.Ehcache)59 Test (org.junit.Test)21 Element (net.sf.ehcache.Element)15 CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)14 ResourcesElementsProvider (org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider)9 Cache (net.sf.ehcache.Cache)7 CacheManager (net.sf.ehcache.CacheManager)7 CacheException (net.sf.ehcache.CacheException)6 Configuration (net.sf.ehcache.config.Configuration)5 DiskStoreConfiguration (net.sf.ehcache.config.DiskStoreConfiguration)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 CacheKey (org.apereo.portal.utils.cache.CacheKey)4 FileSystemResource (org.springframework.core.io.FileSystemResource)4 Resource (org.springframework.core.io.Resource)4 UserDetails (org.springframework.security.core.userdetails.UserDetails)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 FileReader (java.io.FileReader)3