1 package be
.nikiroo
.utils
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
6 import java
.util
.HashMap
;
10 * A memory only version of {@link Cache}.
14 public class CacheMemory
extends Cache
{
15 private Map
<String
, byte[]> data
= new HashMap
<String
, byte[]>();
18 * Create a new {@link CacheMemory}.
20 public CacheMemory() {
24 public boolean check(String uniqueID
, boolean allowTooOld
, boolean stable
) {
25 return data
.containsKey(getKey(uniqueID
));
29 public boolean check(URL url
, boolean allowTooOld
, boolean stable
) {
30 return data
.containsKey(getKey(url
));
34 public int clean(boolean onlyOld
) {
37 cleaned
= data
.size();
45 public InputStream
load(String uniqueID
, boolean allowTooOld
, boolean stable
) {
46 if (check(uniqueID
, allowTooOld
, stable
)) {
47 return load(uniqueID
, allowTooOld
, stable
);
54 public InputStream
load(URL url
, boolean allowTooOld
, boolean stable
) {
55 if (check(url
, allowTooOld
, stable
)) {
56 return load(url
, allowTooOld
, stable
);
63 public boolean remove(String uniqueID
) {
64 return data
.remove(getKey(uniqueID
)) != null;
68 public boolean remove(URL url
) {
69 return data
.remove(getKey(url
)) != null;
73 public void save(InputStream in
, String uniqueID
) throws IOException
{
74 data
.put(getKey(uniqueID
), IOUtils
.toByteArray(in
));
78 public void save(InputStream in
, URL url
) throws IOException
{
79 data
.put(getKey(url
), IOUtils
.toByteArray(in
));
83 * Return a key mapping to the given unique ID.
90 private String
getKey(String uniqueID
) {
91 return "_/" + uniqueID
;
95 * Return a key mapping to the given urm.
102 private String
getKey(URL url
) {
103 return url
.toString();