This repository has been archived by the owner on Jul 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
77 lines (60 loc) · 1.87 KB
/
types.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
71
72
73
74
75
76
77
package lru
import (
"container/list"
"sync"
"time"
)
// Cache is the interface for the LRU cache
type Cache interface {
// Adds a value to the cache, or updates an item in the cache.
// It returns true if an item needed to be removed for storing the new item.
Add(key interface{}, value interface{}, ttl int64) bool
// Returns the value of the provided key, and updates status of the item
// in the cache.
Get(key interface{}) (value interface{}, ok bool)
// Check if a key exsists in the cache.
Contains(key interface{}) (ok bool)
// Expires returns the time of expiration.
Expires(key interface{}) (expires time.Time, ok bool)
// Fetches a value which has expired, or does not exits and fills the cache.
Fetch(key interface{}, ttl int64, call func() (interface{}, error)) (value interface{}, ok bool, err error)
// Removes a key from the cache.
Remove(key interface{}) bool
// Removes the oldest entry from cache.
RemoveOldest() (interface{}, interface{}, bool)
// Returns the oldest entry from the cache.
GetOldest() (interface{}, interface{}, bool)
// Returns a slice of the keys in the cache, from oldest to newest.
Keys() []interface{}
// Returns the number of items in the cache.
Len() int
// Purge is purging the full cache.
Purge()
}
// cache is a thread-safe, fixed size LRU cache with TTL.
type SimpleCache struct {
sync.RWMutex
lru Cache
}
// Sized is the interface to a cache size calculation.
type Sized interface {
Size() int64
}
// LRUCache represents the instance of an LRU cache.
type LRUCache struct {
size int
items map[interface{}]*list.Element
list *list.List
}
// Item represents the internal presentation of a cache entry
type Item struct {
key interface{}
group string // this is not yet used
promotions int32
refs int32
expires int64
ttl int64
timestamp int64
size int64
value interface{}
}