Java Code Examples for io.undertow.server.handlers.cache.DirectBufferCache#get()

The following examples show how to use io.undertow.server.handlers.cache.DirectBufferCache#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CachedResource.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public Long getContentLength() {
    //we always use the underlying size unless the data is cached in the buffer cache
    //to prevent a mis-match between size on disk and cached size
    final DirectBufferCache dataCache = cachingResourceManager.getDataCache();
    if (dataCache == null) {
        return underlyingResource.getContentLength();
    }
    final DirectBufferCache.CacheEntry existing = dataCache.get(cacheKey);
    if (existing == null || !existing.enabled()) {
        return underlyingResource.getContentLength();
    }
    //we only return the
    return (long) existing.size();
}
 
Example 2
Source File: CachedResource.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Long getContentLength() {
    //we always use the underlying size unless the data is cached in the buffer cache
    //to prevent a mis-match between size on disk and cached size
    final DirectBufferCache dataCache = cachingResourceManager.getDataCache();
    if(dataCache == null) {
        return underlyingResource.getContentLength();
    }
    final DirectBufferCache.CacheEntry existing = dataCache.get(cacheKey);
    if(existing == null || !existing.enabled()) {
        return underlyingResource.getContentLength();
    }
    //we only return the
    return (long)existing.size();
}
 
Example 3
Source File: CachedResource.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
@Override
public void serveRangeAsync(OutputChannel sender, HttpServerExchange exchange, long start, long end) {
    final DirectBufferCache dataCache = cachingResourceManager.getDataCache();
    if (dataCache == null) {
        ((RangeAwareResource) underlyingResource).serveRangeAsync(sender, exchange, start, end);
        return;
    }

    final DirectBufferCache.CacheEntry existing = dataCache.get(cacheKey);
    final Long length = getContentLength();
    //if it is not eligible to be served from the cache
    if (length == null || length > cachingResourceManager.getMaxFileSize()) {
        ((RangeAwareResource) underlyingResource).serveRangeAsync(sender, exchange, start, end);
        return;
    }
    //it is not cached yet, just serve it directly
    if (existing == null || !existing.enabled() || !existing.reference()) {
        //it is not cached yet, we can't use a range request to establish the cached item
        //so we just serve it
        ((RangeAwareResource) underlyingResource).serveRangeAsync(sender, exchange, start, end);
    } else {
        //serve straight from the cache
        ByteBuf[] buffers;
        boolean ok = false;
        try {
            LimitedBufferSlicePool.PooledByteBuffer[] pooled = existing.buffers();
            buffers = new ByteBuf[pooled.length];
            for (int i = 0; i < buffers.length; i++) {
                // Keep position from mutating
                buffers[i] = pooled[i].getBuffer().duplicate();
            }
            ok = true;
        } finally {
            if (!ok) {
                existing.dereference();
            }
        }
        if (start > 0) {
            long startDec = start;
            long endCount = 0;
            //handle the start of the range
            for (ByteBuf b : buffers) {
                if (endCount == end) {
                    b.clear();
                    continue;
                } else if (endCount + b.readableBytes() < end) {
                    endCount += b.readableBytes();
                } else {
                    b.writerIndex((int) (b.readerIndex() + (end - endCount)));
                    endCount = end;
                }
                if (b.readableBytes() >= startDec) {
                    startDec = 0;
                    b.readerIndex((int) (b.readerIndex() + startDec));
                } else {
                    startDec -= b.readableBytes();
                    b.clear();
                }
            }
        }
        sender.writeAsync(Unpooled.wrappedBuffer(buffers), true, new DereferenceCallback(existing, IoCallback.END_EXCHANGE), null);
    }
}
 
Example 4
Source File: CachedResource.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void serveRange(Sender sender, HttpServerExchange exchange, long start, long end, IoCallback completionCallback) {
    final DirectBufferCache dataCache = cachingResourceManager.getDataCache();
    if(dataCache == null) {
        ((RangeAwareResource)underlyingResource).serveRange(sender, exchange, start, end, completionCallback);
        return;
    }

    final DirectBufferCache.CacheEntry existing = dataCache.get(cacheKey);
    final Long length = getContentLength();
    //if it is not eligible to be served from the cache
    if (length == null || length > cachingResourceManager.getMaxFileSize()) {
        underlyingResource.serve(sender, exchange, completionCallback);
        return;
    }
    //it is not cached yet, just serve it directly
    if (existing == null || !existing.enabled() || !existing.reference()) {
        //it is not cached yet, install a wrapper to grab the data
        ((RangeAwareResource)underlyingResource).serveRange(sender, exchange, start, end, completionCallback);
    } else {
        //serve straight from the cache
        ByteBuffer[] buffers;
        boolean ok = false;
        try {
            LimitedBufferSlicePool.PooledByteBuffer[] pooled = existing.buffers();
            buffers = new ByteBuffer[pooled.length];
            for (int i = 0; i < buffers.length; i++) {
                // Keep position from mutating
                buffers[i] = pooled[i].getBuffer().duplicate();
            }
            ok = true;
        } finally {
            if (!ok) {
                existing.dereference();
            }
        }
        if(start > 0) {
            long startDec = start;
            long endCount = 0;
            //handle the start of the range
            for(ByteBuffer b : buffers) {
                if(endCount == end) {
                    b.limit(b.position());
                    continue;
                } else if(endCount + b.remaining() < end) {
                    endCount += b.remaining();
                } else {
                    b.limit((int) (b.position() + (end - endCount)));
                    endCount = end;
                }
                if(b.remaining() >= startDec) {
                    startDec = 0;
                    b.position((int) (b.position() + startDec));
                } else {
                    startDec -= b.remaining();
                    b.position(b.limit());
                }
            }
        }
        sender.send(buffers, new DereferenceCallback(existing, completionCallback));
    }
}