github.com/MetalBlockchain/metalgo@v1.11.9/cache/cache.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package cache
     5  
     6  // Cacher acts as a best effort key value store.
     7  type Cacher[K comparable, V any] interface {
     8  	// Put inserts an element into the cache. If space is required, elements will
     9  	// be evicted.
    10  	Put(key K, value V)
    11  
    12  	// Get returns the entry in the cache with the key specified, if no value
    13  	// exists, false is returned.
    14  	Get(key K) (V, bool)
    15  
    16  	// Evict removes the specified entry from the cache
    17  	Evict(key K)
    18  
    19  	// Flush removes all entries from the cache
    20  	Flush()
    21  
    22  	// Returns the number of elements currently in the cache
    23  	Len() int
    24  
    25  	// Returns fraction of cache currently filled (0 --> 1)
    26  	PortionFilled() float64
    27  }
    28  
    29  // Evictable allows the object to be notified when it is evicted
    30  type Evictable[K comparable] interface {
    31  	Key() K
    32  	Evict()
    33  }
    34  
    35  // Deduplicator acts as a best effort deduplication service
    36  type Deduplicator[K comparable, V Evictable[K]] interface {
    37  	// Deduplicate returns either the provided value, or a previously provided
    38  	// value with the same ID that hasn't yet been evicted
    39  	Deduplicate(V) V
    40  
    41  	// Flush removes all entries from the cache
    42  	Flush()
    43  }