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);
}
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.");
}
}
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;
}
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());
}
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();
}
Aggregations