github.com/fufuok/freelru@v0.13.3/cache.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //    http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package freelru
    19  
    20  import "time"
    21  
    22  type Cache[K comparable, V any] interface {
    23  	// SetLifetime sets the default lifetime of LRU elements.
    24  	// Lifetime 0 means "forever".
    25  	SetLifetime(lifetime time.Duration)
    26  
    27  	// SetOnEvict sets the OnEvict callback function.
    28  	// The onEvict function is called for each evicted lru entry.
    29  	SetOnEvict(onEvict OnEvictCallback[K, V])
    30  
    31  	// Len returns the number of elements stored in the cache.
    32  	Len() int
    33  
    34  	// AddWithLifetime adds a key:value to the cache with a lifetime.
    35  	// Returns true, true if key was updated and eviction occurred.
    36  	AddWithLifetime(key K, value V, lifetime time.Duration) (evicted bool)
    37  
    38  	// Add adds a key:value to the cache.
    39  	// Returns true, true if key was updated and eviction occurred.
    40  	Add(key K, value V) (evicted bool)
    41  
    42  	// Get retrieves an element from map under given key.
    43  	Get(key K) (V, bool)
    44  
    45  	// Peek retrieves an element from map under given key without updating the
    46  	// "recently used"-ness of that key.
    47  	Peek(key K) (V, bool)
    48  
    49  	// Contains checks for the existence of a key, without changing its recent-ness.
    50  	Contains(key K) bool
    51  
    52  	// Remove removes an element from the map.
    53  	// The return value indicates whether the key existed or not.
    54  	Remove(key K) bool
    55  
    56  	// Keys returns a slice of the keys in the cache, from oldest to newest.
    57  	Keys() []K
    58  
    59  	// Purge purges all data (key and value) from the LRU.
    60  	Purge()
    61  
    62  	// Metrics returns the metrics of the cache.
    63  	Metrics() Metrics
    64  
    65  	// ResetMetrics resets the metrics of the cache and returns the previous state.
    66  	ResetMetrics() Metrics
    67  }