cache

package
v0.68.12 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package cache provides generic cache. It is used to store values by key and retrieve them later.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[V any] struct {
	Name  string
	Cache map[string]V
	Mutex *sync.RWMutex
}

Cache - generic cache implementation

func ContextCache added in v0.66.3

func ContextCache[T any](ctx context.Context, key any) *Cache[T]

ContextCache returns cache from the context. If the cache is nil, it creates a new instance.

func NewCache

func NewCache[V any](name string) *Cache[V]

NewCache - create new cache with generic type V

func (*Cache[V]) Get

func (c *Cache[V]) Get(ctx context.Context, key string) (V, bool)

Get - fetch value from cache by key

func (*Cache[V]) Put

func (c *Cache[V]) Put(ctx context.Context, key string, value V)

Put - put value into cache by key

type ExpiringCache

type ExpiringCache[V any] struct {
	Name  string
	Cache map[string]ExpiringItem[V]
	Mutex *sync.RWMutex
}

ExpiringCache - cache with items with expiration time

func NewExpiringCache

func NewExpiringCache[V any](name string) *ExpiringCache[V]

NewExpiringCache - create new cache with generic type V

func (*ExpiringCache[V]) Get

func (c *ExpiringCache[V]) Get(ctx context.Context, key string) (V, bool)

Get - fetch value from cache by key

func (*ExpiringCache[V]) Put

func (c *ExpiringCache[V]) Put(ctx context.Context, key string, value V, expiration time.Time)

Put - put value into cache by key

type ExpiringItem

type ExpiringItem[V any] struct {
	Value      V
	Expiration time.Time
}

ExpiringItem - item with expiration time

Jump to

Keyboard shortcuts

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