Search in sources :

Example 36 with CacheConfiguration

use of net.sf.ehcache.config.CacheConfiguration in project spring-framework by spring-projects.

the class EhCacheSupportTests method doTestEhCacheFactoryBean.

private void doTestEhCacheFactoryBean(boolean useCacheManagerFb) throws Exception {
    Cache cache;
    EhCacheManagerFactoryBean cacheManagerFb = null;
    boolean cacheManagerFbInitialized = false;
    try {
        EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
        Class<? extends Ehcache> objectType = cacheFb.getObjectType();
        assertTrue(Ehcache.class.isAssignableFrom(objectType));
        assertTrue("Singleton property", cacheFb.isSingleton());
        if (useCacheManagerFb) {
            cacheManagerFb = new EhCacheManagerFactoryBean();
            cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass()));
            cacheManagerFb.setCacheManagerName("cache");
            cacheManagerFb.afterPropertiesSet();
            cacheManagerFbInitialized = true;
            cacheFb.setCacheManager(cacheManagerFb.getObject());
        }
        cacheFb.setCacheName("myCache1");
        cacheFb.afterPropertiesSet();
        cache = (Cache) cacheFb.getObject();
        Class<? extends Ehcache> objectType2 = cacheFb.getObjectType();
        assertSame(objectType, objectType2);
        CacheConfiguration config = cache.getCacheConfiguration();
        assertEquals("myCache1", cache.getName());
        if (useCacheManagerFb) {
            assertEquals("myCache1.maxElements", 300, config.getMaxEntriesLocalHeap());
        } else {
            assertEquals("myCache1.maxElements", 10000, config.getMaxEntriesLocalHeap());
        }
        // Cache region is not defined. Should create one with default properties.
        cacheFb = new EhCacheFactoryBean();
        if (useCacheManagerFb) {
            cacheFb.setCacheManager(cacheManagerFb.getObject());
        }
        cacheFb.setCacheName("undefinedCache");
        cacheFb.afterPropertiesSet();
        cache = (Cache) cacheFb.getObject();
        config = cache.getCacheConfiguration();
        assertEquals("undefinedCache", cache.getName());
        assertTrue("default maxElements is correct", config.getMaxEntriesLocalHeap() == 10000);
        assertFalse("default eternal is correct", config.isEternal());
        assertTrue("default timeToLive is correct", config.getTimeToLiveSeconds() == 120);
        assertTrue("default timeToIdle is correct", config.getTimeToIdleSeconds() == 120);
        assertTrue("default diskExpiryThreadIntervalSeconds is correct", config.getDiskExpiryThreadIntervalSeconds() == 120);
        // overriding the default properties
        cacheFb = new EhCacheFactoryBean();
        if (useCacheManagerFb) {
            cacheFb.setCacheManager(cacheManagerFb.getObject());
        }
        cacheFb.setBeanName("undefinedCache2");
        cacheFb.setMaxEntriesLocalHeap(5);
        cacheFb.setTimeToLive(8);
        cacheFb.setTimeToIdle(7);
        cacheFb.setDiskExpiryThreadIntervalSeconds(10);
        cacheFb.afterPropertiesSet();
        cache = (Cache) cacheFb.getObject();
        config = cache.getCacheConfiguration();
        assertEquals("undefinedCache2", cache.getName());
        assertTrue("overridden maxElements is correct", config.getMaxEntriesLocalHeap() == 5);
        assertTrue("default timeToLive is correct", config.getTimeToLiveSeconds() == 8);
        assertTrue("default timeToIdle is correct", config.getTimeToIdleSeconds() == 7);
        assertTrue("overridden diskExpiryThreadIntervalSeconds is correct", config.getDiskExpiryThreadIntervalSeconds() == 10);
    } finally {
        if (cacheManagerFbInitialized) {
            cacheManagerFb.destroy();
        } else {
            CacheManager.getInstance().shutdown();
        }
    }
}
Also used : Ehcache(net.sf.ehcache.Ehcache) ClassPathResource(org.springframework.core.io.ClassPathResource) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) SelfPopulatingCache(net.sf.ehcache.constructs.blocking.SelfPopulatingCache) BlockingCache(net.sf.ehcache.constructs.blocking.BlockingCache) UpdatingSelfPopulatingCache(net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache) Cache(net.sf.ehcache.Cache)

Example 37 with CacheConfiguration

use of net.sf.ehcache.config.CacheConfiguration in project spring-framework by spring-projects.

the class EhCacheCacheTests method setUp.

@Before
public void setUp() {
    cacheManager = new CacheManager(new Configuration().name("EhCacheCacheTests").defaultCache(new CacheConfiguration("default", 100)));
    nativeCache = new net.sf.ehcache.Cache(new CacheConfiguration(CACHE_NAME, 100));
    cacheManager.addCache(nativeCache);
    cache = new EhCacheCache(nativeCache);
}
Also used : CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Configuration(net.sf.ehcache.config.Configuration) CacheManager(net.sf.ehcache.CacheManager) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Before(org.junit.Before)

Example 38 with CacheConfiguration

use of net.sf.ehcache.config.CacheConfiguration in project qi4j-sdk by Qi4j.

the class EhCachePoolMixin method activateService.

@Override
public void activateService() throws Exception {
    net.sf.ehcache.config.Configuration configuration = new net.sf.ehcache.config.Configuration();
    configureEhCache(configuration);
    CacheConfiguration cc = createCacheConfiguration("qi4j.ehcache.config.default");
    configuration.setDefaultCacheConfiguration(cc);
    cacheManager = CacheManager.newInstance(configuration);
}
Also used : PersistenceConfiguration(net.sf.ehcache.config.PersistenceConfiguration) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration) Configuration(org.qi4j.api.configuration.Configuration) CacheConfiguration(net.sf.ehcache.config.CacheConfiguration)

Example 39 with CacheConfiguration

use of net.sf.ehcache.config.CacheConfiguration 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 40 with CacheConfiguration

use of net.sf.ehcache.config.CacheConfiguration 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

CacheConfiguration (net.sf.ehcache.config.CacheConfiguration)71 Cache (net.sf.ehcache.Cache)26 CacheManager (net.sf.ehcache.CacheManager)16 Ehcache (net.sf.ehcache.Ehcache)14 Configuration (net.sf.ehcache.config.Configuration)14 Element (net.sf.ehcache.Element)9 PersistenceConfiguration (net.sf.ehcache.config.PersistenceConfiguration)8 DiskStoreConfiguration (net.sf.ehcache.config.DiskStoreConfiguration)6 Test (org.junit.Test)6 Resource (org.springframework.core.io.Resource)6 IOException (java.io.IOException)5 Bean (org.springframework.context.annotation.Bean)5 CachedResource (org.apereo.portal.utils.cache.resource.CachedResource)4 CachingResourceLoaderImpl (org.apereo.portal.utils.cache.resource.CachingResourceLoaderImpl)4 LoadedResource (org.apereo.portal.utils.cache.resource.LoadedResource)4 ResourcesElementsProvider (org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider)4 FileSystemResource (org.springframework.core.io.FileSystemResource)4 FileReader (java.io.FileReader)3 URL (java.net.URL)3 SelfPopulatingCache (net.sf.ehcache.constructs.blocking.SelfPopulatingCache)3