Permalink
Browse files

Turning on caching

  • Loading branch information...
1 parent 28acc64 commit 02aa0b4de520e26a0123a1a3eb60f1e15956760c @alexlod alexlod committed Apr 4, 2012
Showing with 65 additions and 2 deletions.
  1. +18 −1 pom.xml
  2. +47 −1 src/main/java/App.java
View
19 pom.xml
@@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
- <artifactId>helloworld</artifactId>
+ <artifactId>memcachier-fibonacci</artifactId>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
@@ -17,6 +17,12 @@
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
+ <dependency>
+ <groupId>spy</groupId>
+ <artifactId>spymemcached</artifactId>
+ <version>2.8.1</version>
+ <scope>provided</scope>
+ </dependency>
</dependencies>
<build>
<plugins>
@@ -34,4 +40,15 @@
</plugin>
</plugins>
</build>
+ <repositories>
+ <repository>
+ <id>spy</id>
+ <name>Spy Repository</name>
+ <layout>default</layout>
+ <url>http://files.couchbase.com/maven2/</url>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ </repository>
+ </repositories>
</project>
View
@@ -4,6 +4,11 @@
import javax.servlet.http.*;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.*;
+import net.spy.memcached.AddrUtil;
+import net.spy.memcached.MemcachedClient;
+import net.spy.memcached.ConnectionFactoryBuilder;
+import net.spy.memcached.auth.PlainCallbackHandler;
+import net.spy.memcached.auth.AuthDescriptor;
public class App extends HttpServlet {
@@ -12,12 +17,14 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Random generator = new Random();
int digit = generator.nextInt(30);
+ Result r = findOrComputeDigit(digit);
StringBuilder sb = new StringBuilder();
sb.append("<h1>MemCachier Fibonacci Example</h1>");
sb.append("<p>This script computes a random digit of the Fibonacci sequence. Before computing, though, it checks to see if there's a cached value for the digit and serves the cached value if there's a hit.</p>");
sb.append("<p>Digit: " + digit + "</p>");
- sb.append("<p>Value: " + fibDigit(digit) + "</p>");
+ sb.append("<p>Value: " + r.val + "</p>");
+ sb.append("<p>Was in cache? " + r.cached + "</p>");
resp.getWriter().print(sb.toString());
}
@@ -32,11 +39,50 @@ public static void main(String[] args) throws Exception{
server.join();
}
+ private static Result findOrComputeDigit(int n) {
+ AuthDescriptor ad = new AuthDescriptor(new String[]{"PLAIN"},
+ new PlainCallbackHandler(System.getenv("MEMCACHIER_USERNAME"),
+ System.getenv("MEMCACHIER_PASSWORD")));
+
+ try {
+ MemcachedClient mc = new MemcachedClient(
+ new ConnectionFactoryBuilder().setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
+ .setAuthDescriptor(ad)
+ .build(),
+ AddrUtil.getAddresses(System.getenv("MEMCACHIER_SERVERS") + ":11211"));
+ Object inCache = mc.get("" + n);
+ if (inCache == null) {
+ return new Result(computeAndSet(mc, n), false);
+ } else {
+ return new Result((Integer) inCache, true);
+ }
+ } catch (IOException ex) {
+ System.err.println("Couldn't create a connection, bailing out: \nIOException " + ex.getMessage());
+ return new Result(fibDigit(n), false);
+ }
+ }
+
+ private static int computeAndSet(MemcachedClient mc, int n) {
+ int val = fibDigit(n);
+ mc.set("" + n, 3600, val);
+ return val;
+ }
+
private static int fibDigit(int n) {
if (n < 2) {
return n;
} else {
return fibDigit(n - 1) + fibDigit(n - 2);
}
}
+
+ private static class Result {
+ public int val;
+ public boolean cached;
+
+ public Result(int val, boolean cached) {
+ this.val = val;
+ this.cached = cached;
+ }
+ }
}

0 comments on commit 02aa0b4

Please sign in to comment.