inmem

package
v2.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package inmem implements in-memory cache for storing data

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 sharable object which encapsulates cache

func New

func New(
	expires time.Duration,
	evicts time.Duration,
	opts ...Option,
) *Cache

New returns a new cache object

func (Cache) Add

func (c Cache) Add(_ context.Context, k string, val []byte) error

Add an item to the cache only if an item doesn't exist for the given key or if the existing item has expired. Returns error otherwise

func (Cache) Delete

func (c Cache) Delete(_ context.Context, key string)

func (Cache) ExpiredItems

func (c Cache) ExpiredItems() map[string]*item

func (Cache) Flush

func (c Cache) Flush()

func (Cache) Get

func (c Cache) Get(_ context.Context, k string) ([]byte, bool)

func (Cache) GetItem

func (c Cache) GetItem(k string) (*item, bool)

func (Cache) Items

func (c Cache) Items() map[string]*item

Item Returns items which aren't expired

func (Cache) MarkExpired

func (c Cache) MarkExpired()

func (Cache) OnEvicted

func (c Cache) OnEvicted(fn func(string, []byte))

func (Cache) OnExpired

func (c Cache) OnExpired(fn func(string, []byte))

func (Cache) Purge

func (c Cache) Purge()

func (Cache) Replace

func (c Cache) Replace(_ context.Context, k string, val []byte) error

Replace item if it exists

func (Cache) Set

func (c Cache) Set(_ context.Context, k string, val []byte)

Adds the item to cache replacing existing one

func (Cache) SetWithDuration

func (c Cache) SetWithDuration(
	_ context.Context,
	k string,
	val []byte,
	expiration time.Duration,
)

type Option

type Option func(*cache)

func WithOnEvictCallback

func WithOnEvictCallback(fn func(k string, val []byte)) Option

func WithOnExpiredCallback

func WithOnExpiredCallback(fn func(k string, val []byte)) Option

Jump to

Keyboard shortcuts

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