filecache

package
v0.0.0-...-2469478 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

filecache is a simple local file-based cache

Index

Constants

This section is empty.

Variables

View Source
var (
	NotFound = errors.New("not found")
	Expired  = errors.New("expired")
)

Functions

func IsCacheMiss

func IsCacheMiss(err error) bool

IsCacheMiss returns true if the error is NotFound or Expired.

Types

type Cache

type Cache[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](domain string, opts ...Option[T]) *Cache[T]

func (*Cache[T]) Clear

func (c *Cache[T]) Clear() error

func (*Cache[T]) Get

func (c *Cache[T]) Get(key string) (T, error)

Get retrieves a value from the cache with the given key.

func (*Cache[T]) GetOrSet

func (c *Cache[T]) GetOrSet(
	key string,
	f func() (T, time.Duration, error),
) (T, error)

GetOrSet is a convenience method that gets the value from the cache if it exists, otherwise it calls the provided function to get the value and sets it in the cache. If the function returns an error, the error is returned and the value is not cached.

func (*Cache[T]) GetOrSetWithTime

func (c *Cache[T]) GetOrSetWithTime(
	key string,
	f func() (T, time.Time, error),
) (T, error)

GetOrSetWithTime is like GetOrSet but it allows the caller to specify the expiration time of the value.

func (*Cache[T]) Set

func (c *Cache[T]) Set(key string, val T, dur time.Duration) error

Set stores a value in the cache with the given key and expiration duration.

func (*Cache[T]) SetWithTime

func (c *Cache[T]) SetWithTime(key string, val T, t time.Time) error

SetWithTime is like Set but it allows the caller to specify the expiration time of the value.

type Option

type Option[T any] func(*Cache[T])

func WithCacheDir

func WithCacheDir[T any](dir string) Option[T]

Jump to

Keyboard shortcuts

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