use of io.apiman.gateway.engine.storage.model.CacheEntry in project apiman by apiman.
the class EsCacheStoreComponent method get.
/**
* @see io.apiman.gateway.engine.components.ICacheStoreComponent#get(java.lang.String, java.lang.Class, io.apiman.gateway.engine.async.IAsyncResultHandler)
*/
@Override
public <T> void get(String cacheKey, final Class<T> type, final IAsyncResultHandler<T> handler) {
try {
GetResponse response = getClient().get(new GetRequest(getFullIndexName()).id(cacheKey), RequestOptions.DEFAULT);
if (response.isExists()) {
String sourceAsString = response.getSourceAsString();
CacheEntry cacheEntry = JSON_MAPPER.readValue(sourceAsString, CacheEntry.class);
try {
T rval = (T) JSON_MAPPER.reader(type).readValue(cacheEntry.getHead());
handler.handle(AsyncResultImpl.create(rval));
} catch (IOException e) {
// TODO log this error.
handler.handle(AsyncResultImpl.create((T) null));
}
} else {
handler.handle(AsyncResultImpl.create((T) null));
}
} catch (Throwable e) {
handler.handle(AsyncResultImpl.create(e, type));
}
}
use of io.apiman.gateway.engine.storage.model.CacheEntry in project apiman by apiman.
the class EsCacheStoreComponent method put.
/**
* @see io.apiman.gateway.engine.components.ICacheStoreComponent#put(java.lang.String, java.lang.Object, long)
*/
@Override
public <T> void put(String cacheKey, T jsonObject, long timeToLive) throws IOException {
CacheEntry entry = new CacheEntry();
entry.setData(null);
entry.setExpiresOn(System.currentTimeMillis() + (timeToLive * 1000));
entry.setHead(JSON_MAPPER.writeValueAsString(entry));
IndexRequest indexRequest = new IndexRequest(getFullIndexName()).source(JSON_MAPPER.writeValueAsBytes(entry), XContentType.JSON).id(cacheKey);
try {
getClient().index(indexRequest, RequestOptions.DEFAULT);
} catch (Throwable e) {
LOGGER.error(e, "Unable to cache entry at {0}. This should be non-fatal.", cacheKey);
}
}
use of io.apiman.gateway.engine.storage.model.CacheEntry in project apiman by apiman.
the class AbstractCacheStoreComponent method putBinary.
/**
* @see ICacheStoreComponent#putBinary(String, Object, long)
*/
@Override
public <T> ISignalWriteStream putBinary(final String cacheKey, final T jsonObject, final long timeToLive) throws IOException {
final CacheEntry entry = new CacheEntry();
entry.setExpiresOn(System.currentTimeMillis() + (timeToLive * 1000));
entry.setHead(JSON_MAPPER.writeValueAsString(jsonObject));
final IApimanBuffer data = bufferFactory.createBuffer();
return new ISignalWriteStream() {
boolean finished = false;
boolean aborted = false;
@Override
public void abort(Throwable t) {
finished = true;
aborted = false;
}
@Override
public boolean isFinished() {
return finished;
}
@Override
public void write(IApimanBuffer chunk) {
data.append(chunk);
}
@Override
public void end() {
if (!aborted) {
entry.setData(Base64.encodeBase64String(data.getBytes()));
try {
getStore().put(cacheKey, entry, timeToLive);
} catch (Throwable e) {
LOGGER.error("Error writing binary cache entry with key: {}", cacheKey, e);
}
}
finished = true;
}
};
}
use of io.apiman.gateway.engine.storage.model.CacheEntry in project apiman by apiman.
the class AbstractCacheStoreComponent method getBinary.
/**
* @see ICacheStoreComponent#getBinary(String, Class, IAsyncResultHandler)
*/
@Override
public <T> void getBinary(final String cacheKey, final Class<T> type, final IAsyncResultHandler<ISignalReadStream<T>> handler) {
try {
final CacheEntry cacheEntry = getStore().get(cacheKey, CacheEntry.class);
// Did the fetch succeed? If not, return null.
if (null == cacheEntry) {
handler.handle(AsyncResultImpl.create((ISignalReadStream<T>) null));
return;
}
// Is the cache entry expired? If so return null.
if (System.currentTimeMillis() > cacheEntry.getExpiresOn()) {
// Cache item has expired. Return null instead of the cached data.
handler.handle(AsyncResultImpl.create((ISignalReadStream<T>) null));
return;
}
try {
@SuppressWarnings("unchecked") final T head = (T) JSON_MAPPER.readValue(cacheEntry.getHead(), type);
final String b64Data = cacheEntry.getData();
final IApimanBuffer data = bufferFactory.createBuffer(Base64.decodeBase64(b64Data));
final ISignalReadStream<T> rval = new ISignalReadStream<T>() {
IAsyncHandler<IApimanBuffer> bodyHandler;
IAsyncHandler<Void> endHandler;
boolean finished = false;
boolean aborted = false;
@Override
public void bodyHandler(IAsyncHandler<IApimanBuffer> bodyHandler) {
this.bodyHandler = bodyHandler;
}
@Override
public void endHandler(IAsyncHandler<Void> endHandler) {
this.endHandler = endHandler;
}
@Override
public T getHead() {
return head;
}
@Override
public boolean isFinished() {
return finished;
}
@Override
public void abort(Throwable t) {
finished = true;
aborted = true;
}
@Override
public void transmit() {
if (!aborted) {
bodyHandler.handle(data);
endHandler.handle(null);
}
finished = true;
}
};
handler.handle(AsyncResultImpl.create(rval));
} catch (Throwable e) {
LOGGER.error("Error reading binary cache entry with key: {}", cacheKey, e);
handler.handle(AsyncResultImpl.create((ISignalReadStream<T>) null));
}
} catch (Throwable e) {
handler.handle(AsyncResultImpl.create((ISignalReadStream<T>) null));
}
}
use of io.apiman.gateway.engine.storage.model.CacheEntry in project apiman by apiman.
the class AbstractCacheStoreComponent method get.
/**
* @see ICacheStoreComponent#get(String, Class, IAsyncResultHandler)
*/
@Override
public <T> void get(String cacheKey, final Class<T> type, final IAsyncResultHandler<T> handler) {
try {
final CacheEntry cacheEntry = getStore().get(cacheKey, CacheEntry.class);
if (null != cacheEntry) {
try {
@SuppressWarnings("unchecked") final T head = JSON_MAPPER.readValue(cacheEntry.getHead(), type);
handler.handle(AsyncResultImpl.create(head));
} catch (Exception e) {
LOGGER.error("Error reading cache entry with key: {}", cacheKey, e);
handler.handle(AsyncResultImpl.create((T) null));
}
} else {
handler.handle(AsyncResultImpl.create((T) null));
}
} catch (Throwable e) {
handler.handle(AsyncResultImpl.create(e, type));
}
}
Aggregations