Author: michiel
Date: 2009-06-03 16:28:39 +0200 (Wed, 03 Jun 2009)
New Revision: 35621

Modified:
   mmbase/trunk/src/org/mmbase/framework/CachedRenderer.java
Log:
Honor Cache-Control header. Warn if the response cannot be cached (which you 
probably are wanting)

Modified: mmbase/trunk/src/org/mmbase/framework/CachedRenderer.java
===================================================================
--- mmbase/trunk/src/org/mmbase/framework/CachedRenderer.java   2009-06-03 
13:46:59 UTC (rev 35620)
+++ mmbase/trunk/src/org/mmbase/framework/CachedRenderer.java   2009-06-03 
14:28:39 UTC (rev 35621)
@@ -299,6 +299,7 @@
                 connection.setConnectTimeout(timeout);
                 final String etag = connection.getHeaderField("ETag");
                 if (etag != null) {
+                    log.debug("Found an etag header on " + uri + " " + etag);
                     final File etagFile = getETagFile(cacheFile);
                     if ( ! cacheFile.exists() || ! etagFile.exists() || ! 
etag.equals(readETag(etagFile))) {
                         renderWrappedAndFile(cacheFile, blockParameters, w, 
hints, new Runnable() {
@@ -315,11 +316,36 @@
                         renderFile(cacheFile, w);
                     }
                 } else {
+                    List<String> cacheControl = 
Arrays.asList(connection.getHeaderField("Cache-Control").toLowerCase().split("\\s*,\\s*"));
+                    if (cacheControl.contains("no-cache") || 
cacheControl.contains("no-store")) {
+                        log.warn("The response for " + uri + " cannot be 
implicitely cached (Because of Cache-Control: " + cacheControl + ") Use the 
'expires' parameter on " + this + " to override this, because it will _not_ be 
cached now.");
+                        getWraps().render(blockParameters, w, hints);
+                        return;
+                    }
+                    if (cacheControl.contains("must-revalidate")) {
+                        if (cacheFile.exists()) {
+                            log.debug("Server indicated that the cache must be 
revalidated");
+                            cacheFile.delete();
+                        }
+                    }
                     long modified = connection.getLastModified();
+                    if (modified == 0) {
+                        log.warn("No last-modified returned by " + uri + " 
taking it 5 minutes after last rendering. Consider using 'expires'. Cache 
control " + cacheControl);
+                        if (cacheFile.exists()) {
+                            modified = cacheFile.lastModified();
+                            long delay =  5 * 60 * 1000;
+                            if (modified + delay < System.currentTimeMillis()) 
{
+                                modified += delay;
+                            }
+                        }
+                    }
                     if (! cacheFile.exists() || (cacheFile.lastModified() < 
modified)) {
                         log.service("Rendering " + uri + " because " + 
cacheFile + " older than " + new Date(modified));
                         renderWrappedAndFile(cacheFile, blockParameters, w, 
hints, null);
                     } else {
+                        if (log.isDebugEnabled()) {
+                            log.debug("Serving cached file because 
modification time of " + uri + " (" + modified + ") before modification time of 
" + cacheFile + " (" + cacheFile.lastModified() + ")");
+                        }
                         renderFile(cacheFile, w);
                     }
                 }

_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to