Mercurial Hosting > luan
comparison src/goodjava/util/CacheMap.java @ 1607:fa066aaa068c
nginx caching
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 30 Apr 2021 20:23:28 -0600 |
parents | 1b809d2fdf03 |
children |
comparison
equal
deleted
inserted
replaced
1606:7c7f28c724e8 | 1607:fa066aaa068c |
---|---|
32 if( mappedRef != ref && mappedRef != null ) | 32 if( mappedRef != ref && mappedRef != null ) |
33 cache.put( mappedRef.key(), mappedRef ); // put it back | 33 cache.put( mappedRef.key(), mappedRef ); // put it back |
34 } | 34 } |
35 } | 35 } |
36 | 36 |
37 public int size() { | 37 @Override public int size() { |
38 return cache.size(); | 38 return cache.size(); |
39 } | 39 } |
40 | 40 |
41 public boolean isEmpty() { | 41 @Override public boolean isEmpty() { |
42 return cache.isEmpty(); | 42 return cache.isEmpty(); |
43 } | 43 } |
44 | 44 |
45 public boolean containsKey(Object key) { | 45 @Override public boolean containsKey(Object key) { |
46 return cache.containsKey(key); | 46 return cache.containsKey(key); |
47 } | 47 } |
48 | 48 |
49 public V get(Object key) { | 49 @Override public V get(Object key) { |
50 MyReference<K,V> ref = cache.get(key); | 50 MyReference<K,V> ref = cache.get(key); |
51 return ref==null ? null : ref.get(); | 51 return ref==null ? null : ref.get(); |
52 } | 52 } |
53 | 53 |
54 public V put(K key,V value) { | 54 @Override public V put(K key,V value) { |
55 sweep(); | 55 sweep(); |
56 MyReference<K,V> ref = cache.put( key, newReference(key,value,queue) ); | 56 MyReference<K,V> ref = cache.put( key, newReference(key,value,queue) ); |
57 return ref==null ? null : ref.get(); | 57 return ref==null ? null : ref.get(); |
58 } | 58 } |
59 | 59 |
60 public V remove(Object key) { | 60 @Override public V remove(Object key) { |
61 sweep(); | 61 sweep(); |
62 MyReference<K,V> ref = cache.remove(key); | 62 MyReference<K,V> ref = cache.remove(key); |
63 return ref==null ? null : ref.get(); | 63 return ref==null ? null : ref.get(); |
64 } | 64 } |
65 | 65 |
66 public void clear() { | 66 @Override public void clear() { |
67 sweep(); | 67 sweep(); |
68 cache.clear(); | 68 cache.clear(); |
69 } | 69 } |
70 | 70 |
71 /* | 71 /* |
73 GCCacheMap map = new GCCacheMap(); | 73 GCCacheMap map = new GCCacheMap(); |
74 map.cache = (HashMap)cache.clone(); | 74 map.cache = (HashMap)cache.clone(); |
75 return map; | 75 return map; |
76 } | 76 } |
77 */ | 77 */ |
78 public Set<K> keySet() { | 78 @Override public Set<K> keySet() { |
79 return cache.keySet(); | 79 return cache.keySet(); |
80 } | 80 } |
81 | 81 |
82 public Set<Map.Entry<K,V>> entrySet() { | 82 @Override public Set<Map.Entry<K,V>> entrySet() { |
83 return new MySet(); | 83 return new MySet(); |
84 } | 84 } |
85 | 85 |
86 | 86 |
87 private class MySet extends AbstractSet<Map.Entry<K,V>> { | 87 private class MySet extends AbstractSet<Map.Entry<K,V>> { |
88 | 88 |
89 public int size() { | 89 @Override public int size() { |
90 return CacheMap.this.size(); | 90 return CacheMap.this.size(); |
91 } | 91 } |
92 | 92 |
93 public Iterator<Map.Entry<K,V>> iterator() { | 93 @Override public Iterator<Map.Entry<K,V>> iterator() { |
94 return new MyIterator(cache.entrySet().iterator()); | 94 return new MyIterator(); |
95 } | 95 } |
96 | 96 |
97 } | 97 } |
98 | 98 |
99 private class MyIterator implements Iterator<Map.Entry<K,V>> { | 99 private class MyIterator implements Iterator<Map.Entry<K,V>> { |
100 Iterator<Map.Entry<K,MyReference<K,V>>> iter; | 100 final Iterator<Map.Entry<K,MyReference<K,V>>> iter = cache.entrySet().iterator(); |
101 | 101 |
102 MyIterator(Iterator<Map.Entry<K,MyReference<K,V>>> iter) { | 102 @Override public boolean hasNext() { |
103 this.iter = iter; | |
104 } | |
105 | |
106 public boolean hasNext() { | |
107 return iter.hasNext(); | 103 return iter.hasNext(); |
108 } | 104 } |
109 | 105 |
110 public void remove() { | 106 @Override public void remove() { |
111 iter.remove(); | 107 iter.remove(); |
112 } | 108 } |
113 | 109 |
114 public Map.Entry<K,V> next() { | 110 @Override public Map.Entry<K,V> next() { |
115 return new MyEntry( iter.next() ); | 111 return new MyEntry( iter.next() ); |
116 } | 112 } |
117 } | 113 } |
118 | 114 |
119 private class MyEntry implements Map.Entry<K,V> { | 115 private class MyEntry implements Map.Entry<K,V> { |
121 | 117 |
122 MyEntry(Map.Entry<K,MyReference<K,V>> entry) { | 118 MyEntry(Map.Entry<K,MyReference<K,V>> entry) { |
123 this.entry = entry; | 119 this.entry = entry; |
124 } | 120 } |
125 | 121 |
126 public K getKey() { | 122 @Override public K getKey() { |
127 return entry.getKey(); | 123 return entry.getKey(); |
128 } | 124 } |
129 | 125 |
130 public V getValue() { | 126 @Override public V getValue() { |
131 MyReference<K,V> ref = entry.getValue(); | 127 MyReference<K,V> ref = entry.getValue(); |
132 return ref.get(); | 128 return ref.get(); |
133 } | 129 } |
134 | 130 |
135 public V setValue(V value) { | 131 @Override public V setValue(V value) { |
136 MyReference<K,V> ref = entry.setValue( newReference(getKey(),value,queue) ); | 132 MyReference<K,V> ref = entry.setValue( newReference(getKey(),value,queue) ); |
137 return ref.get(); | 133 return ref.get(); |
138 } | 134 } |
139 | 135 |
140 public boolean equals(Object o) { | 136 @Override public boolean equals(Object o) { |
141 if( o==null || !(o instanceof CacheMap.MyEntry) ) | 137 if( o==null || !(o instanceof CacheMap.MyEntry) ) |
142 return false; | 138 return false; |
143 @SuppressWarnings("unchecked") | 139 @SuppressWarnings("unchecked") |
144 MyEntry m = (MyEntry)o; | 140 MyEntry m = (MyEntry)o; |
145 return entry.equals(m.entry); | 141 return entry.equals(m.entry); |
146 } | 142 } |
147 | 143 |
148 public int hashCode() { | 144 @Override public int hashCode() { |
149 K key = getKey(); | 145 K key = getKey(); |
150 V value = getValue(); | 146 V value = getValue(); |
151 return (key==null ? 0 : key.hashCode()) ^ | 147 return (key==null ? 0 : key.hashCode()) ^ |
152 (value==null ? 0 : value.hashCode()); | 148 (value==null ? 0 : value.hashCode()); |
153 } | 149 } |