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