Java Examples for net.sf.ehcache.constructs.blocking.BlockingCache

The following java examples will help you to understand the usage of net.sf.ehcache.constructs.blocking.BlockingCache. These source code samples are taken from different open source projects.

Example 1
Project: nextreports-server-master  File: EhCacheFactory.java View source code
protected void createCache(String name, int expirationTime) {
    synchronized (this.getClass()) {
        cacheManager.addCache(name);
        Ehcache cache = cacheManager.getEhcache(name);
        CacheConfiguration config = cache.getCacheConfiguration();
        config.setEternal(false);
        config.setTimeToLiveSeconds(expirationTime);
        //		    config.setTimeToIdleSeconds(60);
        //		    config.setMaxElementsInMemory(10000);
        //		    config.setMaxElementsOnDisk(1000000);
        BlockingCache blockingCache = new BlockingCache(cache);
        cacheManager.replaceCacheWithDecoratedCache(cache, blockingCache);
    }
}
Example 2
Project: onebusaway-application-modules-master  File: EhCacheFactoryBean.java View source code
/**
   * Decorate the given Cache, if necessary.
   * 
   * @param cache the raw Cache object, based on the configuration of this
   *          FactoryBean
   * @return the (potentially decorated) cache object to be registered with the
   *         CacheManager
   */
protected Ehcache decorateCache(Ehcache cache) {
    if (this.cacheEntryFactory != null) {
        if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) {
            return new UpdatingSelfPopulatingCache(cache, (UpdatingCacheEntryFactory) this.cacheEntryFactory);
        } else {
            return new SelfPopulatingCache(cache, this.cacheEntryFactory);
        }
    }
    if (this.blocking) {
        return new BlockingCache(cache);
    }
    return cache;
}
Example 3
Project: com.idega.core-master  File: CachingFilter.java View source code
/**
     * Build page info either using the cache or building the page directly.
     * <p/>
     * Some requests are for page fragments which should never be gzipped, or for
     * other pages which are not gzipped.
     */
protected PageInfo buildPageInfo(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain, long timeToLive) throws Exception {
    // Look up the cached page
    final String key = calculateKey(request);
    PageInfo pageInfo = null;
    try {
        BlockingCache blockingCache = getBlockingCache();
        checkNoReentry(request);
        Element e = blockingCache.get(key);
        Object value = e == null ? null : e.getObjectValue();
        if (value instanceof PageInfo) {
            pageInfo = (PageInfo) value;
        }
        if (pageInfo == null) {
            try {
                // Page is not cached - build the response, cache it, and send to client
                pageInfo = buildPage(request, response, chain, timeToLive);
                if (pageInfo.isOk()) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("PageInfo ok. Adding to cache " + blockingCache.getName() + " with key " + key);
                    }
                    Element element = new Element(key, pageInfo);
                    blockingCache.put(element);
                } else {
                    if (LOG.isWarnEnabled()) {
                        LOG.warn("PageInfo was not ok(200). Putting null into cache " + blockingCache.getName() + " with key " + key);
                    }
                    blockingCache.put(new Element(key, null));
                }
            } catch (final Throwable throwable) {
                blockingCache.put(new Element(key, null));
                throw new Exception(throwable);
            }
        }
    } finally {
        Thread.currentThread().setName("Application Server Thread");
    }
    return pageInfo;
}
Example 4
Project: miso-lims-master  File: DbUtils.java View source code
public static <T extends Nameable> void updateCaches(CacheManager cacheManager, T obj, Class<T> cacheClass) {
    Cache cache = DbUtils.lookupCache(cacheManager, cacheClass, true);
    if (cache != null && cache.getKeys().size() > 0) {
        log.debug("Removing " + cacheClass.getSimpleName() + " " + obj.getId() + " from " + cache.getName());
        BlockingCache c = new BlockingCache(cache);
        c.remove(DbUtils.hashCodeCacheKeyFor(obj.getId()));
    }
    cache = DbUtils.lookupCache(cacheManager, cacheClass, false);
    if (cache != null && cache.getKeys().size() > 0) {
        log.debug("Removing " + cacheClass.getSimpleName() + " " + obj.getId() + " from " + cache.getName());
        BlockingCache c = new BlockingCache(cache);
        c.remove(DbUtils.hashCodeCacheKeyFor(obj.getId()));
    }
}
Example 5
Project: concourse-connect-master  File: CacheUtils.java View source code
public static Ehcache createInMemoryBlockingCache(String cacheName, int maxElements) {
    CacheManager manager = CacheManager.getInstance();
    Ehcache cache = getCache(cacheName);
    if (cache == null) {
        cache = new Cache(cacheName, maxElements, memoryStoreEvictionPolicy, overflowToDisk, diskStorePath, eternal, timeToLive, timeToIdle, diskPersistent, diskExpiryThreadIntervalSeconds, null);
        BlockingCache blockingCache = new BlockingCache(cache);
        manager.addCache(blockingCache);
        LOG.info("blocking cache created: " + cache.getName());
    }
    return cache;
}
Example 6
Project: lutece-core-master  File: HeadersPageCachingFilter.java View source code
/**
     * Initialization of the filter
     */
private void init() {
    // Execute the doInit
    synchronized (HeadersPageCachingFilter.class) {
        if (blockingCache == null) {
            _strCacheName = filterConfig.getInitParameter(INIT_PARAM_CACHE_NAME);
            CacheService.getInstance().createCache(_strCacheName);
            _cache = CacheManager.getInstance().getCache(_strCacheName);
            CacheService.registerCacheableService(this);
            _logger.debug("Initializing cache : " + _strCacheName);
            setCacheNameIfAnyConfigured(filterConfig);
            final String localCacheName = getCacheName();
            Ehcache cache = getCacheManager().getEhcache(localCacheName);
            if (cache == null) {
                throw new CacheException("cache '" + localCacheName + "' not found in configuration");
            }
            if (!(cache instanceof BlockingCache)) {
                // decorate and substitute
                BlockingCache newBlockingCache = new BlockingCache(cache);
                getCacheManager().replaceCacheWithDecoratedCache(cache, newBlockingCache);
            }
            blockingCache = (BlockingCache) getCacheManager().getEhcache(localCacheName);
            Integer blockingTimeoutMillis = parseBlockingCacheTimeoutMillis(filterConfig);
            if ((blockingTimeoutMillis != null) && (blockingTimeoutMillis > 0)) {
                blockingCache.setTimeoutMillis(blockingTimeoutMillis);
            }
        }
        PageService.addPageEventListener(this);
    }
    _bInit = true;
}
Example 7
Project: spring-framework-2.5.x-master  File: EhCacheSupportTests.java View source code
public void testEhCacheFactoryBeanWithBlockingCache() throws Exception {
    EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
    cacheManagerFb.afterPropertiesSet();
    try {
        CacheManager cm = (CacheManager) cacheManagerFb.getObject();
        EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
        cacheFb.setCacheManager(cm);
        cacheFb.setCacheName("myCache1");
        cacheFb.setBlocking(true);
        cacheFb.afterPropertiesSet();
        Ehcache myCache1 = cm.getEhcache("myCache1");
        assertTrue(myCache1 instanceof BlockingCache);
    } finally {
        cacheManagerFb.destroy();
    }
}
Example 8
Project: spring-modules-master  File: EhCacheFacade.java View source code
/**
	 * Decorate the given Cache, if necessary.
	 * <p>The default implementation simply returns the given cache object as-is.
	 *
	 * @param cache the raw Cache object, based on the configuration of this FactoryBean
	 * @param model the model containing the name of the cache to retrieve
	 * @return the (potentially decorated) cache object to be registered with the CacheManager
	 */
protected Ehcache decorateCache(Cache cache, EhCacheCachingModel model) {
    if (model.getCacheEntryFactory() != null) {
        if (model.getCacheEntryFactory() instanceof UpdatingCacheEntryFactory) {
            return new UpdatingSelfPopulatingCache(cache, (UpdatingCacheEntryFactory) model.getCacheEntryFactory());
        } else {
            return new SelfPopulatingCache(cache, model.getCacheEntryFactory());
        }
    }
    if (model.isBlocking()) {
        return new BlockingCache(cache);
    }
    return cache;
}
Example 9
Project: jooby-master  File: CacheConfigurationBuilderTest.java View source code
@Test
public void cacheDecoratorFactory() {
    Config config = ConfigFactory.empty().withValue("cacheDecoratorFactory.class", fromAnyRef(BlockingCache.class.getName()));
    CacheConfigurationBuilder builder = new CacheConfigurationBuilder("c1");
    CacheConfiguration cache = builder.build(config);
    List<CacheDecoratorFactoryConfiguration> decorators = cache.getCacheDecoratorConfigurations();
    assertEquals(1, decorators.size());
    assertEquals(BlockingCache.class.getName(), decorators.iterator().next().getFullyQualifiedClassPath());
}
Example 10
Project: spring-framework-master  File: EhCacheSupportTests.java View source code
@Test
public void testEhCacheFactoryBeanWithBlockingCache() throws Exception {
    EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
    cacheManagerFb.afterPropertiesSet();
    try {
        CacheManager cm = cacheManagerFb.getObject();
        EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
        cacheFb.setCacheManager(cm);
        cacheFb.setCacheName("myCache1");
        cacheFb.setBlocking(true);
        assertEquals(cacheFb.getObjectType(), BlockingCache.class);
        cacheFb.afterPropertiesSet();
        Ehcache myCache1 = cm.getEhcache("myCache1");
        assertTrue(myCache1 instanceof BlockingCache);
    } finally {
        cacheManagerFb.destroy();
    }
}
Example 11
Project: jrails-master  File: CachingFilter.java View source code
/**
     * Initialises blockingCache to use. The BlockingCache created by this method does not have a lock timeout set.
     * <p/>
     * A timeout can be appled using <code>blockingCache.setTimeoutMillis(int timeout)</code> and takes effect immediately
     * for all new requests
     *
     * @throws CacheException The most likely cause is that a cache has not been
     *                        configured in ehcache's configuration file ehcache.xml for the filter name
     * @param filterConfig
     */
public void doInit(FilterConfig filterConfig) throws CacheException {
    synchronized (this.getClass()) {
        if (blockingCache == null) {
            final String cacheName = getCacheName();
            Ehcache cache = getCacheManager().getEhcache(cacheName);
            if (!(cache instanceof BlockingCache)) {
                //decorate and substitute
                BlockingCache newBlockingCache = new BlockingCache(cache);
                getCacheManager().replaceCacheWithDecoratedCache(cache, newBlockingCache);
            }
            blockingCache = (BlockingCache) getCacheManager().getEhcache(getCacheName());
        }
    }
}
Example 12
Project: sakai-cle-master  File: EhCacheFactoryBean.java View source code
/**
	 * Decorate the given Cache, if necessary.
	 * @param cache the raw Cache object, based on the configuration of this FactoryBean
	 * @return the (potentially decorated) cache object to be registered with the CacheManager
	 */
protected Ehcache decorateCache(Ehcache cache) {
    if (this.cacheEntryFactory != null) {
        if (this.cacheEntryFactory instanceof UpdatingCacheEntryFactory) {
            return new UpdatingSelfPopulatingCache(cache, (UpdatingCacheEntryFactory) this.cacheEntryFactory);
        } else {
            return new SelfPopulatingCache(cache, this.cacheEntryFactory);
        }
    }
    if (this.blocking) {
        return new BlockingCache(cache);
    }
    return cache;
}
Example 13
Project: spring-modules-ehcache-master  File: EhCacheFacade.java View source code
/**
	 * Decorate the given Cache, if necessary.
	 * <p>The default implementation simply returns the given cache object as-is.
	 *
	 * @param cache the raw Cache object, based on the configuration of this FactoryBean
	 * @param model the model containing the name of the cache to retrieve
	 * @return the (potentially decorated) cache object to be registered with the CacheManager
	 */
protected Ehcache decorateCache(Cache cache, EhCacheCachingModel model) {
    if (model.getCacheEntryFactory() != null) {
        if (model.getCacheEntryFactory() instanceof UpdatingCacheEntryFactory) {
            return new UpdatingSelfPopulatingCache(cache, (UpdatingCacheEntryFactory) model.getCacheEntryFactory());
        } else {
            return new SelfPopulatingCache(cache, model.getCacheEntryFactory());
        }
    }
    if (model.isBlocking()) {
        return new BlockingCache(cache);
    }
    return cache;
}
Example 14
Project: fiware-keypass-master  File: BlockingCacheDecoratorFactory.java View source code
@Override
public Ehcache createDefaultDecoratedEhcache(Ehcache cache, Properties properties) {
    return new BlockingCache(cache);
}
Example 15
Project: cloudstack-master  File: EhcacheLimitStore.java View source code
public void setCache(Ehcache cache) {
    BlockingCache ref;
    if (!(cache instanceof BlockingCache)) {
        ref = new BlockingCache(cache);
        cache.getCacheManager().replaceCacheWithDecoratedCache(cache, new BlockingCache(cache));
    } else {
        ref = (BlockingCache) cache;
    }
    this.cache = ref;
}
Example 16
Project: rate-limit-master  File: EhcacheTokenStore.java View source code
/**
     * Sets the non-null {@link Ehcache} used to back this {@link TokenStore}.
     * 
     * @param cache
     *            a non-null {@link Ehcache}
     */
public void setCache(Ehcache cache) {
    BlockingCache ref;
    if (!(cache instanceof BlockingCache)) {
        ref = new BlockingCache(cache);
        cache.getCacheManager().replaceCacheWithDecoratedCache(cache, new BlockingCache(cache));
    } else {
        ref = (BlockingCache) cache;
    }
    this.cache = ref;
}