Commit | Line | Data |
---|---|---|
e8aa5bf9 NR |
1 | package be.nikiroo.utils; |
2 | ||
3 | import java.io.IOException; | |
4 | import java.io.InputStream; | |
5 | import java.net.URL; | |
6 | import java.util.HashMap; | |
7 | import java.util.Map; | |
8 | ||
9 | /** | |
10 | * A memory only version of {@link Cache}. | |
11 | * | |
12 | * @author niki | |
13 | */ | |
14 | public class CacheMemory extends Cache { | |
15 | private Map<String, byte[]> data = new HashMap<String, byte[]>(); | |
16 | ||
17 | /** | |
18 | * Create a new {@link CacheMemory}. | |
19 | */ | |
20 | public CacheMemory() { | |
21 | } | |
22 | ||
23 | @Override | |
24 | public boolean check(String uniqueID, boolean allowTooOld, boolean stable) { | |
25 | return data.containsKey(getKey(uniqueID)); | |
26 | } | |
27 | ||
28 | @Override | |
29 | public boolean check(URL url, boolean allowTooOld, boolean stable) { | |
30 | return data.containsKey(getKey(url)); | |
31 | } | |
32 | ||
33 | @Override | |
34 | public int clean(boolean onlyOld) { | |
35 | int cleaned = 0; | |
36 | if (!onlyOld) { | |
37 | cleaned = data.size(); | |
38 | data.clear(); | |
39 | } | |
40 | ||
41 | return cleaned; | |
42 | } | |
43 | ||
44 | @Override | |
45 | public InputStream load(String uniqueID, boolean allowTooOld, boolean stable) { | |
46 | if (check(uniqueID, allowTooOld, stable)) { | |
47 | return load(uniqueID, allowTooOld, stable); | |
48 | } | |
49 | ||
50 | return null; | |
51 | } | |
52 | ||
53 | @Override | |
54 | public InputStream load(URL url, boolean allowTooOld, boolean stable) { | |
55 | if (check(url, allowTooOld, stable)) { | |
56 | return load(url, allowTooOld, stable); | |
57 | } | |
58 | ||
59 | return null; | |
60 | } | |
61 | ||
62 | @Override | |
63 | public boolean remove(String uniqueID) { | |
64 | return data.remove(getKey(uniqueID)) != null; | |
65 | } | |
66 | ||
67 | @Override | |
68 | public boolean remove(URL url) { | |
69 | return data.remove(getKey(url)) != null; | |
70 | } | |
71 | ||
72 | @Override | |
a09479d8 NR |
73 | public long save(InputStream in, String uniqueID) throws IOException { |
74 | byte[] bytes = IOUtils.toByteArray(in); | |
75 | data.put(getKey(uniqueID), bytes); | |
76 | return bytes.length; | |
e8aa5bf9 NR |
77 | } |
78 | ||
79 | @Override | |
a09479d8 NR |
80 | public long save(InputStream in, URL url) throws IOException { |
81 | byte[] bytes = IOUtils.toByteArray(in); | |
82 | data.put(getKey(url), bytes); | |
83 | return bytes.length; | |
e8aa5bf9 NR |
84 | } |
85 | ||
86 | /** | |
87 | * Return a key mapping to the given unique ID. | |
88 | * | |
89 | * @param uniqueID | |
90 | * the unique ID | |
91 | * | |
92 | * @return the key | |
93 | */ | |
94 | private String getKey(String uniqueID) { | |
95 | return "_/" + uniqueID; | |
96 | } | |
97 | ||
98 | /** | |
99 | * Return a key mapping to the given urm. | |
100 | * | |
101 | * @param url | |
102 | * thr url | |
103 | * | |
104 | * @return the key | |
105 | */ | |
106 | private String getKey(URL url) { | |
107 | return url.toString(); | |
108 | } | |
109 | } |