Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Basic ¶ added in v0.1.0
type Basic[K comparable, V any] struct { *sync.RWMutex // contains filtered or unexported fields }
Basic is a simple cache and has only supports manual eviction.
Example ¶
package main import ( "fmt" "sort" "github.com/bir/iken/cache" ) func main() { c := cache.NewBasic[string, int]() c.Set("a", 1) out, ok := c.Get("a") fmt.Println(out, ok) out, ok = c.Get("b") fmt.Println(out, ok) c.Set("b", 2) kk := c.Keys() sort.Strings(kk) fmt.Println(kk) c.Delete("a") kk = c.Keys() fmt.Println(kk) }
Output: 1 true 0 false [a b] [b]
func NewBasic ¶ added in v0.1.0
func NewBasic[K comparable, V any]() *Basic[K, V]
NewBasic creates a new non-thread safe cache.
func (*Basic[K, V]) Delete ¶ added in v0.1.0
func (c *Basic[K, V]) Delete(key K)
Delete deletes the item with provided key from the cache.
func (*Basic[K, V]) Get ¶ added in v0.1.0
Get gets an item from the cache. Returns the item or zero value, and a bool indicating whether the key was found.
type Cache ¶
type Cache[K comparable, V any] interface { // Get gets an item from the cache. Get(k K) (V, bool) // Set sets any item to the cache, replacing any existing item if it exists. Set(k K, v V) // Delete deletes the item with provided key from the cache. Delete(key K) // Keys returns existing keys, the order is indeterminate. Keys() []K }
Cache is the basic contract for all cache implementations.
type Exists ¶ added in v0.1.1
type Exists[K comparable] struct { *sync.RWMutex // contains filtered or unexported fields }
Exists is a thread safe data structure to track usage of keys. The backing store is a map of empty struct{}, this is the simplest memory efficient means in Go for tracking keys. See: https://medium.com/easyread/golang-series-empty-struct-ed317e6d8600
Example ¶
package main import ( "fmt" "sort" "github.com/bir/iken/cache" ) func main() { c := cache.NewExists[string]() // Empty check ok := c.Check("a") fmt.Println("a exists", ok) // Basic Mark and Check c.Mark("a") ok = c.Check("a") fmt.Println("a exists", ok) // Add another Mark c.Mark("b") ok = c.Check("a") fmt.Println("a exists", ok) ok = c.Check("b") fmt.Println("b exists", ok) // List keys kk := c.Keys() sort.Strings(kk) fmt.Println("keys", kk) // Remove item c.Delete("a") ok = c.Check("a") fmt.Println("a exists", ok) ok = c.Check("b") fmt.Println("b exists", ok) }
Output: a exists false a exists true a exists true b exists true keys [a b] a exists false b exists true
func NewExists ¶ added in v0.1.1
func NewExists[K comparable]() *Exists[K]
NewExists creates a new thread safe exists.
func (*Exists[K]) Delete ¶ added in v0.1.1
func (c *Exists[K]) Delete(key K)
Delete deletes the item with provided key from the cache.
type NoOp ¶ added in v0.1.1
type NoOp[K comparable, V any] struct{}
NoOp is a facade cache, it never returns a hit. Useful for easily disabling cache at runtime.
func NewNoOp ¶ added in v0.1.1
func NewNoOp[K comparable, V any]() *NoOp[K, V]
NewNoOp creates a new NOP cache.