github.com/tirogen/go-ethereum@v1.10.12-0.20221226051715-250cfede41b6/common/lru/lru.go (about)

     1  // Copyright 2022 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package lru
    18  
    19  import "sync"
    20  
    21  // Cache is a LRU cache.
    22  // This type is safe for concurrent use.
    23  type Cache[K comparable, V any] struct {
    24  	cache BasicLRU[K, V]
    25  	mu    sync.Mutex
    26  }
    27  
    28  // NewCache creates an LRU cache.
    29  func NewCache[K comparable, V any](capacity int) *Cache[K, V] {
    30  	return &Cache[K, V]{cache: NewBasicLRU[K, V](capacity)}
    31  }
    32  
    33  // Add adds a value to the cache. Returns true if an item was evicted to store the new item.
    34  func (c *Cache[K, V]) Add(key K, value V) (evicted bool) {
    35  	c.mu.Lock()
    36  	defer c.mu.Unlock()
    37  
    38  	return c.cache.Add(key, value)
    39  }
    40  
    41  // Contains reports whether the given key exists in the cache.
    42  func (c *Cache[K, V]) Contains(key K) bool {
    43  	c.mu.Lock()
    44  	defer c.mu.Unlock()
    45  
    46  	return c.cache.Contains(key)
    47  }
    48  
    49  // Get retrieves a value from the cache. This marks the key as recently used.
    50  func (c *Cache[K, V]) Get(key K) (value V, ok bool) {
    51  	c.mu.Lock()
    52  	defer c.mu.Unlock()
    53  
    54  	return c.cache.Get(key)
    55  }
    56  
    57  // Len returns the current number of items in the cache.
    58  func (c *Cache[K, V]) Len() int {
    59  	c.mu.Lock()
    60  	defer c.mu.Unlock()
    61  
    62  	return c.cache.Len()
    63  }
    64  
    65  // Peek retrieves a value from the cache, but does not mark the key as recently used.
    66  func (c *Cache[K, V]) Peek(key K) (value V, ok bool) {
    67  	c.mu.Lock()
    68  	defer c.mu.Unlock()
    69  
    70  	return c.cache.Peek(key)
    71  }
    72  
    73  // Purge empties the cache.
    74  func (c *Cache[K, V]) Purge() {
    75  	c.mu.Lock()
    76  	defer c.mu.Unlock()
    77  
    78  	c.cache.Purge()
    79  }
    80  
    81  // Remove drops an item from the cache. Returns true if the key was present in cache.
    82  func (c *Cache[K, V]) Remove(key K) bool {
    83  	c.mu.Lock()
    84  	defer c.mu.Unlock()
    85  
    86  	return c.cache.Remove(key)
    87  }
    88  
    89  // Keys returns all keys of items currently in the LRU.
    90  func (c *Cache[K, V]) Keys() []K {
    91  	c.mu.Lock()
    92  	defer c.mu.Unlock()
    93  
    94  	return c.cache.Keys()
    95  }