1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package nl.hippo.client.caching;
17
18 import java.io.IOException;
19 import java.io.Serializable;
20 import java.util.Iterator;
21
22 import nl.hippo.client.api.caching.Cache;
23 import nl.hippo.client.api.caching.CachedResponse;
24 import nl.hippo.client.api.caching.EventAwareCache;
25 import nl.hippo.client.api.caching.SourceValidity;
26 import nl.hippo.client.api.event.EventValidity;
27 import nl.hippo.client.api.event.NamedEvent;
28
29 public class EventCache implements EventAwareCache {
30
31 private EventCacheConfig config;
32 private EventRegistry eventRegistry;
33
34 public EventCache(EventCacheConfig config) {
35 this.config = config;
36 this.eventRegistry = new EventRegistry();
37 }
38
39 /**
40 * Implementation of nl.hippo.client.event.api.EventAware
41 */
42 public void processEvent(NamedEvent e) {
43 if (e != null) {
44 Serializable[] keys = eventRegistry.keysForEvent(e);
45 if (keys == null)
46 return;
47 for (int i = 0; i < keys.length; i++) {
48 if (keys[i] != null) {
49
50
51
52
53
54 getDelegate().remove(keys[i]);
55 }
56 }
57 }
58 }
59
60 public Object get(Object key) {
61 Object obj = getDelegate().get(key);
62 if (obj != null && obj instanceof CachedResponse) {
63 Object cachedObj = ((CachedResponse) obj).getResponse();
64 return cachedObj;
65 }
66 return obj;
67 }
68
69 public void store(Object key, Object value) throws IOException {
70 if (value instanceof CachedResponse) {
71 CachedResponse cr = (CachedResponse) value;
72 SourceValidity[] srcvals = cr.getValidityObjects();
73 for (int i = 0; i < srcvals.length; i++) {
74 if (srcvals[i] instanceof EventValidity) {
75 EventValidity validity = (EventValidity) srcvals[i];
76 NamedEvent event = validity.getEvent();
77 eventRegistry.register(event, key);
78 }
79 }
80 }
81 getDelegate().store(key, value);
82 }
83
84 public void remove(Object key) {
85 getDelegate().remove(key);
86 }
87
88 public void clear() {
89 getDelegate().clear();
90 }
91
92 public boolean containsKey(Object key) {
93 return getDelegate().containsKey(key);
94 }
95
96 public Iterator memoryKeys() {
97 return getDelegate().memoryKeys();
98 }
99
100 public int memorySize() {
101 return getDelegate().memorySize();
102 }
103
104 public String getStatisticsOverview() {
105 return getDelegate().getStatisticsOverview();
106 }
107
108 private Cache getDelegate() {
109 return config.getDelegate();
110 }
111
112 }