Search in sources :

Example 1 with CacheEntry

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));
    }
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) IOException(java.io.IOException) CacheEntry(io.apiman.gateway.engine.storage.model.CacheEntry) GetResponse(org.elasticsearch.action.get.GetResponse)

Example 2 with CacheEntry

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);
    }
}
Also used : CacheEntry(io.apiman.gateway.engine.storage.model.CacheEntry) IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 3 with CacheEntry

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;
        }
    };
}
Also used : IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) CacheEntry(io.apiman.gateway.engine.storage.model.CacheEntry) ISignalWriteStream(io.apiman.gateway.engine.io.ISignalWriteStream)

Example 4 with CacheEntry

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));
    }
}
Also used : IApimanBuffer(io.apiman.gateway.engine.io.IApimanBuffer) ISignalReadStream(io.apiman.gateway.engine.io.ISignalReadStream) CacheEntry(io.apiman.gateway.engine.storage.model.CacheEntry) IAsyncHandler(io.apiman.gateway.engine.async.IAsyncHandler)

Example 5 with CacheEntry

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));
    }
}
Also used : CacheEntry(io.apiman.gateway.engine.storage.model.CacheEntry) IOException(java.io.IOException)

Aggregations

CacheEntry (io.apiman.gateway.engine.storage.model.CacheEntry)8 IApimanBuffer (io.apiman.gateway.engine.io.IApimanBuffer)4 IAsyncHandler (io.apiman.gateway.engine.async.IAsyncHandler)2 ISignalReadStream (io.apiman.gateway.engine.io.ISignalReadStream)2 ISignalWriteStream (io.apiman.gateway.engine.io.ISignalWriteStream)2 IOException (java.io.IOException)2 GetRequest (org.elasticsearch.action.get.GetRequest)2 GetResponse (org.elasticsearch.action.get.GetResponse)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2