Documentation
¶
Index ¶
Examples ¶
Constants ¶
const DefaultShards int = 128
DefaultShards represents the number of shards allocated to LRU if WithShards option is not configured.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] interface { // Capacity returns the maximum allocated capacity of the LRU cache. Capacity() int // Compaction is an expensive O(N) operation to deal with memory fragmentation. Compaction() // Contains verifies if the key is present in the LRU Cache. // It does not update the key to recent status. Contains(key K) bool // Flush clears the LRU cache of all its keys and values. Flush() // Get retrieves the cache value using key. // It returns false if the key is not found. Get(key K) (V, bool) // GetQuiet retrieves the cache value without updating it // to be the most recently used. // It returns false if the key is not found. GetQuiet(key K) (V, bool) // Put adds a new value to the cache with the given key. Put(key K, value V) // Size returns the current size of the LRU cache. Size() int }
Cache defines the general implementation of a 'Least Recently Used' cache. It has thread-safe operations.
As Items are Added to the Cache, The 'Least Recently Used' key is evicted from the Cache.
K represents the type of the key, whereas V represents the type of the Value in the cache
Example ¶
ExampleCache shows a small example of how to initialize a LRU instance and do basic operations like Put, Size, Contains and Capacity.
package main
import (
"fmt"
"github.com/justpranavrs/tlru"
)
// Member is the type of the value stored in the cache.
type Member struct {
Name string
Email string
}
func main() {
cache, err := tlru.New[int, Member](256) // create a lru instance
if err != nil {
fmt.Printf("[ERROR] could not initialize LRU instance: %v", err)
return
}
cache.Put(1, Member{ // insert in user data with user id 1
Name: "justpranavrs",
Email: "iliketlru@gmail.com",
})
fmt.Println(cache.Size()) // gets the current size of the cache
fmt.Println(cache.Contains(2))
fmt.Println(cache.Contains(1)) // reports whether key 1 is present in the cache
fmt.Println(cache.Capacity())
cache.Flush()
fmt.Println(cache.Size())
fmt.Println(cache.Capacity()) // capacity allocated for the cache
}
Output: 1 false true 256 0 256
type LRU ¶
type LRU[K comparable, V any] struct { // contains filtered or unexported fields }
LRU is the better implementation of lrucore.LRUCore. It is a 'Least Recently Used' cache with many instances of lrucore.LRUCore to prevent mutual extension locks on a single instance. It has thread-safe but not a completely lock free operations.
It doesn't work on the standard principle of LRU, rather when the mux routes it to a lrucore.LRUCore instance, it removes its 'LRU', but not the globally oldest item in the cache.
mux.Mux32 takes care of routing the shards to their containers consistently using the FNV-1a non cryptographic hashing algorithm. It uses a custom offset unlike the fixed offset to prevent Hash DOS attacks.
func New ¶
New creates a LRU instance with the given capacity and options. It creates the required lrucore.LRUCore instances, initiates the mux.Mux32 for shard routing.
func (*LRU[K, V]) Capacity ¶
Capacity returns the maximum allocated capacity of the LRU cache across all sharded instances of lrucore.LRUCore.
func (*LRU[K, V]) Compaction ¶
func (l *LRU[K, V]) Compaction()
Compaction is an expensive O(N) operation to deal with memory fragmentation. It compacts all keys across the sharded architecture. For more details, refer lrucore.LRUCore.Compaction
func (*LRU[K, V]) Contains ¶
Contains verifies if the key is present in the LRU Cache by checking the correct sharded instance. It does not update the key to recent status.
func (*LRU[K, V]) Flush ¶
func (l *LRU[K, V]) Flush()
Flush clears the LRU cache of all its keys and values across all sharded instances.
func (*LRU[K, V]) Get ¶
Get retrieves the cache value using key. It returns false if the key is not found. It updates the key as 'recent' only in its respective shard.
func (*LRU[K, V]) GetQuiet ¶
GetQuiet retrieves the cache value without updating it to be the most recently used. It returns false if the key is not found.
type LRUOption ¶
type LRUOption[K comparable, V any] struct { // contains filtered or unexported fields }
LRUOption allows the use of custom options on the New method of LRU.
func WithShards ¶
func WithShards[K comparable, V any](num int) LRUOption[K, V]
WithShards assigns the LRU instance with num shards. Shards are separate instances of lrucore.LRUCore to prevent mutex locks from slowing down the cache.
For performance optimizations, number of shards are automatically rounded up to the next power of 2.
Returns [err.ErrNoShards] if num is less than 1.
func WithUnsafe ¶
func WithUnsafe[K comparable, V any]() LRUOption[K, V]
WithUnsafe provides a faster routing method to [Mux32] by using Go's unsafe package. It does byte conversion with worrying about Go's Garbage Collector (GC).