Enhanced PushCacheFilter

Can learn associations from if-modified-since requests
has __renewPushCache__ special URI to renew associations HACK!
This commit is contained in:
Greg Wilkins 2014-09-21 15:02:40 +10:00
parent 51631c2a46
commit 580c87bcc6

View File

@ -65,7 +65,8 @@ public class PushCacheFilter implements Filter
private static final Logger LOG = Log.getLogger(PushCacheFilter.class);
private final ConcurrentMap<String, PrimaryResource> _cache = new ConcurrentHashMap<>();
private long _associatePeriod = 2000L;
private long _associatePeriod = 4000L;
private volatile long _renew = System.nanoTime();
@Override
public void init(FilterConfig config) throws ServletException
@ -78,6 +79,7 @@ public class PushCacheFilter implements Filter
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException
{
long now=System.nanoTime();
HttpServletRequest request = (HttpServletRequest)req;
if (Boolean.TRUE==req.getAttribute("org.eclipse.jetty.pushed"))
@ -87,7 +89,6 @@ public class PushCacheFilter implements Filter
return;
}
// Iterating over fields is more efficient than multiple gets
HttpFields fields = Request.getBaseRequest(req).getHttpFields();
boolean conditional = false;
@ -120,9 +121,13 @@ public class PushCacheFilter implements Filter
if (LOG.isDebugEnabled())
LOG.debug("{} {} referrer={} conditional={}", request.getMethod(), request.getRequestURI(), referrer, conditional);
if (!conditional)
{
String path = URIUtil.addPaths(request.getServletPath(), request.getPathInfo());
if (path.endsWith("__renewPushCache__"))
{
if (LOG.isDebugEnabled())
LOG.debug("Renew {}", now);
_renew=now;
}
if (referrer != null)
{
@ -141,7 +146,7 @@ public class PushCacheFilter implements Filter
if (primaryTimestamp != 0)
{
RequestDispatcher dispatcher = request.getServletContext().getRequestDispatcher(path);
if (System.nanoTime() - primaryTimestamp < TimeUnit.MILLISECONDS.toNanos(_associatePeriod))
if (now - primaryTimestamp < TimeUnit.MILLISECONDS.toNanos(_associatePeriod))
{
if (primaryResource._associated.putIfAbsent(path, dispatcher) == null)
{
@ -167,12 +172,23 @@ public class PushCacheFilter implements Filter
PrimaryResource t = new PrimaryResource();
primaryResource = _cache.putIfAbsent(path, t);
primaryResource = primaryResource == null ? t : primaryResource;
primaryResource._timestamp.compareAndSet(0, System.nanoTime());
primaryResource._timestamp.compareAndSet(0, now);
if (LOG.isDebugEnabled())
LOG.debug("Cached {}", path);
}
else
{
long last=primaryResource._timestamp.get();
if (last<_renew && primaryResource._timestamp.compareAndSet(last, now))
{
primaryResource._associated.clear();
if (LOG.isDebugEnabled())
LOG.debug("Clear associated {}", path);
}
}
if (!primaryResource._associated.isEmpty())
// Push associated for non conditional
if (!conditional && !primaryResource._associated.isEmpty())
{
for (RequestDispatcher dispatcher : primaryResource._associated.values())
{
@ -181,7 +197,6 @@ public class PushCacheFilter implements Filter
((Dispatcher)dispatcher).push(request);
}
}
}
chain.doFilter(req, resp);
}