sdcache

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package sdcache 缓存工具箱

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = sderr.Sentinel("cache key not found")
)

Functions

func DoTestCommon

func DoTestCommon(t *testing.T, c Cache)

func DoTestExpiration

func DoTestExpiration(t *testing.T, c Cache, ttlSecs int64)

Types

type Cache

type Cache interface {
	Clear(ctx context.Context) error
	Get(ctx context.Context, k any) (any, error)
	GetTTL(ctx context.Context, k any) (time.Duration, error)
	Put(ctx context.Context, k, v any, opts *PutOptions) error
	Delete(ctx context.Context, k any) error
	GetOrPut(ctx context.Context, k any, loader func(ctx context.Context, k any) (any, error), opts *PutOptions) (any, error)
}

type Double

type Double[L1, L2 interface {
	comparable
	Cache
}] struct {
	L1 L1
	L2 L2
}

func D

func D[L1, L2 interface {
	comparable
	Cache
}](c1 L1, c2 L2) Double[L1, L2]

func (Double[L1, L2]) Clear

func (d Double[L1, L2]) Clear(ctx context.Context) error

func (Double[L1, L2]) Delete

func (d Double[L1, L2]) Delete(ctx context.Context, k any) error

func (Double[L1, L2]) Get

func (d Double[L1, L2]) Get(ctx context.Context, k any) (any, error)

func (Double[L1, L2]) GetOrPut

func (d Double[L1, L2]) GetOrPut(ctx context.Context, k any, loader func(ctx context.Context, k any) (any, error), opts *PutOptions) (any, error)

func (Double[L1, L2]) GetTTL

func (d Double[L1, L2]) GetTTL(ctx context.Context, k any) (time.Duration, error)

func (Double[L1, L2]) Put

func (d Double[L1, L2]) Put(ctx context.Context, k, v any, opts *PutOptions) error

type Encoder

type Encoder interface {
	EncodeValue(k, v any) ([]byte, error)
	DecodeValue(data []byte) (any, error)
}

type FuncKey

type FuncKey struct {
	Encode func(k any) (string, error)
	Decode func(s string) (any, error)
	Prefix string
}

func (FuncKey) DecodeKey

func (fk FuncKey) DecodeKey(s string) (any, error)

func (FuncKey) EncodeKey

func (fk FuncKey) EncodeKey(k any) (string, error)

func (FuncKey) PrefixForClear

func (fk FuncKey) PrefixForClear() string

type Int64Key

type Int64Key struct{ Prefix string }

func (Int64Key) DecodeKey

func (ik Int64Key) DecodeKey(s string) (any, error)

func (Int64Key) EncodeKey

func (ik Int64Key) EncodeKey(k any) (string, error)

func (Int64Key) PrefixForClear

func (ik Int64Key) PrefixForClear() string

type JsonEncoder

type JsonEncoder[T any] struct{}

func (JsonEncoder[T]) DecodeValue

func (j JsonEncoder[T]) DecodeValue(data []byte) (any, error)

func (JsonEncoder[T]) EncodeValue

func (j JsonEncoder[T]) EncodeValue(k, v any) ([]byte, error)

type Key

type Key interface {
	EncodeKey(k any) (string, error)
	DecodeKey(s string) (any, error)
	PrefixForClear() string
}

type PutOptions

type PutOptions struct {
	TTL  time.Duration
	Cost int64
}

type StrKey

type StrKey struct{ Prefix string }

func (StrKey) DecodeKey

func (sk StrKey) DecodeKey(s string) (any, error)

func (StrKey) EncodeKey

func (sk StrKey) EncodeKey(k any) (string, error)

func (StrKey) PrefixForClear

func (sk StrKey) PrefixForClear() string

type TextEncoder

type TextEncoder struct{}

func (TextEncoder) DecodeValue

func (t TextEncoder) DecodeValue(data []byte) (any, error)

func (TextEncoder) EncodeValue

func (t TextEncoder) EncodeValue(k, v any) ([]byte, error)

type Typed

type Typed[K, V any] struct {
	C Cache
}

func T

func T[K, V any](c Cache) Typed[K, V]

func (Typed[K, V]) Clear

func (t Typed[K, V]) Clear(ctx context.Context) error

func (Typed[K, V]) Delete

func (t Typed[K, V]) Delete(ctx context.Context, k K) error

func (Typed[K, V]) Get

func (t Typed[K, V]) Get(ctx context.Context, k K) (V, error)

func (Typed[K, V]) GetOrPut

func (t Typed[K, V]) GetOrPut(ctx context.Context, k K, loader func(ctx context.Context, k K) (V, error), opts *PutOptions) (V, error)

func (Typed[K, V]) GetTTL

func (t Typed[K, V]) GetTTL(ctx context.Context, k K) (time.Duration, error)

func (Typed[K, V]) Put

func (t Typed[K, V]) Put(ctx context.Context, k K, v V, opts *PutOptions) error

Directories

Path Synopsis
Package sdcacheredis 使用redis实现的cache
Package sdcacheredis 使用redis实现的cache
Package sdcacheristretto 使用ristretto实现的cache
Package sdcacheristretto 使用ristretto实现的cache

Jump to

Keyboard shortcuts

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