// Copyright (c) HashiCorp, Inc.// SPDX-License-Identifier: MPL-2.0package simplelruimport ()// EvictCallback is used to get a callback when a cache entry is evictedtypeEvictCallback[ comparable, any] func(key , value )// LRU implements a non-thread safe fixed size LRU cachetypeLRU[ comparable, any] struct { size int evictList *internal.LruList[, ] items map[]*internal.Entry[, ] onEvict EvictCallback[, ]}// NewLRU constructs an LRU of the given sizefunc [ comparable, any]( int, EvictCallback[, ]) (*LRU[, ], error) {if <= 0 {returnnil, errors.New("must provide a positive size") } := &LRU[, ]{size: ,evictList: internal.NewList[, ](),items: make(map[]*internal.Entry[, ]),onEvict: , }return , nil}// Purge is used to completely clear the cache.func ( *LRU[, ]) () {for , := range .items {if .onEvict != nil { .onEvict(, .Value) }delete(.items, ) } .evictList.Init()}// Add adds a value to the cache. Returns true if an eviction occurred.func ( *LRU[, ]) ( , ) ( bool) {// Check for existing itemif , := .items[]; { .evictList.MoveToFront() .Value = returnfalse }// Add new item := .evictList.PushFront(, ) .items[] = := .evictList.Length() > .size// Verify size not exceededif { .removeOldest() }return}// Get looks up a key's value from the cache.func ( *LRU[, ]) ( ) ( , bool) {if , := .items[]; { .evictList.MoveToFront()return .Value, true }return}// Contains checks if a key is in the cache, without updating the recent-ness// or deleting it for being stale.func ( *LRU[, ]) ( ) ( bool) { _, = .items[]return}// Peek returns the key value (or undefined if not found) without updating// the "recently used"-ness of the key.func ( *LRU[, ]) ( ) ( , bool) {var *internal.Entry[, ]if , = .items[]; {return .Value, true }return}// Remove removes the provided key from the cache, returning if the// key was contained.func ( *LRU[, ]) ( ) ( bool) {if , := .items[]; { .removeElement()returntrue }returnfalse}// RemoveOldest removes the oldest item from the cache.func ( *LRU[, ]) () ( , , bool) {if := .evictList.Back(); != nil { .removeElement()return .Key, .Value, true }return}// GetOldest returns the oldest entryfunc ( *LRU[, ]) () ( , , bool) {if := .evictList.Back(); != nil {return .Key, .Value, true }return}// Keys returns a slice of the keys in the cache, from oldest to newest.func ( *LRU[, ]) () [] { := make([], .evictList.Length()) := 0for := .evictList.Back(); != nil; = .PrevEntry() { [] = .Key ++ }return}// Values returns a slice of the values in the cache, from oldest to newest.func ( *LRU[, ]) () [] { := make([], len(.items)) := 0for := .evictList.Back(); != nil; = .PrevEntry() { [] = .Value ++ }return}// Len returns the number of items in the cache.func ( *LRU[, ]) () int {return .evictList.Length()}// Resize changes the cache size.func ( *LRU[, ]) ( int) ( int) { := .Len() - if < 0 { = 0 }for := 0; < ; ++ { .removeOldest() } .size = return}// removeOldest removes the oldest item from the cache.func ( *LRU[, ]) () {if := .evictList.Back(); != nil { .removeElement() }}// removeElement is used to remove a given list element from the cachefunc ( *LRU[, ]) ( *internal.Entry[, ]) { .evictList.Remove()delete(.items, .Key)if .onEvict != nil { .onEvict(.Key, .Value) }}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.