cache

package
v0.3.0-beta0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	MaxSizeToCache int
	Config         *config.CacheConfig
	// contains filtered or unexported fields
}

Cache defines operations for a cache service.

func New

func New(driver Driver, config *config.CacheConfig) *Cache

func (*Cache) Delete

func (c *Cache) Delete(ctx context.Context, key string) error

Deletes deletes an object identified by key from the cache.

func (*Cache) FlushAll

func (c *Cache) FlushAll(ctx context.Context) error

FlushAll clears all entries in the cache.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key string, dest Cacheable) error

Get takes a key string, and a Cacheable object. It fetches an object from the cache, and decodes the binary into dest if it is found. Otherwise it returns a error from Driver.Get.

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, object Cacheable) error

Set takes a Cacheable object, encodes it into binary, checks the length, and set the value into the cache if it's under MaxSizeToCache. If the object exceeds MaxSizeToCache, Set deletes the object from the cache.

type Cacheable

type Cacheable interface {
	// CacheKey returns a cache key string to manage cached entries.
	CacheKey() string
	// DecodeBytes deserializes the byte slice given by by.
	// It can assume ownership and destroy the content of by.
	DecodeBytes(by []byte) error
	// EncodeBytes returns a serialized byte slice of the object.
	EncodeBytes() ([]byte, error)
}

Cacheable is an interface that objects implement to support caching.

type Driver

type Driver interface {
	Set(ctx context.Context, key string, value []byte, expiration time.Duration) error
	Get(ctx context.Context, key string) ([]byte, error)
	Delete(ctx context.Context, key string) error
	ListKeys(ctx context.Context) ([]string, error)
	FlushAll(ctx context.Context) error
}

Driver interface defines common operations for the cache store.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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