cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Forever            = time.Duration(-1)
	DefaultTTL         = 5 * time.Minute
	DefaultMaxEntries  = 10000
	DefaultCleanup     = 5 * time.Minute
	DefaultDialTimeout = 2 * time.Second
	DefaultPrefix      = "coyote"
	UnknownLength      = -1
)

Variables

View Source
var (
	ErrUnsupported    = errors.New("coyote/cache: the backend does not support this operation")
	ErrUnreachable    = errors.New("coyote/cache: cache is unreachable")
	ErrClosed         = errors.New("coyote/cache: cache is closed")
	ErrBuildAbandoned = errors.New("coyote/cache: the build for this key did not finish")
)

Functions

func Close

func Close(c Cache) error

func Evictions

func Evictions(c Cache) int64

func GetJSON

func GetJSON[T any](ctx context.Context, c Cache, key string) (T, bool, error)

func GetOrSet

func GetOrSet(ctx context.Context, c Cache, key string, ttl time.Duration, build func(context.Context) ([]byte, error)) ([]byte, error)

func Length

func Length(c Cache) int

func Ping

func Ping(ctx context.Context, c Cache) error

func Remember

func Remember[T any](ctx context.Context, c Cache, key string, ttl time.Duration, build func(context.Context) (T, error)) (T, error)

func SetJSON

func SetJSON[T any](ctx context.Context, c Cache, key string, value T, ttl time.Duration) error

Types

type Cache

type Cache interface {
	Get(ctx context.Context, key string) ([]byte, bool, error)
	Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
	Delete(ctx context.Context, key string) error
	Has(ctx context.Context, key string) (bool, error)
	Clear(ctx context.Context) error
}

type Closer

type Closer interface {
	Close() error
}

type Codec

type Codec interface {
	Encode(value any) ([]byte, error)
	Decode(raw []byte, target any) error
}

type Counter

type Counter interface {
	Cache
	Incr(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)
}

type Evicter

type Evicter interface {
	Cache
	Evictions() int64
}

type FileOptions

type FileOptions struct {
	Dir             string
	CleanupInterval time.Duration
}

type FileStore

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

func NewFileStore

func NewFileStore(opts FileOptions) *FileStore

func (*FileStore) Clear

func (f *FileStore) Clear(context.Context) error

func (*FileStore) ClearPrefix

func (f *FileStore) ClearPrefix(ctx context.Context, prefix string) error

func (*FileStore) Close

func (f *FileStore) Close() error

func (*FileStore) Delete

func (f *FileStore) Delete(_ context.Context, key string) error

func (*FileStore) Dir

func (f *FileStore) Dir() string

func (*FileStore) Get

func (f *FileStore) Get(_ context.Context, key string) ([]byte, bool, error)

func (*FileStore) Has

func (f *FileStore) Has(ctx context.Context, key string) (bool, error)

func (*FileStore) Incr

func (f *FileStore) Incr(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)

func (*FileStore) Len

func (f *FileStore) Len() int

func (*FileStore) Ping

func (f *FileStore) Ping(context.Context) error

func (*FileStore) Set

func (f *FileStore) Set(_ context.Context, key string, value []byte, ttl time.Duration) error

func (*FileStore) Sweep

func (f *FileStore) Sweep() int

type Handle

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

func New

func New(store Cache, opts Options) *Handle

func (*Handle) Alias

func (h *Handle) Alias() string

func (*Handle) Clear

func (h *Handle) Clear(ctx context.Context) error

func (*Handle) ClearPrefix

func (h *Handle) ClearPrefix(ctx context.Context, prefix string) error

func (*Handle) Close

func (h *Handle) Close() error

func (*Handle) Delete

func (h *Handle) Delete(ctx context.Context, key string) error

func (*Handle) Get

func (h *Handle) Get(ctx context.Context, key string) ([]byte, bool, error)

func (*Handle) GetMulti

func (h *Handle) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)

func (*Handle) Has

func (h *Handle) Has(ctx context.Context, key string) (bool, error)

func (*Handle) Incr

func (h *Handle) Incr(ctx context.Context, key string, delta int64, ttl time.Duration) (int64, error)

func (*Handle) Key

func (h *Handle) Key(key string) string

func (*Handle) Len

func (h *Handle) Len() int

func (*Handle) Namespace

func (h *Handle) Namespace() string

func (*Handle) Ping

func (h *Handle) Ping(ctx context.Context) error

func (*Handle) Set

func (h *Handle) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

func (*Handle) Stats

func (h *Handle) Stats() Stats

func (*Handle) Store

func (h *Handle) Store() Cache

func (*Handle) TTL

func (h *Handle) TTL(ttl time.Duration) time.Duration

type JSONCodec

type JSONCodec struct{}

func (JSONCodec) Decode

func (JSONCodec) Decode(raw []byte, target any) error

func (JSONCodec) Encode

func (JSONCodec) Encode(value any) ([]byte, error)

type Measured

type Measured interface {
	Cache
	Len() int
}

type MemoryOptions

type MemoryOptions struct {
	MaxEntries      int
	MaxBytes        int64
	CleanupInterval time.Duration
}

type MemoryStore

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

func NewMemoryStore

func NewMemoryStore(opts MemoryOptions) *MemoryStore

func (*MemoryStore) Bytes

func (m *MemoryStore) Bytes() int64

func (*MemoryStore) Clear

func (m *MemoryStore) Clear(_ context.Context) error

func (*MemoryStore) ClearPrefix

func (m *MemoryStore) ClearPrefix(_ context.Context, prefix string) error

func (*MemoryStore) Close

func (m *MemoryStore) Close() error

func (*MemoryStore) Delete

func (m *MemoryStore) Delete(_ context.Context, key string) error

func (*MemoryStore) Evictions

func (m *MemoryStore) Evictions() int64

func (*MemoryStore) Get

func (m *MemoryStore) Get(_ context.Context, key string) ([]byte, bool, error)

func (*MemoryStore) GetMulti

func (m *MemoryStore) GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)

func (*MemoryStore) Has

func (m *MemoryStore) Has(ctx context.Context, key string) (bool, error)

func (*MemoryStore) Incr

func (m *MemoryStore) Incr(_ context.Context, key string, delta int64, ttl time.Duration) (int64, error)

func (*MemoryStore) Len

func (m *MemoryStore) Len() int

func (*MemoryStore) Ping

func (m *MemoryStore) Ping(context.Context) error

func (*MemoryStore) Set

func (m *MemoryStore) Set(_ context.Context, key string, value []byte, ttl time.Duration) error

type Multi

type Multi interface {
	Cache
	GetMulti(ctx context.Context, keys []string) (map[string][]byte, error)
}

type Namespacer

type Namespacer interface {
	Cache
	ClearPrefix(ctx context.Context, prefix string) error
}

type NoopStore

type NoopStore struct{}

func NewNoopStore

func NewNoopStore() *NoopStore

func (*NoopStore) Clear

func (*NoopStore) Clear(context.Context) error

func (*NoopStore) ClearPrefix

func (*NoopStore) ClearPrefix(context.Context, string) error

func (*NoopStore) Delete

func (*NoopStore) Delete(context.Context, string) error

func (*NoopStore) Get

func (*NoopStore) Get(context.Context, string) ([]byte, bool, error)

func (*NoopStore) Has

func (*NoopStore) Len

func (*NoopStore) Len() int

func (*NoopStore) Ping

func (*NoopStore) Ping(context.Context) error

func (*NoopStore) Set

type Options

type Options struct {
	Alias   string
	Backend string
	Prefix  string
	Version string
	TTL     time.Duration
	Codec   Codec
}

type Pinger

type Pinger interface {
	Cache
	Ping(ctx context.Context) error
}

type Stats

type Stats struct {
	Alias     string
	Backend   string
	Hits      int64
	Misses    int64
	Sets      int64
	Deletes   int64
	Errors    int64
	Evictions int64
	Entries   int
}

func (Stats) HitPercent

func (s Stats) HitPercent() float64

func (Stats) HitRate

func (s Stats) HitRate() float64

func (Stats) Lookups

func (s Stats) Lookups() int64

type Status

type Status struct {
	Alias     string
	Backend   string
	Address   string
	Reachable bool
	Err       error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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