util/cache: add package for general-purpose caching

This package allows caching arbitrary key/value pairs in-memory, along
with an interface implemented by the cache types.

Extracted from #7493

Signed-off-by: Andrew Dunham <andrew@du.nham.ca>
Change-Id: Ic8ca820927c456721cf324a0c8f3882a57752cc9
This commit is contained in:
Andrew Dunham
2023-08-16 12:22:23 -04:00
parent f706a3abd0
commit 9fd29f15c7
5 changed files with 379 additions and 0 deletions
+18
View File
@@ -0,0 +1,18 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package cache
// None provides no caching and always calls the provided FillFunc.
//
// It is safe for concurrent use if the underlying FillFunc is.
type None[K comparable, V any] struct{}
// Get always calls the provided FillFunc and returns what it does.
func (c None[K, V]) Get(_ K, f FillFunc[V]) (V, error) {
v, _, e := f()
return v, e
}
// Forget implements Cache.
func (c None[K, V]) Forget() {}