Skip to content

Commit 79f9dcd

Browse files
committed
fewer defers, document DeletePrefix
1 parent 04261a5 commit 79f9dcd

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

bucket.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ func (b *bucket) set(key string, value interface{}, duration time.Duration) (*It
2727
expires := time.Now().Add(duration).UnixNano()
2828
item := newItem(key, value, expires)
2929
b.Lock()
30-
defer b.Unlock()
3130
existing := b.lookup[key]
3231
b.lookup[key] = item
32+
b.Unlock()
3333
return item, existing
3434
}
3535

3636
func (b *bucket) delete(key string) *Item {
3737
b.Lock()
38-
defer b.Unlock()
3938
item := b.lookup[key]
4039
delete(b.lookup, key)
40+
b.Unlock()
4141
return item
4242
}
4343

@@ -73,15 +73,15 @@ func (b *bucket) deletePrefix(prefix string, deletables chan *Item) int {
7373
}
7474

7575
b.Lock()
76-
defer b.Unlock()
7776
for _, item := range items {
7877
delete(lookup, item.key)
7978
}
79+
b.Unlock()
8080
return len(items)
8181
}
8282

8383
func (b *bucket) clear() {
8484
b.Lock()
85-
defer b.Unlock()
8685
b.lookup = make(map[string]*Item)
86+
b.Unlock()
8787
}

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ item, err := cache.Fetch("user:4", time.Minute * 10, func() (interface{}, error)
9191
cache.Delete("user:4")
9292
```
9393

94+
### DeletePrefix
95+
`DeletePrefix` deletes all keys matching the provided prefix. Returns the number of keys removed.
96+
9497
### Clear
9598
`Clear` clears the cache. This method is **not** thread safe. It is meant to be used from tests.
9699

0 commit comments

Comments
 (0)