Search in sources :

Example 76 with Element

use of net.sf.ehcache.Element in project ninja by ninjaframework.

the class CacheEhCacheImpl method set.

public void set(String key, Object value, int expiration) {
    Element element = new Element(key, value);
    element.setTimeToLive(expiration);
    ehCache.put(element);
}
Also used : Element(net.sf.ehcache.Element)

Example 77 with Element

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

the class CacheProducer method performCacheOperation.

private void performCacheOperation(Exchange exchange, String operation, String key) throws Exception {
    if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_ADD)) {
        LOG.debug("Adding an element with key {} into the Cache", key);
        Element element = createElementFromBody(key, exchange, CacheConstants.CACHE_OPERATION_ADD);
        cache.put(element);
    } else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_UPDATE)) {
        LOG.debug("Updating an element with key {} into the Cache", key);
        Element element = createElementFromBody(key, exchange, CacheConstants.CACHE_OPERATION_UPDATE);
        cache.put(element);
    } else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_DELETEALL)) {
        LOG.debug("Deleting All elements from the Cache");
        cache.removeAll();
    } else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_DELETE)) {
        LOG.debug("Deleting an element with key {} into the Cache", key);
        cache.remove(key);
    } else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_GET)) {
        LOG.debug("Quering an element with key {} from the Cache", key);
        Element element = cache.get(key);
        if (element != null) {
            exchange.getIn().setHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND, true);
            exchange.getIn().setBody(element.getObjectValue());
        } else {
            exchange.getIn().removeHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND);
        }
    } else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_CHECK)) {
        LOG.debug("Querying an element with key {} from the Cache", key);
        // getQuiet checks for element expiry
        Element element = cache.getQuiet(key);
        if (element != null) {
            exchange.getIn().setHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND, true);
        } else {
            exchange.getIn().removeHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND);
        }
    } else {
        throw new CacheException(CacheConstants.CACHE_OPERATION + " " + operation + " is not supported.");
    }
}
Also used : CacheException(net.sf.ehcache.CacheException) Element(net.sf.ehcache.Element)

Example 78 with Element

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

the class CacheProducer method createElementFromBody.

private Element createElementFromBody(String key, Exchange exchange, String cacheOperation) throws NoTypeConversionAvailableException {
    Element element;
    Object body = exchange.getIn().getBody();
    if (body == null) {
        throw new CacheException("Body cannot be null for operation " + cacheOperation);
    } else if (body instanceof Serializable) {
        element = new Element(key, body);
    } else if (config.isObjectCache()) {
        element = new Element(key, body);
    } else {
        InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, body);
        // Read InputStream into a byte[] buffer
        element = new Element(key, exchange.getContext().getTypeConverter().mandatoryConvertTo(byte[].class, is));
    }
    // set overrides for the cache expiration and such
    final Integer ttl = exchange.getIn().getHeader(CacheConstants.CACHE_ELEMENT_EXPIRY_TTL, Integer.class);
    if (ttl != null) {
        element.setTimeToLive(ttl);
    }
    final Integer idle = exchange.getIn().getHeader(CacheConstants.CACHE_ELEMENT_EXPIRY_IDLE, Integer.class);
    if (idle != null) {
        element.setTimeToIdle(idle);
    }
    final Boolean flag = exchange.getIn().getHeader(CacheConstants.CACHE_ELEMENT_EXPIRY_ETERNAL, Boolean.class);
    if (flag != null) {
        element.setEternal(flag);
    }
    return element;
}
Also used : Serializable(java.io.Serializable) CacheException(net.sf.ehcache.CacheException) InputStream(java.io.InputStream) Element(net.sf.ehcache.Element)

Example 79 with Element

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

the class CacheProducerTest method testAddingDataElementTimeToLive.

@Test
public void testAddingDataElementTimeToLive() throws Exception {
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            from("direct:a").setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD)).setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")).setHeader(CacheConstants.CACHE_ELEMENT_EXPIRY_TTL, constant(42)).to("cache://TestCache1");
        }
    });
    context.start();
    log.debug("------------Beginning CacheProducer Add Test---------------");
    sendOriginalFile();
    Element element = fetchElement("Ralph_Waldo_Emerson");
    assertEquals(42, element.getTimeToLive());
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) Element(net.sf.ehcache.Element) Test(org.junit.Test) BaseCacheTest(org.apache.camel.component.BaseCacheTest)

Example 80 with Element

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

the class CacheProducerTest method testCheckExpiredDataFromCache.

@Test
public void testCheckExpiredDataFromCache() throws Exception {
    context.addRoutes(new RouteBuilder() {

        public void configure() {
            onException(CacheException.class).handled(true).to("log:LOGGER").to("mock:CacheProducerTest.cacheException");
            from("direct:a").setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD)).setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")).setBody(constant("Test body")).to("cache://TestCache1?timeToLiveSeconds=1");
            from("direct:b").setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_URL_CHECK)).setHeader(CacheConstants.CACHE_KEY, constant("Ralph_Waldo_Emerson")).to("cache://TestCache1").choice().when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNull()).to("mock:CacheProducerTest.result").end();
        }
    });
    // Put an element to the cache
    resultEndpoint.expectedMessageCount(1);
    cacheExceptionEndpoint.expectedMessageCount(0);
    context.start();
    log.debug("------------Beginning CacheProducer Check An Element Does Not Exist After Expiry Test---------------");
    sendOriginalFile();
    // simulate the cache element to appear as "expired" (without having to wait for it to happen)
    // however as there's no public API to do so we're forced to make use of the reflection API here
    Cache testCache = cache.getCacheManagerFactory().getInstance().getCache("TestCache1");
    Element element = testCache.getQuiet("Ralph_Waldo_Emerson");
    ReflectionHelper.setField(Element.class.getDeclaredField("creationTime"), element, System.currentTimeMillis() - 10000);
    testCache.putQuiet(element);
    // Check that the element is not found when expired
    template.sendBody("direct:b", "dummy");
    resultEndpoint.assertIsSatisfied();
    cacheExceptionEndpoint.assertIsSatisfied();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) Element(net.sf.ehcache.Element) Cache(net.sf.ehcache.Cache) Test(org.junit.Test) BaseCacheTest(org.apache.camel.component.BaseCacheTest)

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