[CVS .] Implemented caching

Jon Tirsen <tirsen-yCVjj/[email protected]> Wed, 25 Feb 2004 16:35:51 -0600
Newsgroups gmane.comp.java.nanocontainer.cvs
Message-ID <[email protected]>
Commit in ./snippet/src/main/snippet on MAIN

SnippetMacro.java +14 -4 1.2 -> 1.3

Implemented caching

----------

. /snippet /src /main /snippet

SnippetMacro.java 1.2 -> 1.3

diff -u -r1.2 -r1.3
--- SnippetMacro.java 25 Feb 2004 22:22:03 -0000 1.2
+++ SnippetMacro.java 25 Feb 2004 22:35:51 -0000 1.3
@@ -15,6 +15,7 @@

private Map cache = new HashMap();
private long timeout = 60 * 60 * 1000; // one hour default cache
private Map timeCached = new HashMap();

+ private boolean debug = false;

public String getName() {
return "snippet";

@@ -45,13 +46,22 @@

}

StringBuffer getSnippet(URL url, String id) throws IOException {

+ StringBuffer result;

String cachedSnippet = (String) getCachedSnippet(url, id);
if(cachedSnippet != null) {

- return new StringBuffer(cachedSnippet);

+ result = new StringBuffer(cachedSnippet);
+ if (debug) {
+ result.append("(Served from cache)");
+ }

}

-
- StringBuffer snippet = new SnippetReader(url).readSnippet(id);
- return snippet;

+ else {
+ result = new SnippetReader(url).readSnippet(id);
+ cacheSnippet(url, id, result.toString());
+ if (debug) {
+ result.append("(Fetched from url, cache content " + cache + ")");
+ }
+ }
+ return result;

}

private Object getCachedSnippet(URL url, String id) {