Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add file-based caching for annotations - was taking waaaaay too long to
look them up from the hosting MN each time they were read. cache will be cleared when the app container (typically tomcat) is restarted.
- Loading branch information
1 parent
c21320e
commit 7034ca8
Showing
2 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/main/java/org/dataone/annotator/store/FileBasedCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.dataone.annotator.store; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
|
||
public class FileBasedCache { | ||
|
||
private static Map<String, File> cache = new HashMap<String, File>(); | ||
|
||
public static void cache(String id, InputStream is) throws IOException { | ||
if (!cache.containsKey(id)) { | ||
File file = File.createTempFile("annotator_cache", ".dat"); | ||
file.deleteOnExit(); | ||
IOUtils.copy(is, new FileOutputStream(file)); | ||
cache.put(id, file); | ||
} | ||
} | ||
|
||
public static InputStream get(String id) throws FileNotFoundException { | ||
|
||
if (cache.containsKey(id)) { | ||
File file = cache.get(id); | ||
return new FileInputStream(file); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static boolean remove(String id) { | ||
if (cache.containsKey(id)) { | ||
File file = cache.get(id); | ||
file.delete(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public static void clear() { | ||
for (File file: cache.values()) { | ||
file.delete(); | ||
} | ||
cache.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters