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.
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. Set(k K, v V) // Delete deletes the item with provided key from the cache. Delete(key K) // Keys returns cache keys. the order is sorted by created. Keys() []K }
Cache is the basic contract for all cache implementations.
Example ¶
package main
import (
"fmt"
"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()
fmt.Println(kk)
c.Delete("a")
kk = c.Keys()
fmt.Println(kk)
}
Output: 1 true 0 false [a b] [b]
type NoOpCache ¶
type NoOpCache[K comparable, V any] struct{}
NoOpCache is a facade cache, it never returns a hit. Useful for easily disabling cache at runtime.
func NewNoOpCache ¶
func NewNoOpCache[K comparable, V any]() *NoOpCache[K, V]
NewNoOpCache creates a new NOP cache.
Click to show internal directories.
Click to hide internal directories.