1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.apache.commons.cache; |
17 | |
|
18 | |
import java.io.Serializable; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.HashSet; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | 0 | public class GroupMapImpl extends BaseStorageListener implements GroupMap, StorageListener { |
28 | |
|
29 | 0 | protected Cache _cache = null; |
30 | |
|
31 | 0 | protected HashMap _groupToKeys = new HashMap(); |
32 | |
|
33 | 0 | protected HashMap _keyToGroup = new HashMap(); |
34 | |
|
35 | |
|
36 | |
public synchronized Serializable[] getKeysForGroup(Serializable group) { |
37 | |
try { |
38 | 0 | HashSet set = (HashSet)(_groupToKeys.get(group)); |
39 | 0 | return (Serializable[])(set.toArray(new Serializable[0])); |
40 | 0 | } catch(Exception e) { |
41 | 0 | return new Serializable[0]; |
42 | |
} |
43 | |
} |
44 | |
|
45 | |
|
46 | |
public void setCache(Cache c) { |
47 | 0 | if(null != _cache) { |
48 | 0 | Object mutex = _cache; |
49 | 0 | synchronized(mutex) { |
50 | 0 | synchronized(c) { |
51 | 0 | synchronized(this) { |
52 | 0 | unsetCache(); |
53 | 0 | _cache = c; |
54 | 0 | _cache.registerStorageListener(this); |
55 | 0 | } |
56 | 0 | } |
57 | 0 | } |
58 | 0 | } else { |
59 | 0 | synchronized(c) { |
60 | 0 | synchronized(this) { |
61 | 0 | _cache = c; |
62 | 0 | _cache.registerStorageListener(this); |
63 | 0 | } |
64 | 0 | } |
65 | |
} |
66 | 0 | } |
67 | |
|
68 | |
|
69 | |
public void unsetCache() { |
70 | 0 | if(null != _cache) { |
71 | 0 | Object mutex = _cache; |
72 | 0 | synchronized(mutex) { |
73 | 0 | synchronized(this) { |
74 | 0 | _cache.unregisterStorageListener(this); |
75 | 0 | clear(); |
76 | 0 | _cache = null; |
77 | 0 | } |
78 | 0 | } |
79 | |
} |
80 | 0 | } |
81 | |
|
82 | |
public synchronized void stored(Serializable key, Serializable val, Long expiresAt, Long cost, Serializable group) { |
83 | 0 | if(null == group) { |
84 | 0 | Object oldgroup = _keyToGroup.get(key); |
85 | 0 | if(null != oldgroup) { |
86 | 0 | HashSet oldset = (HashSet)(_groupToKeys.get(oldgroup)); |
87 | 0 | if(null != oldset) { |
88 | 0 | oldset.remove(key); |
89 | |
} |
90 | |
} |
91 | 0 | } else { |
92 | 0 | HashSet set = (HashSet)(_groupToKeys.get(group)); |
93 | 0 | if(null != set) { |
94 | 0 | set.add(key); |
95 | |
} else { |
96 | 0 | set = new HashSet(); |
97 | 0 | set.add(key); |
98 | 0 | _groupToKeys.put(group,set); |
99 | |
} |
100 | 0 | Object oldgroup = _keyToGroup.put(key,group); |
101 | 0 | if(null != oldgroup && !(oldgroup.equals(group))) { |
102 | 0 | HashSet oldset = (HashSet)(_groupToKeys.get(oldgroup)); |
103 | 0 | if(null != oldset) { |
104 | 0 | oldset.remove(key); |
105 | |
} |
106 | |
} |
107 | |
} |
108 | 0 | } |
109 | |
|
110 | |
public synchronized void cleared(Serializable key) { |
111 | 0 | Serializable group = (Serializable)(_keyToGroup.remove(key)); |
112 | 0 | if(null != group) { |
113 | 0 | HashSet set = (HashSet)(_groupToKeys.get(group)); |
114 | 0 | set.remove(key); |
115 | |
} |
116 | 0 | } |
117 | |
|
118 | |
public void cleared() { |
119 | 0 | clear(); |
120 | 0 | } |
121 | |
|
122 | |
protected synchronized void clear() { |
123 | 0 | _groupToKeys.clear(); |
124 | 0 | _keyToGroup.clear(); |
125 | 0 | } |
126 | |
|
127 | |
public static void main(String[] args) { |
128 | 0 | GroupMapImpl tm = new GroupMapImpl(); |
129 | 0 | tm.stored("key1","val",null,null,"group1"); |
130 | 0 | tm.stored("key2","val",null,null,"group1"); |
131 | 0 | tm.stored("key3","val",null,null,"group1"); |
132 | 0 | tm.stored("key4","val",null,null,"group1"); |
133 | 0 | Serializable[] keys = tm.getKeysForGroup("group1"); |
134 | 0 | for(int i=0;i<keys.length;i++) { |
135 | 0 | System.out.println(keys[i]); |
136 | |
} |
137 | 0 | } |
138 | |
} |