Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2025 License: MIT Imports: 35 Imported by: 1

Documentation

Index

Constants

View Source
const (
	ItemDataTypeMap = iota
	ItemDataTypeDBJsonTable
	ItemDataTypeJsonValue
	ItemDataTypeBytes
	ItemDataTypeString
	ItemDataTypeResponse
	ItemNil
)

Variables

View Source
var ErrMissCache = errors.New("Key not found in cache")
View Source
var (
	ErrNoCacheConfigured = errors.New("no cache configured")
)

Functions

func FieldKey

func FieldKey(field *ast.Field, vars map[string]any) (string, error)

func QueryKey

func QueryKey(query string, vars map[string]any) (string, error)

Types

type BackendType

type BackendType string
const (
	L2RedisBackend     BackendType = "redis"
	L2MemcachedBackend BackendType = "memcached"
	L2PegasusBackend   BackendType = "pegasus"
)

type CacheItem

type CacheItem struct {
	Type int8
	Data any
}

func NewCacheItem

func NewCacheItem(data any) (*CacheItem, error)

func (*CacheItem) DecodeMsgpack

func (item *CacheItem) DecodeMsgpack(dec *msgpack.Decoder) (err error)

func (*CacheItem) EncodeMsgpack

func (item *CacheItem) EncodeMsgpack(enc *msgpack.Encoder) error

type Config

type Config struct {
	TTL time.Duration // default time to live for the cache
	L1  L1Config
	L2  L2Config
}

func (Config) Init

type Info

type Info struct {
	Use        bool
	Key        string
	Tags       []string
	TTL        time.Duration
	Invalidate bool
}

func QueryInfo

func QueryInfo(field *ast.Field, vars map[string]any) Info

func (*Info) Merge

func (i *Info) Merge(other Info)

func (Info) Options

func (i Info) Options() []Option

type L1Config

type L1Config struct {
	Enabled      bool
	MaxSize      int // Maximum size of the cache Megabytes
	MaxItemSize  int // Maximum size of an item in the cache
	Shards       int // Number of shards in the cache
	CleanTime    time.Duration
	EvictionTime time.Duration
}

func (L1Config) Init

type L2Config

type L2Config struct {
	Enabled bool
	Backend BackendType

	Addresses []string // Addresses for the L2 cache
	Database  int      // Database for the L2 cache (for Redis)
	Username  string   // Username for the L2 cache (for Redis)
	Password  string   // Password for the L2 cache (for Redis)
}

func (L2Config) Init

type Option

type Option func(o *Options)

func WithTTL

func WithTTL(ttl time.Duration) Option

func WithTags

func WithTags(tags ...string) Option

type Options

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

func ApplyOptions

func ApplyOptions(opts ...Option) *Options

type Service

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

func New

func New(config Config) *Service

func (*Service) Attach

func (s *Service) Attach(ctx context.Context, db *db.Pool) error

func (*Service) Catalog

func (s *Service) Catalog(ctx context.Context) cs.Source

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, key string) error

func (*Service) Engine

func (s *Service) Engine() engines.Engine

func (*Service) Get

func (s *Service) Get(ctx context.Context, key string) (any, error)

func (*Service) Init

func (s *Service) Init(ctx context.Context) error

func (*Service) Invalidate

func (s *Service) Invalidate(ctx context.Context, tags ...string) error

func (*Service) IsReadonly

func (s *Service) IsReadonly() bool

func (*Service) Load

func (s *Service) Load(ctx context.Context, key string, fn func() (any, error), options ...Option) (any, error)

func (*Service) Name

func (s *Service) Name() string

func (*Service) Set

func (s *Service) Set(ctx context.Context, key string, data any, options ...Option) error

Jump to

Keyboard shortcuts

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