1 | package net.spy.memcached; |
2 | |
3 | import java.util.Collection; |
4 | import java.util.Collections; |
5 | import java.util.Map; |
6 | import java.util.Set; |
7 | |
8 | import net.spy.memcached.transcoders.Transcoder; |
9 | |
10 | /** |
11 | * Base class for a Map interface to memcached. |
12 | * |
13 | * <p> |
14 | * This Map interface makes memcached a bit easier to use for some purposes |
15 | * by providing a limited Map implementation. |
16 | * </p> |
17 | * |
18 | * <p> |
19 | * Do note that nothing that iterates over the map will work (such is |
20 | * memcached). All iteration mechanisms will return empty iterators and |
21 | * such. |
22 | * </p> |
23 | * |
24 | * @param <V> the type of value taken and returned by this Map's underlying |
25 | * transcoder, and thus taken and returned by this Map. |
26 | */ |
27 | public class BaseCacheMap<V> implements Map<String, V> { |
28 | |
29 | private final String keyPrefix; |
30 | private final Transcoder<V> transcoder; |
31 | private final MemcachedClientIF client; |
32 | private final int exp; |
33 | |
34 | /** |
35 | * Build a BaseCacheMap. |
36 | * |
37 | * @param c the underlying client |
38 | * @param expiration the expiration for objects set through this Map |
39 | * @param prefix a prefix to ensure objects in this map are unique |
40 | * @param t the transcoder to serialize and deserialize objects |
41 | */ |
42 | public BaseCacheMap(MemcachedClientIF c, int expiration, |
43 | String prefix, Transcoder<V> t) { |
44 | super(); |
45 | keyPrefix = prefix; |
46 | transcoder = t; |
47 | client = c; |
48 | exp = expiration; |
49 | } |
50 | |
51 | public void clear() { |
52 | // TODO: Support a rolling key generation. |
53 | throw new UnsupportedOperationException(); |
54 | } |
55 | |
56 | private String getKey(String k) { |
57 | return keyPrefix + k; |
58 | } |
59 | |
60 | public boolean containsKey(Object key) { |
61 | return get(key) != null; |
62 | } |
63 | |
64 | /** |
65 | * This method always returns false, as truth cannot be determined without |
66 | * iteration. |
67 | */ |
68 | public boolean containsValue(Object value) { |
69 | return false; |
70 | } |
71 | |
72 | public Set<Map.Entry<String, V>> entrySet() { |
73 | return Collections.emptySet(); |
74 | } |
75 | |
76 | public V get(Object key) { |
77 | V rv = null; |
78 | try { |
79 | rv = client.get(getKey((String)key), transcoder); |
80 | } catch(ClassCastException e) { |
81 | // Most likely, this is because the key wasn't a String. |
82 | // Either way, it's a no. |
83 | } |
84 | return rv; |
85 | } |
86 | |
87 | public boolean isEmpty() { |
88 | return false; |
89 | } |
90 | |
91 | public Set<String> keySet() { |
92 | return Collections.emptySet(); |
93 | } |
94 | |
95 | public void putAll(Map<? extends String, ? extends V> t) { |
96 | for(Map.Entry<? extends String, ? extends V> me : t.entrySet()) { |
97 | client.set(getKey(me.getKey()), exp, me.getValue()); |
98 | } |
99 | } |
100 | |
101 | public V remove(Object key) { |
102 | V rv = null; |
103 | try { |
104 | rv = get(key); |
105 | client.delete(getKey((String)key)); |
106 | } catch(ClassCastException e) { |
107 | // Not a string key. Ignore. |
108 | } |
109 | return rv; |
110 | } |
111 | |
112 | public int size() { |
113 | return 0; |
114 | } |
115 | |
116 | public Collection<V> values() { |
117 | return Collections.emptySet(); |
118 | } |
119 | |
120 | public V put(String key, V value) { |
121 | V rv = get(key); |
122 | client.set(getKey(key), exp, value); |
123 | return rv; |
124 | } |
125 | |
126 | } |