1 | |
package org.webmacro.resource; |
2 | |
|
3 | |
import java.util.Map; |
4 | |
|
5 | |
import org.slf4j.Logger; |
6 | |
import org.slf4j.LoggerFactory; |
7 | |
|
8 | |
import org.webmacro.Broker; |
9 | |
import org.webmacro.InitException; |
10 | |
import org.webmacro.ResourceException; |
11 | |
import org.webmacro.util.Settings; |
12 | |
|
13 | |
import java.util.concurrent.ConcurrentHashMap; |
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
public class SimpleCacheManager implements CacheManager { |
22 | |
|
23 | 40 | static Logger _log = LoggerFactory.getLogger(SimpleCacheManager.class); |
24 | |
|
25 | |
private static final String NAME = "SimpleCacheManager"; |
26 | 118 | private final Map _cache = new ConcurrentHashMap(); |
27 | |
|
28 | |
private String _resourceType; |
29 | |
|
30 | 118 | public SimpleCacheManager() { |
31 | 118 | } |
32 | |
|
33 | |
public void init(Broker b, Settings config, String resourceType) |
34 | |
throws InitException { |
35 | |
|
36 | 118 | _resourceType = resourceType; |
37 | |
|
38 | 118 | _log.info(NAME + "." + _resourceType); |
39 | 118 | } |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public void flush() { |
45 | 0 | _cache.clear(); |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public void destroy() { |
52 | 0 | _cache.clear(); |
53 | 0 | } |
54 | |
|
55 | |
public boolean supportsReload() { |
56 | 0 | return false; |
57 | |
} |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
public Object get(final Object query, ResourceLoader helper) |
65 | |
throws ResourceException { |
66 | 6943 | Object o = null; |
67 | |
|
68 | 6943 | o = _cache.get(query); |
69 | 6943 | if (o == null) { |
70 | 219 | o = helper.load(query, null); |
71 | 219 | _cache.put(query, o); |
72 | |
} |
73 | 6943 | return o; |
74 | |
} |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
public Object get(final Object query) { |
81 | 2298 | return _cache.get(query); |
82 | |
} |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
public void put(final Object query, Object resource) { |
88 | 58 | _cache.put(query, resource); |
89 | 58 | } |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public void invalidate(final Object query) { |
95 | 0 | _cache.remove(query); |
96 | 0 | } |
97 | |
|
98 | |
public String toString() { |
99 | 0 | return NAME + "(type = " + _resourceType + ")"; |
100 | |
} |
101 | |
} |