internal

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is an abstracted cache that provides a skeletal implementation, of the Cache interface to minimize the effort required to implement interface.

func New

func New(c Collection, cap int) *Cache

New return new abstracted cache.

func (*Cache) Cap

func (c *Cache) Cap() int

Cap Returns the cache capacity.

func (*Cache) Contains

func (c *Cache) Contains(key interface{}) (ok bool)

Contains Checks if a key exists in cache.

func (*Cache) DelSilently

func (c *Cache) DelSilently(key interface{})

DelSilently the key value silently without call onEvicted.

func (*Cache) Delete

func (c *Cache) Delete(key interface{})

Delete deletes the key value.

func (*Cache) Discard

func (c *Cache) Discard() (key, value interface{})

Discard oldest entry from cache to make room for the new ones.

func (*Cache) Expiry

func (c *Cache) Expiry(key interface{}) (t time.Time, ok bool)

Expiry returns key value expiry time.

func (*Cache) GC added in v1.0.5

func (c *Cache) GC() time.Duration

GC returns the remaining time duration for the next gc cycle if there any, Otherwise, it return 0.

Calling GC without waits for the duration to elapsed considered a no-op.

func (*Cache) Ignore added in v1.0.5

func (c *Cache) Ignore(ch chan<- Event, ops ...Op)

Ignore causes the provided ops to be ignored. Ignore undoes the effect of any prior calls to Notify for the provided ops. If no ops are provided, ch removed.

func (*Cache) Keys

func (c *Cache) Keys() (keys []interface{})

Keys return cache records keys.

func (*Cache) Len

func (c *Cache) Len() int

Len Returns the number of items in the cache.

func (*Cache) Load

func (c *Cache) Load(key interface{}) (interface{}, bool)

Load returns key value.

func (*Cache) Notify added in v1.0.5

func (c *Cache) Notify(ch chan<- Event, ops ...Op)

Notify causes cache to relay events to ch. If no operations are provided, all incoming operations will be relayed to ch. Otherwise, just the provided operations will.

func (*Cache) Peek

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

Peek returns key value without updating the underlying "rank".

func (*Cache) Purge

func (c *Cache) Purge()

Purge Clears all cache entries.

func (*Cache) RegisterOnEvicted

func (c *Cache) RegisterOnEvicted(fn func(key, value interface{}))

RegisterOnEvicted registers a function, to call it when an entry is purged from the cache.

func (*Cache) RegisterOnExpired

func (c *Cache) RegisterOnExpired(fn func(key, value interface{}))

RegisterOnExpired registers a function, to call it when an entry TTL elapsed.

func (*Cache) Resize

func (c *Cache) Resize(size int) int

Resize cache, returning number evicted

func (*Cache) SetTTL

func (c *Cache) SetTTL(ttl time.Duration)

SetTTL sets entries default TTL.

func (*Cache) Store

func (c *Cache) Store(key, value interface{})

Store sets the value for a key.

func (*Cache) StoreWithTTL

func (c *Cache) StoreWithTTL(key, value interface{}, ttl time.Duration)

StoreWithTTL sets the key value with TTL overrides the default.

func (*Cache) TTL

func (c *Cache) TTL() time.Duration

TTL returns entries default TTL.

func (*Cache) Update

func (c *Cache) Update(key, value interface{})

Update the key value without updating the underlying "rank".

type Collection

type Collection interface {
	Move(*Entry)
	Add(*Entry)
	Remove(*Entry)
	Discard() *Entry
	Len() int
	Init()
}

Collection represents the cache underlying data structure, and defines the functions or operations that can be applied to the data elements.

type Entry

type Entry struct {
	Key     interface{}
	Value   interface{}
	Element interface{}
	Exp     time.Time
	// contains filtered or unexported fields
}

Entry is used to hold a value in the cache.

type Event added in v1.0.5

type Event struct {
	// Op represents cache operation that triggered the event.
	Op Op
	// Key represents cache entry key.
	Key interface{}
	// Value represents cache key value.
	Value interface{}
	// Expiry represents cache key value expiry time.
	Expiry time.Time
	// Ok report whether the read operation succeed.
	Ok bool
}

Event represents a single cache entry change.

func (Event) String added in v1.0.5

func (e Event) String() string

String returns a string representation of the event in the form "file: REMOVE|WRITE|..."

type Op added in v1.0.5

type Op uint8

Op describes a set of cache operations.

const (
	Read Op = iota + 1
	Write
	Remove
)

These are the generalized cache operations that can trigger a event.

func (Op) String added in v1.0.5

func (op Op) String() string

Jump to

Keyboard shortcuts

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