cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2021 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package cache implements a caching strategy for byte slices.

Index

Constants

This section is empty.

Variables

Set is used by wire.

Functions

This section is empty.

Types

type Cache

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

Cache implements a 3-level caching strategy.

func ProvideCache

func ProvideCache(
	ctx context.Context,
	auto promauto.Factory,
	cfg *Config,
	logger *log.Logger,
) (*Cache, func(), error)

ProvideCache is called by wire.

func (*Cache) Get

func (c *Cache) Get(key string, val interface{}) bool

Get retrieves a value from the cache, providing it to val, which must be a *[]byte, *string, or encoding.BinaryUnmarshaler.

func (*Cache) Prune

func (c *Cache) Prune() (n int, err error)

Prune removes cache files until they are below the configured threshold. Since atime is often disabled and we don't (yet) maintain any other statistics about file access, we select files randomly to delete.

func (*Cache) Put

func (c *Cache) Put(key string, val interface{})

Put stores a value into the Cache. The value must be a []byte, string, or an object which implements encoding.BinaryMarshaler.

type Config

type Config struct {
	MaxMem  int    // Maximum in-memory cache size, in MB.
	MaxDisk int    // Maximum on-disk cache size, in MB.
	Path    string // The location of the disk cache.

	// OnAdd is an optional callback that will be notified when new
	// data is added to the cache.
	OnAdd func(key string, data []byte)

	// OnMiss provides an escape-hatch for retrieving cached data from
	// other stores. Any non-nil error may be returned to indicate a
	// cache miss.
	OnMiss func(key string) ([]byte, error)
}

Config provides knobs for the cache.

func (*Config) Bind

func (c *Config) Bind(flags *pflag.FlagSet)

Bind attaches the configuration to the pflag.FlagSet.

Jump to

Keyboard shortcuts

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