@@ -136,32 +136,6 @@ func (c *Cache) Get(key interface{}) (value interface{}, closer io.Closer, exist
136
136
}
137
137
}
138
138
139
- // Pop evicts and returns the oldest key and value. If LFU ordering is
140
- // enabled, then the least frequently used key and value.
141
- //
142
- // Pop is a non-blocking operation.
143
- func (c * Cache ) Pop () (key , value interface {}) {
144
- c .mu .Lock ()
145
- defer c .mu .Unlock ()
146
- if key = c .list .Pop (); key == nil {
147
- return nil , nil
148
- }
149
- value , ok := c .evictLocked (key )
150
- if ! ok {
151
- panic ("evcache: invalid map state" )
152
- }
153
- return key , value
154
- }
155
-
156
- // Evict evicts a key and returns its value.
157
- //
158
- // Evict is a non-blocking operation.
159
- func (c * Cache ) Evict (key interface {}) (interface {}, bool ) {
160
- c .mu .Lock ()
161
- defer c .mu .Unlock ()
162
- return c .evictLocked (key )
163
- }
164
-
165
139
// Range calls f for each key and value present in the cache in no particular order.
166
140
// If f returns false, Range stops the iteration.
167
141
//
@@ -189,7 +163,7 @@ func (c *Cache) Range(f func(key, value interface{}) bool) {
189
163
190
164
// Flush evicts all keys from the cache.
191
165
//
192
- // Flush skips keys whose fetch callback is currently running .
166
+ // Flush is a non-blocking operation .
193
167
func (c * Cache ) Flush () {
194
168
c .records .Range (func (key , _ interface {}) bool {
195
169
c .Evict (key )
@@ -201,9 +175,33 @@ func (c *Cache) Flush() {
201
175
//
202
176
// Len does not block.
203
177
func (c * Cache ) Len () int {
178
+ return c .list .Len ()
179
+ }
180
+
181
+ // Pop evicts and returns the oldest key and value. If LFU ordering is
182
+ // enabled, then the least frequently used key and value.
183
+ //
184
+ // Pop may block if a concurrent Do is running.
185
+ func (c * Cache ) Pop () (key , value interface {}) {
204
186
c .mu .Lock ()
205
187
defer c .mu .Unlock ()
206
- return c .list .Len ()
188
+ if key = c .list .Pop (); key == nil {
189
+ return nil , nil
190
+ }
191
+ value , ok := c .evictLocked (key )
192
+ if ! ok {
193
+ panic ("evcache: invalid map state" )
194
+ }
195
+ return key , value
196
+ }
197
+
198
+ // Evict evicts a key and returns its value.
199
+ //
200
+ // Evict may block if a concurrent Do is running.
201
+ func (c * Cache ) Evict (key interface {}) (interface {}, bool ) {
202
+ c .mu .Lock ()
203
+ defer c .mu .Unlock ()
204
+ return c .evictLocked (key )
207
205
}
208
206
209
207
// Set the value in the cache for a key.
@@ -213,7 +211,8 @@ func (c *Cache) Len() int {
213
211
// depending on whether LFU ordering is enabled or not.
214
212
//
215
213
// Set may block until a concurrent Fetch callback has returned
216
- // and then immediately overwrite the value.
214
+ // and then immediately overwrite the value. It may also block
215
+ // if a concurrent Do is running.
217
216
func (c * Cache ) Set (key , value interface {}, ttl time.Duration ) {
218
217
var front interface {}
219
218
defer func () {
@@ -256,9 +255,10 @@ func (c *Cache) Set(key, value interface{}, ttl time.Duration) {
256
255
// the least frequently used record or the eldest is evicted
257
256
// depending on whether LFU ordering is enabled or not.
258
257
//
259
- // Fetch may block until a concurrent Fetch callback has returned and will return
260
- // that new value or continues with fetching a new key if the concurrent
261
- // callback returned an error.
258
+ // Fetch may block until a concurrent Fetch callback for key has returned and will return
259
+ // that new value or continues with fetching a new value if the concurrent callback
260
+ // returned an error. It may also block if a concurrent Do is running and the value
261
+ // did not exist causing a store. If the value exists, Fetch will not block.
262
262
func (c * Cache ) Fetch (key interface {}, ttl time.Duration , f FetchCallback ) (value interface {}, closer io.Closer , err error ) {
263
263
var front interface {}
264
264
defer func () {
@@ -309,7 +309,7 @@ func (c *Cache) Fetch(key interface{}, ttl time.Duration, f FetchCallback) (valu
309
309
// from least to most frequently used. If f returns false,
310
310
// Do stops the iteration.
311
311
//
312
- // Do blocks Pop, Set, Evict and Fetch (only if it stores a value).
312
+ // Do blocks Pop, Evict, Set and Fetch (only if it stores a value).
313
313
// It does not visit keys whose Fetch callback is currently running.
314
314
// f is not allowed to modify the cache.
315
315
func (c * Cache ) Do (f func (key , value interface {}) bool ) {
0 commit comments