cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

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(ne NewEncoder, nd NewDecoder, clearCache bool, appPath string, opts ...Option) (*Cache, error)

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) Has added in v0.3.0

func (c *Cache) Has(key string) bool

func (*Cache) Set

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

func (*Cache) SetAsync added in v0.9.0

func (c *Cache) SetAsync(key string, val any) (chan struct{}, error)

type Decoder added in v0.3.0

type Decoder interface {
	Decode(any) error
}

Decoder defines a basic interface for the Decode implementations. The Cache uses the Decoder instance when retrieving the state.

type Encoder added in v0.3.0

type Encoder interface {
	Encode(any) error
}

Encoder defines a basic interface for the Encode implementations. The Cache uses the Encoder instance when persisting the state.

type NewDecoder added in v0.3.0

type NewDecoder func(io.Reader) Decoder

NewDecoder defines a function type that returns a new Decoder instance. This function will be called on each cache restoring operation since the io.Reader may differ.

type NewEncoder added in v0.3.0

type NewEncoder func(io.Writer) Encoder

NewEncoder defines a function type that returns a new Encoder instance. This function will be called on each cache persistent operation since the io.Writer may differ.

type Option

type Option func(*Cache)

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

func WithCompress

func WithCompress() 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