cache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2022 License: MIT Imports: 7 Imported by: 0

README

cache

Documentation

Index

Constants

View Source
const (
	// NoTTL indicates that an item should never expire.
	NoTTL time.Duration = -1

	// DefaultTTL indicates that the default TTL
	// value should be used.
	DefaultTTL time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func New

func New[K comparable, V any](opts ...Option[K, V]) *Cache[K, V]

func (*Cache[K, V]) Delete

func (c *Cache[K, V]) Delete(key K)

func (*Cache[K, V]) DeleteAll

func (c *Cache[K, V]) DeleteAll()

func (*Cache[K, V]) DeleteExpired

func (c *Cache[K, V]) DeleteExpired()

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K, opts ...Option[K, V]) *Item[K, V]

func (*Cache[K, V]) Items

func (c *Cache[K, V]) Items() map[K]*Item[K, V]

func (*Cache[K, V]) Keys

func (c *Cache[K, V]) Keys() []K

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() int

func (*Cache[K, V]) Metrics

func (c *Cache[K, V]) Metrics() Metrics

func (*Cache[K, V]) OnEviction

func (c *Cache[K, V]) OnEviction(fn func(context.Context, EvictionReason, *Item[K, V])) func()

func (*Cache[K, V]) OnInsertion

func (c *Cache[K, V]) OnInsertion(fn func(context.Context, *Item[K, V])) func()

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(key K, value V, ttl time.Duration) *Item[K, V]

func (*Cache[K, V]) Start

func (c *Cache[K, V]) Start()

func (*Cache[K, V]) Stop

func (c *Cache[K, V]) Stop()

func (*Cache[K, V]) Touch

func (c *Cache[K, V]) Touch(key K)

type EvictionReason

type EvictionReason int
const (
	EvictionReasonDeleted EvictionReason = iota + 1
	EvictionReasonExpired
	EvictionReasonCapacityReached
)

type Item

type Item[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Item holds all the information that is associated with a single cache value.

func (*Item[K, V]) ExpiresAt

func (item *Item[K, V]) ExpiresAt() time.Time

ExpiresAt returns the expiration timestamp of the item.

func (*Item[K, V]) IsExpired

func (item *Item[K, V]) IsExpired() bool

IsExpired returns a bool value that indicates whether the item is expired.

func (*Item[K, V]) Key

func (item *Item[K, V]) Key() K

Key returns the key of the item.

func (*Item[K, V]) TTL

func (item *Item[K, V]) TTL() time.Duration

TTL returns the TTL value of the item.

func (*Item[K, V]) Value

func (item *Item[K, V]) Value() V

Value returns the value of the item.

type Loader

type Loader[K comparable, V any] interface {
	Load(c *Cache[K, V], key K) *Item[K, V]
}

type LoaderFunc

type LoaderFunc[K comparable, V any] func(*Cache[K, V], K) *Item[K, V]

func (LoaderFunc[K, V]) Load

func (l LoaderFunc[K, V]) Load(c *Cache[K, V], key K) *Item[K, V]

type Metrics

type Metrics struct {
	Hits       uint64
	Misses     uint64
	Evictions  uint64
	Insertions uint64
}

type Option

type Option[K comparable, V any] interface {
	// contains filtered or unexported methods
}

func WithCapacity

func WithCapacity[K comparable, V any](c uint64) Option[K, V]

func WithDisableTouchOnHit

func WithDisableTouchOnHit[K comparable, V any]() Option[K, V]

func WithLoader

func WithLoader[K comparable, V any](l Loader[K, V]) Option[K, V]

func WithTTL

func WithTTL[K comparable, V any](ttl time.Duration) Option[K, V]

type SuppressedLoader

type SuppressedLoader[K comparable, V any] struct {
	Loader[K, V]
	// contains filtered or unexported fields
}

func (*SuppressedLoader[K, V]) Load

func (l *SuppressedLoader[K, V]) Load(c *Cache[K, V], key K) *Item[K, V]

Jump to

Keyboard shortcuts

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