[CVS .] Implemented caching
Jon Tirsen <tirsen-yCVjj/[email protected]> Wed, 25 Feb 2004 16:22:03 -0600
| Newsgroups | gmane.comp.java.nanocontainer.cvs |
|---|---|
| Message-ID | <[email protected]> |
Commit in ./snippet on MAIN
src/main/snippet/SnippetMacro.java +55 -1 1.1.1.1 -> 1.2
src/test/snippet/SnippetMacroTest.java +25 -2 1.2 -> 1.3
build.xml +1 -1 1.1.1.1 -> 1.2
+81 -4
3 modified files
Implemented caching
----------
. /snippet /src /main /snippet
SnippetMacro.java 1.1.1.1 -> 1.2
diff -u -r1.1.1.1 -r1.2
--- SnippetMacro.java 25 Feb 2004 20:09:16 -0000 1.1.1.1
+++ SnippetMacro.java 25 Feb 2004 22:22:03 -0000 1.2
@@ -7,9 +7,14 @@
import java.io.Writer;
import java.net.URL;
import java.net.MalformedURLException;
+import java.util.Map;
+import java.util.HashMap;
public class SnippetMacro extends BaseMacro {
static final String EOL = System.getProperty("line.separator");
+ private Map cache = new HashMap();
+ private long timeout = 60 * 60 * 1000; // one hour default cache
+ private Map timeCached = new HashMap();
public String getName() {
return "snippet";
@@ -28,7 +33,7 @@
}
String lang = parameter.get("lang");
- StringBuffer snippet = new SnippetReader(url).readSnippet(id);
+ StringBuffer snippet = getSnippet(url, id);
if(lang != null) {
snippet.insert(0, EOL);
@@ -39,9 +44,58 @@
writer.write(snippet.toString());
}
+ StringBuffer getSnippet(URL url, String id) throws IOException {
+ String cachedSnippet = (String) getCachedSnippet(url, id);
+ if(cachedSnippet != null) {
+ return new StringBuffer(cachedSnippet);
+ }
+
+ StringBuffer snippet = new SnippetReader(url).readSnippet(id);
+ return snippet;
+ }
+
+ private Object getCachedSnippet(URL url, String id) {
+ if(isCacheTimedout(url, id)) {
+ removeFromCache(url, id);
+ }
+ return cache.get(globalSnippetId(url, id));
+ }
+
+ boolean isCacheTimedout(URL url, String id) {
+ return timeInCache(url, id) >= timeout;
+ }
+
+ long timeInCache(URL url, String id) {
+ return System.currentTimeMillis() - getTimeCached(url, id);
+ }
+
+ long getTimeCached(URL url, String id) {
+ String globalId = globalSnippetId(url, id);
+ return timeCached.containsKey(globalId) ? ((Long) timeCached.get(globalId)).longValue() : 0;
+ }
+
+ private void removeFromCache(URL url, String id) {
+ String globalId = globalSnippetId(url, id);
+ timeCached.remove(globalId);
+ cache.remove(globalId);
+ }
+
+ private String globalSnippetId(URL url, String id) {
+ return url + " " + id;
+ }
+
private void required(String id, String param) {
if (id == null || "".equals(id)) {
throw new IllegalArgumentException(param + " is a required parameter");
}
+ }
+
+ public void cacheSnippet(URL url, String id, String content) {
+ cache.put(globalSnippetId(url, id), content);
+ timeCached.put(globalSnippetId(url, id), new Long(System.currentTimeMillis()));
+ }
+
+ public void setCacheTimeout(int timeout) {
+ this.timeout = timeout;
}
}
----------
. /snippet /src /test /snippet
SnippetMacroTest.java 1.2 -> 1.3
diff -u -r1.2 -r1.3
--- SnippetMacroTest.java 25 Feb 2004 21:47:01 -0000 1.2
+++ SnippetMacroTest.java 25 Feb 2004 22:22:03 -0000 1.3
@@ -3,9 +3,9 @@
import junit.framework.TestCase;
import org.radeox.macro.parameter.BaseMacroParameter;
-import java.net.URL;
-import java.io.StringWriter;
import java.io.IOException;
+import java.io.StringWriter;
+import java.net.URL;
public class SnippetMacroTest extends TestCase {
static final String EOL = System.getProperty("line.separator");
@@ -53,6 +53,29 @@
public void TODOtestQuotesStartCurlyBracket() {
}
+
+ public void testReturnsCachedSnippetIfSnippetIsInCache() throws IOException {
+ URL url = new URL("file:cachedUrl");
+ String id = "snippet id";
+ String content = "cached content";
+ snippetMacro.cacheSnippet(url, id, content);
+ assertEquals(content, snippetMacro.getSnippet(url, id).toString());
+ }
+
+ public void testRemovesSnippetFromCacheWhenTimedOut() throws IOException {
+ assertEquals("assertEquals(2, 1 + 1);" + EOL,
+ snippetMacro.getSnippet(snippetUrl(), "mySnippetId").toString());
+ snippetMacro.cacheSnippet(snippetUrl(), "mySnippetId", "different cache content");
+ assertEquals(System.currentTimeMillis(), snippetMacro.getTimeCached(snippetUrl(), "mySnippetId"));
+ assertFalse(snippetMacro.isCacheTimedout(snippetUrl(), "mySnippetId"));
+ assertEquals("different cache content",
+ snippetMacro.getSnippet(snippetUrl(), "mySnippetId").toString());
+ snippetMacro.setCacheTimeout(0);
+ assertTrue(snippetMacro.isCacheTimedout(snippetUrl(), "mySnippetId"));
+ assertEquals("assertEquals(2, 1 + 1);" + EOL,
+ snippetMacro.getSnippet(snippetUrl(), "mySnippetId").toString());
+ }
+
public void TODOtestCachesResults() {
----------
. /snippet
build.xml 1.1.1.1 -> 1.2
diff -u -r1.1.1.1 -r1.2
--- build.xml 25 Feb 2004 20:09:15 -0000 1.1.1.1
+++ build.xml 25 Feb 2004 22:22:03 -0000 1.2
@@ -1,5 +1,5 @@
<project name="snippet" default="jar">
- <property name="version" value="0.1" />
+ <property name="version" value="0.2" />
<property name="confluence.home" value="c:\confluence" />
<target name="jar">