cache

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEntryNotFound = errors.New("entry is not found")
	ErrEntryTooBig   = errors.New("entry is too big")
)

Functions

This section is empty.

Types

type Cache

type Cache interface {
	io.Closer

	Name() string

	// Set saves entry with the given key,
	// it returns an ErrEntryTooBig when entry is too big.
	Set(ctx context.Context, key string, entry []byte) error

	// Delete removes the given key.
	Delete(ctx context.Context, key string) ([]byte, error)

	// Get reads entry for the given key,
	// it returns an ErrEntryNotFound when no entry exists for the given key.
	Get(ctx context.Context, key string) ([]byte, error)
}

Cache holds the action of caching.

func MustNewFile

func MustNewFile(ctx context.Context) Cache

MustNewFile likes NewFile, but panic if error found.

func MustNewFileWithConfig

func MustNewFileWithConfig(ctx context.Context, cfg FileConfig) Cache

MustNewFileWithConfig likes NewFileWithConfig, but panic if error found.

func MustNewMemory

func MustNewMemory(ctx context.Context) Cache

MustNewMemory likes NewMemory, but panic if error found.

func MustNewMemoryWithConfig

func MustNewMemoryWithConfig(ctx context.Context, cfg MemoryConfig) Cache

MustNewMemoryWithConfig likes NewMemoryWithConfig, but panic if error found.

func NewFile

func NewFile(ctx context.Context) (Cache, error)

NewFile returns a filesystem Cache implementation.

func NewFileWithConfig

func NewFileWithConfig(ctx context.Context, cfg FileConfig) (Cache, error)

NewFileWithConfig returns a filesystem Cache implementation with given configuration.

func NewMemory

func NewMemory(ctx context.Context) (Cache, error)

NewMemory returns an in-memory Cache implementation.

func NewMemoryWithConfig

func NewMemoryWithConfig(ctx context.Context, cfg MemoryConfig) (Cache, error)

NewMemoryWithConfig returns an in-memory Cache implementation with given configuration.

func WithSingleFlight

func WithSingleFlight(c Cache) Cache

type FileConfig

type FileConfig struct {
	// Namespace indicates the operating workspace.
	Namespace string
	// EntryMaxAge indicates the maximum lifetime of each entry,
	// default is 15 mins.
	EntryMaxAge time.Duration
	// LazyEntryEviction indicates to evict an expired entry at next peeking,
	// by default, a background looping tries to evict expired entries per 3 mins.
	LazyEntryEviction bool
	// Buckets indicates the bucket number of cache,
	// value must be a power of two,
	// default is 12.
	Buckets int
}

FileConfig holds the configuration of the filesystem cache, entry indexes by key and stores in one file.

func (*FileConfig) Default

func (c *FileConfig) Default()

func (*FileConfig) Validate

func (c *FileConfig) Validate() error

type MemoryConfig

type MemoryConfig struct {
	// Namespace indicates the operating workspace.
	Namespace string
	// EntryMaxAge indicates the maximum lifetime of each entry,
	// default is 15 mins.
	EntryMaxAge time.Duration
	// LazyEntryEviction indicates to evict an expired entry at next peeking,
	// by default, a background looping tries to evict expired entries per 3 mins.
	LazyEntryEviction bool
	// Buckets indicates the bucket number of cache,
	// value must be a power of two,
	// default is 64.
	Buckets int
	// BucketCapacity indicates the maximum MB of each bucket,
	// default is 1 MB.
	BucketCapacity int
	// LazyBucketCapacityScale indicates to scale when the current bucket is not enough to put a new entry,
	// by default, create the bucket with the given capacity to avoid any array copying.
	// It's worth noticing that the bucket capacity can not exceed even configured LazyBucketCapacityScale to true.
	LazyBucketCapacityScale bool
}

MemoryConfig holds the configuration of the in-memory cache, entry indexes by key and stores in one bucket, the total cache size is BucketCapacity * Buckets.

func (*MemoryConfig) Default

func (c *MemoryConfig) Default()

func (*MemoryConfig) Validate

func (c *MemoryConfig) Validate() error

Jump to

Keyboard shortcuts

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