1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.jcs3.jcache;
20
21 import java.util.Collections;
22 import java.util.Map;
23
24 import javax.cache.Cache;
25 import javax.cache.configuration.CacheEntryListenerConfiguration;
26 import javax.cache.event.EventType;
27
28 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
29 import org.apache.commons.jcs3.engine.behavior.ICompositeCacheAttributes;
30 import org.apache.commons.jcs3.engine.behavior.IElementAttributes;
31 import org.apache.commons.jcs3.engine.control.CompositeCache;
32
33
34 public class ExpiryAwareCache<A, B> extends CompositeCache<A, B>
35 {
36 private Map<CacheEntryListenerConfiguration<A, B>, JCSListener<A, B>> listeners;
37 private Cache<A, B> cacheRef;
38
39 ExpiryAwareCache(final ICompositeCacheAttributes cattr, final IElementAttributes attr)
40 {
41 super(cattr, attr);
42 }
43
44 @Override
45 protected void doExpires(final ICacheElement<A, B> element)
46 {
47 super.doExpires(element);
48 for (final JCSListener<A, B> listener : listeners.values())
49 {
50 listener.onExpired(Collections.singletonList(new JCSCacheEntryEvent<>(
51 cacheRef, EventType.REMOVED, null, element.getKey(), element.getVal())));
52 }
53 }
54
55 void init(final Cache<A, B> cache, final Map<CacheEntryListenerConfiguration<A, B>, JCSListener<A, B>> listeners)
56 {
57 this.cacheRef = cache;
58 this.listeners = listeners;
59 }
60 }