cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 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.

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

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 cache keys. the order is sorted by created.

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.
	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.

func (*NoOpCache[K, V]) Delete

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

Delete no-op.

func (*NoOpCache[K, V]) Get

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

Get always returns !ok.

func (*NoOpCache[K, _]) Keys

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

Keys always returns nil array.

func (*NoOpCache[K, V]) Set

func (c *NoOpCache[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