cache

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 5, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultCachePath = "./cache"

DefaultCachePath defines a default path for storing file cache entries.

Variables

View Source
var ErrNoCache = errors.New("cache entry not found")

ErrNoCache defines an error that may occur if the requested cache entry was not found.

Functions

This section is empty.

Types

type Cache

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

Cache provides a file cache API. It saves and restores an arbitrary data types which can marshaled/unmarshalled as JSON into file cache. The cache entries can be restored by the corresponding key. The cache files will be stored at configured path or default DefaultCachePath will be used.

func NewCache

func NewCache(opts ...Option) *Cache

func (*Cache) Get

func (c *Cache) Get(key string, target any) error

Get retrieves a cache entry by its key and maps data to the provided target. The target instance must be a pointer type and support JSON unmarshalling. If the corresponding cache entry was not found the ErrNoCache error will be returned.

func (*Cache) Set

func (c *Cache) Set(key string, val any) error

Set creates a new cache entry for specified key or overrides the existing one. The val instance must support JSON marshalling.

type Option

type Option func(*Cache)

Option defines a type for providing configuration options for Cache instance.

func WithCacheDir

func WithCacheDir(dir string) Option

WithCacheDir allows setting a custom path for storing cache files. By default, DefaultCachePath will be used.

func WithCompress

func WithCompress(enable bool) Option

WithCompress enables or disables compression of cache files. By default it's enabled.

Jump to

Keyboard shortcuts

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