@@ -50,6 +50,11 @@ trait Cache[-Key, +Error, +Value] {
50
50
*/
51
51
def get (key : Key ): IO [Error , Value ]
52
52
53
+ /**
54
+ * Invalidates the value associated with the specified key.
55
+ */
56
+ def invalidate (key : Key ): UIO [Unit ]
57
+
53
58
/**
54
59
* Returns the approximate number of values in the cache.
55
60
*/
@@ -114,6 +119,24 @@ object Cache {
114
119
115
120
new Cache [Key , Error , Value ] {
116
121
122
+ def cacheStats : UIO [CacheStats ] =
123
+ ZIO .succeed(CacheStats (hits.longValue, misses.longValue))
124
+
125
+ def contains (k : Key ): UIO [Boolean ] =
126
+ ZIO .succeed(map.containsKey(k))
127
+
128
+ def entryStats (k : Key ): UIO [Option [EntryStats ]] =
129
+ ZIO .succeed {
130
+ val value = map.get(k)
131
+ if (value eq null ) None
132
+ else {
133
+ value match {
134
+ case MapValue .Pending (_, _) => None
135
+ case MapValue .Complete (_, _, entryState) => Some (EntryStats (entryState.loaded))
136
+ }
137
+ }
138
+ }
139
+
117
140
def get (k : Key ): IO [Error , Value ] =
118
141
ZIO .effectSuspendTotal {
119
142
var key : MapKey [Key ] = null
@@ -160,26 +183,14 @@ object Cache {
160
183
161
184
}
162
185
163
- def contains (k : Key ): UIO [Boolean ] =
164
- ZIO .succeed(map.containsKey(k))
186
+ def invalidate (k : Key ): UIO [Unit ] =
187
+ ZIO .succeed {
188
+ map.remove(k)
189
+ ()
190
+ }
165
191
166
192
def size : UIO [Int ] =
167
193
ZIO .succeed(map.size)
168
-
169
- def cacheStats : UIO [CacheStats ] =
170
- ZIO .succeed(CacheStats (hits.longValue, misses.longValue))
171
-
172
- def entryStats (k : Key ): UIO [Option [EntryStats ]] =
173
- ZIO .succeed {
174
- val value = map.get(k)
175
- if (value eq null ) None
176
- else {
177
- value match {
178
- case MapValue .Pending (_, _) => None
179
- case MapValue .Complete (_, _, entryState) => Some (EntryStats (entryState.loaded))
180
- }
181
- }
182
- }
183
194
}
184
195
}
185
196
}
0 commit comments