Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / github.com / hashicorp / golang-lru / lru.go
1 package lru
2
3 import (
4         "sync"
5
6         "github.com/hashicorp/golang-lru/simplelru"
7 )
8
9 // Cache is a thread-safe fixed size LRU cache.
10 type Cache struct {
11         lru  simplelru.LRUCache
12         lock sync.RWMutex
13 }
14
15 // New creates an LRU of the given size.
16 func New(size int) (*Cache, error) {
17         return NewWithEvict(size, nil)
18 }
19
20 // NewWithEvict constructs a fixed size cache with the given eviction
21 // callback.
22 func NewWithEvict(size int, onEvicted func(key interface{}, value interface{})) (*Cache, error) {
23         lru, err := simplelru.NewLRU(size, simplelru.EvictCallback(onEvicted))
24         if err != nil {
25                 return nil, err
26         }
27         c := &Cache{
28                 lru: lru,
29         }
30         return c, nil
31 }
32
33 // Purge is used to completely clear the cache.
34 func (c *Cache) Purge() {
35         c.lock.Lock()
36         c.lru.Purge()
37         c.lock.Unlock()
38 }
39
40 // Add adds a value to the cache.  Returns true if an eviction occurred.
41 func (c *Cache) Add(key, value interface{}) (evicted bool) {
42         c.lock.Lock()
43         evicted = c.lru.Add(key, value)
44         c.lock.Unlock()
45         return evicted
46 }
47
48 // Get looks up a key's value from the cache.
49 func (c *Cache) Get(key interface{}) (value interface{}, ok bool) {
50         c.lock.Lock()
51         value, ok = c.lru.Get(key)
52         c.lock.Unlock()
53         return value, ok
54 }
55
56 // Contains checks if a key is in the cache, without updating the
57 // recent-ness or deleting it for being stale.
58 func (c *Cache) Contains(key interface{}) bool {
59         c.lock.RLock()
60         containKey := c.lru.Contains(key)
61         c.lock.RUnlock()
62         return containKey
63 }
64
65 // Peek returns the key value (or undefined if not found) without updating
66 // the "recently used"-ness of the key.
67 func (c *Cache) Peek(key interface{}) (value interface{}, ok bool) {
68         c.lock.RLock()
69         value, ok = c.lru.Peek(key)
70         c.lock.RUnlock()
71         return value, ok
72 }
73
74 // ContainsOrAdd checks if a key is in the cache  without updating the
75 // recent-ness or deleting it for being stale,  and if not, adds the value.
76 // Returns whether found and whether an eviction occurred.
77 func (c *Cache) ContainsOrAdd(key, value interface{}) (ok, evicted bool) {
78         c.lock.Lock()
79         defer c.lock.Unlock()
80
81         if c.lru.Contains(key) {
82                 return true, false
83         }
84         evicted = c.lru.Add(key, value)
85         return false, evicted
86 }
87
88 // Remove removes the provided key from the cache.
89 func (c *Cache) Remove(key interface{}) {
90         c.lock.Lock()
91         c.lru.Remove(key)
92         c.lock.Unlock()
93 }
94
95 // RemoveOldest removes the oldest item from the cache.
96 func (c *Cache) RemoveOldest() {
97         c.lock.Lock()
98         c.lru.RemoveOldest()
99         c.lock.Unlock()
100 }
101
102 // Keys returns a slice of the keys in the cache, from oldest to newest.
103 func (c *Cache) Keys() []interface{} {
104         c.lock.RLock()
105         keys := c.lru.Keys()
106         c.lock.RUnlock()
107         return keys
108 }
109
110 // Len returns the number of items in the cache.
111 func (c *Cache) Len() int {
112         c.lock.RLock()
113         length := c.lru.Len()
114         c.lock.RUnlock()
115         return length
116 }