Skip to content

Commit

Permalink
Merge pull request #8621 from eclipse/jetty-12.0.x-CachedContentFactory
Browse files Browse the repository at this point in the history
Fix Caching ContentFactories in Jetty-12
  • Loading branch information
lachlan-roberts authored Oct 4, 2022
2 parents f76df2c + 1c9ac9c commit 05a4b96
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 1,059 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,32 @@ public void flushCache()
}
}

/**
* Tests whether the given HttpContent is cacheable, and if there is enough room to fit it in the cache.
*
* @param httpContent the HttpContent to test.
* @return whether the HttpContent is cacheable.
*/
protected boolean isCacheable(HttpContent httpContent)
{
if (httpContent == null)
return false;

if (httpContent.getResource().isDirectory())
return false;

if (_maxCachedFiles <= 0)
return false;

// Will it fit in the cache?
long len = httpContent.getContentLengthValue();
if (len <= 0)
return false;
if (isUseFileMappedBuffer())
return true;
return ((len <= _maxCachedFileSize) && (len + getCachedSize() <= _maxCacheSize));
}

@Override
public HttpContent getContent(String path) throws IOException
{
Expand All @@ -178,14 +204,14 @@ public HttpContent getContent(String path) throws IOException
}

HttpContent httpContent = _authority.getContent(path);
// Do not cache directories or files that are too big
if (httpContent != null && !httpContent.getResource().isDirectory() && httpContent.getContentLengthValue() <= _maxCachedFileSize)
if (isCacheable(httpContent))
{
httpContent = cachingHttpContent = new CachingHttpContent(path, httpContent);
_cache.put(path, cachingHttpContent);
_cachedSize.addAndGet(cachingHttpContent.calculateSize());
shrinkCache();
}

return httpContent;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ public Map<CompressedContentFormat, HttpContent> getPrecompressedContents()
@Override
public ByteBuffer getBuffer()
{
return _content.getBuffer();
return _precompressedContent.getBuffer();
}

@Override
public void release()
{
_content.release();
_precompressedContent.release();
}
}
Loading

0 comments on commit 05a4b96

Please sign in to comment.