cache

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultSize = 100
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Set key-value pair with an expiration.
	Set(key, value interface{}, expire time.Duration)

	// Get value from the cache by the key.
	Get(key interface{}) (interface{}, bool)

	// Get value from the cache or load by loader.
	GetOrLoad(key interface{}, loader LoaderFunc) (interface{}, error)

	// Return value without updating the "recently used"-ness of the key.
	Peek(key interface{}) (interface{}, bool)

	// Delete the specified key from the cache.
	Del(key interface{}) bool

	// Check if a key exists in the cache.
	Contain(key interface{}) bool

	// Return the number of items in the cache.
	Len(withoutExpired ...bool) int

	// Return a slice of the keys in the cache.
	Keys(withoutExpired ...bool) []interface{}

	// Clear the cache entities.
	Purge()
}

Cache is the interface for LRU/ARC cache.

func New

func New(evictType EvictType, opts ...Option) (c Cache)

type EvictType

type EvictType string
const (
	LRU EvictType = "lru"
	ARC           = "arc"
)

type LRUCache

type LRUCache struct {
	// contains filtered or unexported fields
}

func (*LRUCache) Contain

func (c *LRUCache) Contain(key interface{}) bool

Check if a key exists in the cache.

func (*LRUCache) Del

func (c *LRUCache) Del(key interface{}) bool

Delete the specified key from the cache.

func (*LRUCache) Get

func (c *LRUCache) Get(key interface{}) (interface{}, bool)

Get value from the cache by the key.

func (*LRUCache) GetOrLoad

func (c *LRUCache) GetOrLoad(key interface{}, loader LoaderFunc) (interface{}, error)

func (*LRUCache) Keys

func (c *LRUCache) Keys(withoutExpired ...bool) []interface{}

Return a slice of the keys in the cache.

func (*LRUCache) Len

func (c *LRUCache) Len(withoutExpired ...bool) int

Return the number of items in the cache.

func (*LRUCache) Peek

func (c *LRUCache) Peek(key interface{}) (interface{}, bool)

Return value without updating the "recently used"-ness of the key.

func (*LRUCache) Purge

func (c *LRUCache) Purge()

Clear the cache entities.

func (*LRUCache) Set

func (c *LRUCache) Set(key, value interface{}, expire time.Duration)

Set key-value pair with an expiration.

type LoaderFunc

type LoaderFunc func(interface{}) (interface{}, time.Duration, error)

type Option

type Option func(*base)

func WithSize

func WithSize(size int) Option

Jump to

Keyboard shortcuts

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