cache

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 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)

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

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

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

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

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

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

	// Purge clears 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

Contain checks if a key exists in the cache.

func (*LRUCache) Del

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

Del 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)

GetOrLoad gets value from the cache or load by loader.

func (*LRUCache) Keys

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

Keys returns a slice of the keys in the cache.

func (*LRUCache) Len

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

Len returns the number of items in the cache.

func (*LRUCache) Peek

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

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

func (*LRUCache) Purge

func (c *LRUCache) Purge()

Purge clears 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