Skip to content

Commit

Permalink
Fix cache invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
lincolnthree committed Jun 23, 2014
1 parent 28c7aa4 commit e4470c0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.Map;

import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
Expand All @@ -23,6 +25,7 @@
/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
@Stateless
@Path("/v1/webhooks")
@Produces({ "application/xml", "application/json" })
public class HooksIntegrationService
Expand Down Expand Up @@ -51,27 +54,35 @@ public Response githubUpdateRepository(String payload) throws Exception
}
else
{
try (CloseableHttpClient client = HttpClientBuilder.create().build())
try
{
Address address = AddressBuilder.begin().scheme("http").domain(SiteConstants.REDOCULOUS_DOMAIN)
.path("/api/v1/manage").query("repo", repo).build();

HttpResponse response = client.execute(new HttpPut(address.toString()));

if (response.getStatusLine().getStatusCode() != 200)
throw new IllegalStateException("failed! (server returned status code: "
+ response.getStatusLine().getStatusCode() + ")");
}
catch (Exception e)
{
return Response.status(Status.INTERNAL_SERVER_ERROR)
.entity("Failed to purge Redoculous cache for repo: " + repo).build();
invalidateRedoculous(repo);
}
finally
{
downloader.invalidateCachesByAddress(".*repo=" + repo + ".*");
downloader.invalidateCachesByAddress(repo);
}
}
return Response.status(Status.OK).build();
}

@Asynchronous
private void invalidateRedoculous(String repo)
{
try (CloseableHttpClient client = HttpClientBuilder.create().build())
{
Address address = AddressBuilder.begin().scheme("http").domain(SiteConstants.REDOCULOUS_DOMAIN)
.path("/api/v1/manage").query("repo", repo).build();

HttpResponse response = client.execute(new HttpPut(address.toString()));

if (response.getStatusLine().getStatusCode() != 200)
throw new IllegalStateException("failed! (server returned status code: "
+ response.getStatusLine().getStatusCode() + ")");
}
catch (Exception e)
{
throw new IllegalStateException("Failed to purge Redoculous cache for repo: " + repo, e);
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/org/jboss/forge/website/service/CacheEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ public String getAddress()
{
return address;
}

@Override
public String toString()
{
return "CacheEntry [address=" + address + ", time=" + time + "]";
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import javax.inject.Singleton;

Expand All @@ -18,6 +19,7 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.ocpsoft.common.util.Streams;
import org.ocpsoft.urlbuilder.util.Decoder;

/**
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
Expand Down Expand Up @@ -83,10 +85,13 @@ public void invalidateCaches()

public void invalidateCachesByAddress(String pattern)
{
pattern = ".*" + Pattern.quote(pattern) + ".*";
log.info("Invalidating caches for pattern [" + pattern + "]");
for (CacheEntry entry : cache.values())
{
if (entry.getAddress().matches(pattern))
if (entry.getAddress().matches(pattern)
|| Decoder.query(entry.getAddress()).matches(pattern)
|| Decoder.path(entry.getAddress()).matches(pattern))
{
log.info("Invalidating cache entry [" + entry.getAddress() + "] for pattern [" + pattern + "]");
entry.invalidate();
Expand Down

0 comments on commit e4470c0

Please sign in to comment.