cache

package
v0.5.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 1 Imported by: 0

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]) Clear added in v0.5.3

func (c *Basic[K, V]) Clear()

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

func (c *Basic[K, V]) Get(k K) (V, bool)

Get gets an item from the cache. Returns the item or zero value, and a bool indicating whether the key was found.

func (*Basic[K, _]) Keys added in v0.1.0

func (c *Basic[K, _]) Keys() []K

Keys returns existing keys, the order is indeterminate.

func (*Basic[K, V]) Set added in v0.1.0

func (c *Basic[K, V]) Set(k K, v V)

Set sets any item to the cache. replacing any existing item. The default item never expires.

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
	// Clear resets the cache.
	Clear()
}

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]) Check added in v0.1.1

func (c *Exists[K]) Check(k K) bool

Check returns true if the key is marked.

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.

func (*Exists[K]) Keys added in v0.1.1

func (c *Exists[K]) Keys() []K

Keys returns existing keys, the order is indeterminate.

func (*Exists[K]) Mark added in v0.1.1

func (c *Exists[K]) Mark(k K)

Mark flags the key.

func (*Exists[K]) MarkIf added in v0.1.2

func (c *Exists[K]) MarkIf(k K) bool

MarkIf flags the key and returns if it was previously set.

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.

func (*NoOp[K, V]) Clear added in v0.5.3

func (c *NoOp[K, V]) Clear()

Clear no-op.

func (*NoOp[K, V]) Delete added in v0.1.1

func (c *NoOp[K, V]) Delete(_ K)

Delete no-op.

func (*NoOp[K, V]) Get added in v0.1.1

func (c *NoOp[K, V]) Get(_ K) (out V, ok bool)

Get always returns !ok.

func (*NoOp[K, _]) Keys added in v0.1.1

func (c *NoOp[K, _]) Keys() []K

Keys always returns nil array.

func (*NoOp[K, V]) Set added in v0.1.1

func (c *NoOp[K, V]) Set(_ K, _ V)

Set no-op.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL