summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-cache/v2/lookup.go
blob: d353b295903eb10c7d112c4518302c4f4d650308 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package cache

// LookupCfg is the LookupCache configuration.
type LookupCfg[OGKey, AltKey comparable, Value any] struct {
	// RegisterLookups is called on init to register lookups
	// within LookupCache's internal LookupMap
	RegisterLookups func(*LookupMap[OGKey, AltKey])

	// AddLookups is called on each addition to the cache, to
	// set any required additional key lookups for supplied item
	AddLookups func(*LookupMap[OGKey, AltKey], Value)

	// DeleteLookups is called on each eviction/invalidation of
	// an item in the cache, to remove any unused key lookups
	DeleteLookups func(*LookupMap[OGKey, AltKey], Value)
}

// LookupCache is a cache built on-top of TTLCache, providing multi-key
// lookups for items in the cache by means of additional lookup maps. These
// maps simply store additional keys => original key, with hook-ins to automatically
// call user supplied functions on adding an item, or on updating/deleting an
// item to keep the LookupMap up-to-date.
type LookupCache[OGKey, AltKey comparable, Value any] interface {
	Cache[OGKey, Value]

	// GetBy fetches a cached value by supplied lookup identifier and key
	GetBy(lookup string, key AltKey) (value Value, ok bool)

	// CASBy will attempt to perform a CAS operation on supplied lookup identifier and key
	CASBy(lookup string, key AltKey, cmp, swp Value) bool

	// SwapBy will attempt to perform a swap operation on supplied lookup identifier and key
	SwapBy(lookup string, key AltKey, swp Value) Value

	// HasBy checks if a value is cached under supplied lookup identifier and key
	HasBy(lookup string, key AltKey) bool

	// InvalidateBy invalidates a value by supplied lookup identifier and key
	InvalidateBy(lookup string, key AltKey) bool
}

type lookupTTLCache[OK, AK comparable, V any] struct {
	TTLCache[OK, V]
	config LookupCfg[OK, AK, V]
	lookup LookupMap[OK, AK]
}

// NewLookup returns a new initialized LookupCache.
func NewLookup[OK, AK comparable, V any](cfg LookupCfg[OK, AK, V]) LookupCache[OK, AK, V] {
	switch {
	case cfg.RegisterLookups == nil:
		panic("cache: nil lookups register function")
	case cfg.AddLookups == nil:
		panic("cache: nil lookups add function")
	case cfg.DeleteLookups == nil:
		panic("cache: nil delete lookups function")
	}
	c := &lookupTTLCache[OK, AK, V]{config: cfg}
	c.TTLCache.Init()
	c.lookup.lookup = make(map[string]map[AK]OK)
	c.config.RegisterLookups(&c.lookup)
	c.SetEvictionCallback(nil)
	c.SetInvalidateCallback(nil)
	return c
}

func (c *lookupTTLCache[OK, AK, V]) SetEvictionCallback(hook Hook[OK, V]) {
	if hook == nil {
		hook = emptyHook[OK, V]
	}
	c.TTLCache.SetEvictionCallback(func(key OK, value V) {
		hook(key, value)
		c.config.DeleteLookups(&c.lookup, value)
	})
}

func (c *lookupTTLCache[OK, AK, V]) SetInvalidateCallback(hook Hook[OK, V]) {
	if hook == nil {
		hook = emptyHook[OK, V]
	}
	c.TTLCache.SetInvalidateCallback(func(key OK, value V) {
		hook(key, value)
		c.config.DeleteLookups(&c.lookup, value)
	})
}

func (c *lookupTTLCache[OK, AK, V]) GetBy(lookup string, key AK) (V, bool) {
	c.Lock()
	origKey, ok := c.lookup.Get(lookup, key)
	if !ok {
		c.Unlock()
		var value V
		return value, false
	}
	v, ok := c.GetUnsafe(origKey)
	c.Unlock()
	return v, ok
}

func (c *lookupTTLCache[OK, AK, V]) Put(key OK, value V) bool {
	c.Lock()
	put := c.PutUnsafe(key, value)
	if put {
		c.config.AddLookups(&c.lookup, value)
	}
	c.Unlock()
	return put
}

func (c *lookupTTLCache[OK, AK, V]) Set(key OK, value V) {
	c.Lock()
	defer c.Unlock()
	c.SetUnsafe(key, value)
	c.config.AddLookups(&c.lookup, value)
}

func (c *lookupTTLCache[OK, AK, V]) CASBy(lookup string, key AK, cmp, swp V) bool {
	c.Lock()
	defer c.Unlock()
	origKey, ok := c.lookup.Get(lookup, key)
	if !ok {
		return false
	}
	return c.CASUnsafe(origKey, cmp, swp)
}

func (c *lookupTTLCache[OK, AK, V]) SwapBy(lookup string, key AK, swp V) V {
	c.Lock()
	defer c.Unlock()
	origKey, ok := c.lookup.Get(lookup, key)
	if !ok {
		var value V
		return value
	}
	return c.SwapUnsafe(origKey, swp)
}

func (c *lookupTTLCache[OK, AK, V]) HasBy(lookup string, key AK) bool {
	c.Lock()
	has := c.lookup.Has(lookup, key)
	c.Unlock()
	return has
}

func (c *lookupTTLCache[OK, AK, V]) InvalidateBy(lookup string, key AK) bool {
	c.Lock()
	defer c.Unlock()
	origKey, ok := c.lookup.Get(lookup, key)
	if !ok {
		return false
	}
	c.InvalidateUnsafe(origKey)
	return true
}

// LookupMap is a structure that provides lookups for
// keys to primary keys under supplied lookup identifiers.
// This is essentially a wrapper around map[string](map[K1]K2).
type LookupMap[OK comparable, AK comparable] struct {
	lookup map[string](map[AK]OK)
}

// RegisterLookup registers a lookup identifier in the LookupMap,
// note this can only be doing during the cfg.RegisterLookups() hook.
func (l *LookupMap[OK, AK]) RegisterLookup(id string) {
	if _, ok := l.lookup[id]; ok {
		panic("cache: lookup mapping already exists for identifier")
	}
	l.lookup[id] = make(map[AK]OK, 100)
}

// Get fetches an entry's primary key for lookup identifier and key.
func (l *LookupMap[OK, AK]) Get(id string, key AK) (OK, bool) {
	keys, ok := l.lookup[id]
	if !ok {
		var key OK
		return key, false
	}
	origKey, ok := keys[key]
	return origKey, ok
}

// Set adds a lookup to the LookupMap under supplied lookup identifier,
// linking supplied key to the supplied primary (original) key.
func (l *LookupMap[OK, AK]) Set(id string, key AK, origKey OK) {
	keys, ok := l.lookup[id]
	if !ok {
		panic("cache: invalid lookup identifier")
	}
	keys[key] = origKey
}

// Has checks if there exists a lookup for supplied identifier and key.
func (l *LookupMap[OK, AK]) Has(id string, key AK) bool {
	keys, ok := l.lookup[id]
	if !ok {
		return false
	}
	_, ok = keys[key]
	return ok
}

// Delete removes a lookup from LookupMap with supplied identifier and key.
func (l *LookupMap[OK, AK]) Delete(id string, key AK) {
	keys, ok := l.lookup[id]
	if !ok {
		return
	}
	delete(keys, key)
}