summaryrefslogtreecommitdiff
path: root/vendor/github.com/hashicorp/golang-lru/v2/simplelru
diff options
context:
space:
mode:
authorLibravatar dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>2024-03-04 09:42:11 +0000
committerLibravatar GitHub <noreply@github.com>2024-03-04 09:42:11 +0000
commitadb4cdcf6c9eacd369eaaf5b21774b198d57c040 (patch)
treea4f9c8fe2ce147fa952bcb795690afb9bb2affcb /vendor/github.com/hashicorp/golang-lru/v2/simplelru
parent[bugfix] update postgresqlstmt to correctly use postgres err hook (#2711) (diff)
downloadgotosocial-adb4cdcf6c9eacd369eaaf5b21774b198d57c040.tar.xz
[chore]: Bump modernc.org/sqlite from 1.28.0 to 1.29.2 (#2718)
Diffstat (limited to 'vendor/github.com/hashicorp/golang-lru/v2/simplelru')
-rw-r--r--vendor/github.com/hashicorp/golang-lru/v2/simplelru/LICENSE_list29
-rw-r--r--vendor/github.com/hashicorp/golang-lru/v2/simplelru/lru.go177
-rw-r--r--vendor/github.com/hashicorp/golang-lru/v2/simplelru/lru_interface.go46
3 files changed, 252 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/golang-lru/v2/simplelru/LICENSE_list b/vendor/github.com/hashicorp/golang-lru/v2/simplelru/LICENSE_list
new file mode 100644
index 000000000..c4764e6b2
--- /dev/null
+++ b/vendor/github.com/hashicorp/golang-lru/v2/simplelru/LICENSE_list
@@ -0,0 +1,29 @@
+This license applies to simplelru/list.go
+
+Copyright (c) 2009 The Go Authors. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+ * Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+copyright notice, this list of conditions and the following disclaimer
+in the documentation and/or other materials provided with the
+distribution.
+ * Neither the name of Google Inc. nor the names of its
+contributors may be used to endorse or promote products derived from
+this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/vendor/github.com/hashicorp/golang-lru/v2/simplelru/lru.go b/vendor/github.com/hashicorp/golang-lru/v2/simplelru/lru.go
new file mode 100644
index 000000000..f69792388
--- /dev/null
+++ b/vendor/github.com/hashicorp/golang-lru/v2/simplelru/lru.go
@@ -0,0 +1,177 @@
+// Copyright (c) HashiCorp, Inc.
+// SPDX-License-Identifier: MPL-2.0
+
+package simplelru
+
+import (
+ "errors"
+
+ "github.com/hashicorp/golang-lru/v2/internal"
+)
+
+// EvictCallback is used to get a callback when a cache entry is evicted
+type EvictCallback[K comparable, V any] func(key K, value V)
+
+// LRU implements a non-thread safe fixed size LRU cache
+type LRU[K comparable, V any] struct {
+ size int
+ evictList *internal.LruList[K, V]
+ items map[K]*internal.Entry[K, V]
+ onEvict EvictCallback[K, V]
+}
+
+// NewLRU constructs an LRU of the given size
+func NewLRU[K comparable, V any](size int, onEvict EvictCallback[K, V]) (*LRU[K, V], error) {
+ if size <= 0 {
+ return nil, errors.New("must provide a positive size")
+ }
+
+ c := &LRU[K, V]{
+ size: size,
+ evictList: internal.NewList[K, V](),
+ items: make(map[K]*internal.Entry[K, V]),
+ onEvict: onEvict,
+ }
+ return c, nil
+}
+
+// Purge is used to completely clear the cache.
+func (c *LRU[K, V]) Purge() {
+ for k, v := range c.items {
+ if c.onEvict != nil {
+ c.onEvict(k, v.Value)
+ }
+ delete(c.items, k)
+ }
+ c.evictList.Init()
+}
+
+// Add adds a value to the cache. Returns true if an eviction occurred.
+func (c *LRU[K, V]) Add(key K, value V) (evicted bool) {
+ // Check for existing item
+ if ent, ok := c.items[key]; ok {
+ c.evictList.MoveToFront(ent)
+ ent.Value = value
+ return false
+ }
+
+ // Add new item
+ ent := c.evictList.PushFront(key, value)
+ c.items[key] = ent
+
+ evict := c.evictList.Length() > c.size
+ // Verify size not exceeded
+ if evict {
+ c.removeOldest()
+ }
+ return evict
+}
+
+// Get looks up a key's value from the cache.
+func (c *LRU[K, V]) Get(key K) (value V, ok bool) {
+ if ent, ok := c.items[key]; ok {
+ c.evictList.MoveToFront(ent)
+ return ent.Value, true
+ }
+ return
+}
+
+// Contains checks if a key is in the cache, without updating the recent-ness
+// or deleting it for being stale.
+func (c *LRU[K, V]) Contains(key K) (ok bool) {
+ _, ok = c.items[key]
+ return ok
+}
+
+// Peek returns the key value (or undefined if not found) without updating
+// the "recently used"-ness of the key.
+func (c *LRU[K, V]) Peek(key K) (value V, ok bool) {
+ var ent *internal.Entry[K, V]
+ if ent, ok = c.items[key]; ok {
+ return ent.Value, true
+ }
+ return
+}
+
+// Remove removes the provided key from the cache, returning if the
+// key was contained.
+func (c *LRU[K, V]) Remove(key K) (present bool) {
+ if ent, ok := c.items[key]; ok {
+ c.removeElement(ent)
+ return true
+ }
+ return false
+}
+
+// RemoveOldest removes the oldest item from the cache.
+func (c *LRU[K, V]) RemoveOldest() (key K, value V, ok bool) {
+ if ent := c.evictList.Back(); ent != nil {
+ c.removeElement(ent)
+ return ent.Key, ent.Value, true
+ }
+ return
+}
+
+// GetOldest returns the oldest entry
+func (c *LRU[K, V]) GetOldest() (key K, value V, ok bool) {
+ if ent := c.evictList.Back(); ent != nil {
+ return ent.Key, ent.Value, true
+ }
+ return
+}
+
+// Keys returns a slice of the keys in the cache, from oldest to newest.
+func (c *LRU[K, V]) Keys() []K {
+ keys := make([]K, c.evictList.Length())
+ i := 0
+ for ent := c.evictList.Back(); ent != nil; ent = ent.PrevEntry() {
+ keys[i] = ent.Key
+ i++
+ }
+ return keys
+}
+
+// Values returns a slice of the values in the cache, from oldest to newest.
+func (c *LRU[K, V]) Values() []V {
+ values := make([]V, len(c.items))
+ i := 0
+ for ent := c.evictList.Back(); ent != nil; ent = ent.PrevEntry() {
+ values[i] = ent.Value
+ i++
+ }
+ return values
+}
+
+// Len returns the number of items in the cache.
+func (c *LRU[K, V]) Len() int {
+ return c.evictList.Length()
+}
+
+// Resize changes the cache size.
+func (c *LRU[K, V]) Resize(size int) (evicted int) {
+ diff := c.Len() - size
+ if diff < 0 {
+ diff = 0
+ }
+ for i := 0; i < diff; i++ {
+ c.removeOldest()
+ }
+ c.size = size
+ return diff
+}
+
+// removeOldest removes the oldest item from the cache.
+func (c *LRU[K, V]) removeOldest() {
+ if ent := c.evictList.Back(); ent != nil {
+ c.removeElement(ent)
+ }
+}
+
+// removeElement is used to remove a given list element from the cache
+func (c *LRU[K, V]) removeElement(e *internal.Entry[K, V]) {
+ c.evictList.Remove(e)
+ delete(c.items, e.Key)
+ if c.onEvict != nil {
+ c.onEvict(e.Key, e.Value)
+ }
+}
diff --git a/vendor/github.com/hashicorp/golang-lru/v2/simplelru/lru_interface.go b/vendor/github.com/hashicorp/golang-lru/v2/simplelru/lru_interface.go
new file mode 100644
index 000000000..043b8bcc3
--- /dev/null
+++ b/vendor/github.com/hashicorp/golang-lru/v2/simplelru/lru_interface.go
@@ -0,0 +1,46 @@
+// Copyright (c) HashiCorp, Inc.
+// SPDX-License-Identifier: MPL-2.0
+
+// Package simplelru provides simple LRU implementation based on build-in container/list.
+package simplelru
+
+// LRUCache is the interface for simple LRU cache.
+type LRUCache[K comparable, V any] interface {
+ // Adds a value to the cache, returns true if an eviction occurred and
+ // updates the "recently used"-ness of the key.
+ Add(key K, value V) bool
+
+ // Returns key's value from the cache and
+ // updates the "recently used"-ness of the key. #value, isFound
+ Get(key K) (value V, ok bool)
+
+ // Checks if a key exists in cache without updating the recent-ness.
+ Contains(key K) (ok bool)
+
+ // Returns key's value without updating the "recently used"-ness of the key.
+ Peek(key K) (value V, ok bool)
+
+ // Removes a key from the cache.
+ Remove(key K) bool
+
+ // Removes the oldest entry from cache.
+ RemoveOldest() (K, V, bool)
+
+ // Returns the oldest entry from the cache. #key, value, isFound
+ GetOldest() (K, V, bool)
+
+ // Returns a slice of the keys in the cache, from oldest to newest.
+ Keys() []K
+
+ // Values returns a slice of the values in the cache, from oldest to newest.
+ Values() []V
+
+ // Returns the number of items in the cache.
+ Len() int
+
+ // Clears all cache entries.
+ Purge()
+
+ // Resizes cache, returning number evicted
+ Resize(int) int
+}