Author: lryan
Date: Fri Mar 13 22:30:27 2009
New Revision: 753457

URL: http://svn.apache.org/viewvc?rev=753457&view=rev
Log:
Minor improvements to invalidation
- Dont create an invalidation mark for uncacheable content
- Avoid reading the same mark from the invalidation cache if viewer == owner.

Modified:
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/DefaultInvalidationService.java
    
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/DefaultRequestPipeline.java
    
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/DefaultInvalidationServiceTest.java

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/DefaultInvalidationService.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/DefaultInvalidationService.java?rev=753457&r1=753456&r2=753457&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/DefaultInvalidationService.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/DefaultInvalidationService.java
 Fri Mar 13 22:30:27 2009
@@ -55,6 +55,7 @@
   protected final Cache<String,Long> invalidationEntries;
   private final AtomicLong marker;
 
+  private static final String TOKEN_PREFIX = "INV_TOK:";
 
   @Inject
   public DefaultInvalidationService(HttpCache httpCache, CacheProvider 
cacheProvider) {
@@ -125,9 +126,9 @@
     // Assume the container is consistent in its use of either appId or appUrl.
     // Use appId
     if (!StringUtils.isEmpty(token.getAppId())) {
-      return "INVALIDATION_TOKEN:" + token.getAppId() + ":" + userId;
+      return TOKEN_PREFIX + token.getAppId() + ":" + userId;
     }
-    return "INVALIDATION_TOKEN:" + token.getAppUrl() + ":" + userId;
+    return TOKEN_PREFIX + token.getAppUrl() + ":" + userId;
   }
 
   /**
@@ -136,21 +137,29 @@
   private String getInvalidationMark(HttpRequest request) {
     StringBuilder currentInvalidation = new StringBuilder();
 
+    Long ownerStamp = null;
     if (request.getOAuthArguments().getSignOwner()) {
       String ownerKey = getKey(request.getSecurityToken().getOwnerId(), 
request.getSecurityToken());
-      Long ownerStamp = invalidationEntries.getElement(ownerKey);
-      if (ownerStamp != null) {
-        currentInvalidation.append("o=").append(ownerStamp).append(";");
-      }
+      ownerStamp = invalidationEntries.getElement(ownerKey);
     }
+    Long viewerStamp = null;
     if (request.getOAuthArguments().getSignViewer()) {
-      String viewerKey = getKey(request.getSecurityToken().getViewerId(),
-          request.getSecurityToken());
-      Long viewerStamp = invalidationEntries.getElement(viewerKey);
-      if (viewerStamp != null) {
-        currentInvalidation.append("v=").append(viewerStamp).append(";");
+      if (ownerStamp != null &&
+          request.getSecurityToken().getOwnerId().equals(
+              request.getSecurityToken().getViewerId())) {
+        viewerStamp = ownerStamp;
+      } else {
+        String viewerKey = getKey(request.getSecurityToken().getViewerId(),
+            request.getSecurityToken());
+        viewerStamp = invalidationEntries.getElement(viewerKey);
       }
     }
+    if (ownerStamp != null) {
+      currentInvalidation.append("o=").append(ownerStamp).append(";");
+    }
+    if (viewerStamp != null) {
+      currentInvalidation.append("v=").append(viewerStamp).append(";");
+    }
     return currentInvalidation.toString();
   }
 

Modified: 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/DefaultRequestPipeline.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/DefaultRequestPipeline.java?rev=753457&r1=753456&r2=753457&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/DefaultRequestPipeline.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/http/DefaultRequestPipeline.java
 Fri Mar 13 22:30:27 2009
@@ -17,10 +17,10 @@
  */
 package org.apache.shindig.gadgets.http;
 
+import org.apache.shindig.common.util.Utf8UrlCoder;
 import org.apache.shindig.gadgets.GadgetException;
 import org.apache.shindig.gadgets.oauth.OAuthRequest;
 import org.apache.shindig.gadgets.rewrite.image.ImageRewriter;
-import org.apache.shindig.common.util.Utf8UrlCoder;
 
 import com.google.inject.Inject;
 import com.google.inject.Provider;
@@ -53,18 +53,19 @@
 
   public HttpResponse execute(HttpRequest request) throws GadgetException {
     normalizeProtocol(request);
-
-    HttpResponse cachedResponse = null;
+    HttpResponse invalidatedResponse = null;
 
     if (!request.getIgnoreCache()) {
-      cachedResponse = httpCache.getResponse(request);
+      HttpResponse cachedResponse = httpCache.getResponse(request);
       // Note that we dont remove invalidated entries from the cache as we 
want them to be
       // available in the event of a backend fetch failure
       if (cachedResponse != null) {
-        if (cachedResponse.isStale()) {
-          cachedResponse = null;
-        } else if(invalidationService.isValid(request, cachedResponse)) {
-          return cachedResponse;
+        if (!cachedResponse.isStale()) {
+          if(invalidationService.isValid(request, cachedResponse)) {
+            return cachedResponse;
+          } else {
+            invalidatedResponse = cachedResponse;
+          }
         }
       }
     }
@@ -82,19 +83,21 @@
         return HttpResponse.error();
     }
 
-    if (fetchedResponse.isError() && cachedResponse != null) {
-      // Use the invalidated cache response if it is not stale. We don't 
update its
+    if (fetchedResponse.isError() && invalidatedResponse != null) {
+      // Use the invalidated cached response if it is not stale. We don't 
update its
       // mark so it remains invalidated
-      return cachedResponse;
+      return invalidatedResponse;
     }
 
     if (!fetchedResponse.isError() && !request.getIgnoreCache() && 
request.getCacheTtl() != 0) {
       fetchedResponse = imageRewriter.rewrite(request.getUri(), 
fetchedResponse);
     }
 
-    if (!request.getIgnoreCache()) {
+    if (!request.getIgnoreCache() ) {
       // Mark the response with invalidation information prior to caching
-      fetchedResponse = invalidationService.markResponse(request, 
fetchedResponse);
+      if (fetchedResponse.getCacheTtl() > 0) {
+        fetchedResponse = invalidationService.markResponse(request, 
fetchedResponse);
+      }
       httpCache.addResponse(request, fetchedResponse);
     }
     return fetchedResponse;

Modified: 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/DefaultInvalidationServiceTest.java
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/DefaultInvalidationServiceTest.java?rev=753457&r1=753456&r2=753457&view=diff
==============================================================================
--- 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/DefaultInvalidationServiceTest.java
 (original)
+++ 
incubator/shindig/trunk/java/gadgets/src/test/java/org/apache/shindig/gadgets/http/DefaultInvalidationServiceTest.java
 Fri Mar 13 22:30:27 2009
@@ -26,7 +26,9 @@
 import org.apache.shindig.gadgets.rewrite.image.NoOpImageRewriter;
 
 import com.google.common.collect.ImmutableSet;
+
 import junit.framework.TestCase;
+
 import org.easymock.EasyMock;
 import org.easymock.IMocksControl;
 
@@ -100,13 +102,13 @@
         appyToken);
     
assertEquals(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME).getSize(),
 4);
     
assertNotNull(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME)
-        .getElement("INVALIDATION_TOKEN:AppX:1"));
+        .getElement("INV_TOK:AppX:1"));
     
assertNotNull(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME)
-        .getElement("INVALIDATION_TOKEN:AppX:2"));
+        .getElement("INV_TOK:AppX:2"));
     
assertNotNull(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME)
-        .getElement("INVALIDATION_TOKEN:AppY:1"));
+        .getElement("INV_TOK:AppY:1"));
     
assertNotNull(cacheProvider.createCache(DefaultInvalidationService.CACHE_NAME)
-        .getElement("INVALIDATION_TOKEN:AppY:2"));
+        .getElement("INV_TOK:AppY:2"));
 
   }
 


Reply via email to