@@ -40,31 +40,35 @@ func (c *Cache) Purge() {
40
40
// Add adds a value to the cache. Returns true if an eviction occurred.
41
41
func (c * Cache ) Add (key , value interface {}) (evicted bool ) {
42
42
c .lock .Lock ()
43
- defer c .lock .Unlock ()
44
- return c .lru .Add (key , value )
43
+ evicted = c .lru .Add (key , value )
44
+ c .lock .Unlock ()
45
+ return evicted
45
46
}
46
47
47
48
// Get looks up a key's value from the cache.
48
49
func (c * Cache ) Get (key interface {}) (value interface {}, ok bool ) {
49
50
c .lock .Lock ()
50
- defer c .lock .Unlock ()
51
- return c .lru .Get (key )
51
+ value , ok = c .lru .Get (key )
52
+ c .lock .Unlock ()
53
+ return value , ok
52
54
}
53
55
54
56
// Contains checks if a key is in the cache, without updating the
55
57
// recent-ness or deleting it for being stale.
56
58
func (c * Cache ) Contains (key interface {}) bool {
57
59
c .lock .RLock ()
58
- defer c .lock .RUnlock ()
59
- return c .lru .Contains (key )
60
+ containKey := c .lru .Contains (key )
61
+ c .lock .RUnlock ()
62
+ return containKey
60
63
}
61
64
62
65
// Peek returns the key value (or undefined if not found) without updating
63
66
// the "recently used"-ness of the key.
64
67
func (c * Cache ) Peek (key interface {}) (value interface {}, ok bool ) {
65
68
c .lock .RLock ()
66
- defer c .lock .RUnlock ()
67
- return c .lru .Peek (key )
69
+ value , ok = c .lru .Peek (key )
70
+ c .lock .RUnlock ()
71
+ return value , ok
68
72
}
69
73
70
74
// ContainsOrAdd checks if a key is in the cache without updating the
@@ -98,13 +102,15 @@ func (c *Cache) RemoveOldest() {
98
102
// Keys returns a slice of the keys in the cache, from oldest to newest.
99
103
func (c * Cache ) Keys () []interface {} {
100
104
c .lock .RLock ()
101
- defer c .lock .RUnlock ()
102
- return c .lru .Keys ()
105
+ keys := c .lru .Keys ()
106
+ c .lock .RUnlock ()
107
+ return keys
103
108
}
104
109
105
110
// Len returns the number of items in the cache.
106
111
func (c * Cache ) Len () int {
107
112
c .lock .RLock ()
108
- defer c .lock .RUnlock ()
109
- return c .lru .Len ()
113
+ length := c .lru .Len ()
114
+ c .lock .RUnlock ()
115
+ return length
110
116
}
0 commit comments