cache

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package cache provides object caching in memory.

Index

Constants

This section is empty.

Variables

View Source
var (
	Cache  Shardable
	Engine Driver
)

Functions

func Has

func Has(key string) bool

Has returns whether the key exists.

func Load

func Load(ctx context.Context, key string, loader Loader, opts ...Option) (loadType LoadType, result Result, _ error)

Load loads value from cache, if there is no cached value, call the loader to get value, and then stores it.

Types

type Driver

type Driver interface {
	Load(ctx context.Context, m *sync.Map, key string, loader ResultLoader, arg OptionArg) (loadType LoadType, result Result, err error)
}

Driver loads value from m, if there is no cached value, call the loader to get value, and then stores it into m.

type HashFunc

type HashFunc func(key string) int

HashFunc returns the hash value of the key.

var (
	SimpleHash HashFunc = func(key string) int {
		const max = 20
		n := len(key)
		if n > max {
			n = max
		}
		c := 0
		for i := 0; i < n; i++ {
			c += int(key[i])
		}
		return c
	}
)

type JSONResult

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

JSONResult stores a JSON string.

func (*JSONResult) JSON

func (r *JSONResult) JSON() (string, error)

JSON returns the JSON encoding of the stored value.

func (*JSONResult) Load

func (r *JSONResult) Load(v interface{}) error

Load injects the saved value to v.

type LoadType

type LoadType int
const (
	LoadNone LoadType = iota
	LoadOnCtx
	LoadCache
	LoadSource
)

type Loader

type Loader func(ctx context.Context, key string) (interface{}, error)

Loader gets value from a background, such as Redis, MySQL, etc.

type Option

type Option func(*OptionArg)

func ExpireAfterWrite

func ExpireAfterWrite(v time.Duration) Option

ExpireAfterWrite sets the expiration time of the cache value after write.

type OptionArg

type OptionArg struct {
	ExpireAfterWrite time.Duration
}

type Result

type Result interface {
	JSON() (string, error)
	Load(v interface{}) error
}

Result stores a value.

func NewJSONResult

func NewJSONResult(v string) Result

NewJSONResult returns a Result which stores a JSON string.

func NewValueResult

func NewValueResult(v interface{}) Result

NewValueResult returns a Result which stores a `reflect.Value`.

type ResultLoader

type ResultLoader func(ctx context.Context, key string) (Result, error)

ResultLoader returns a wrapper for the source value of the key.

type Shardable

type Shardable interface {
	Sharding(key string) *sync.Map
}

A Shardable cache can improve the performance of concurrency.

type Storage

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

Storage is a Shardable cache implementation.

func NewStorage

func NewStorage(size int, hash HashFunc) *Storage

NewStorage returns a new *Storage.

func (*Storage) Reset

func (s *Storage) Reset()

Reset resets the Storage.

func (*Storage) Sharding

func (s *Storage) Sharding(key string) *sync.Map

Sharding returns the shard of the key.

type ValueResult

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

ValueResult stores a `reflect.Value`.

func (*ValueResult) JSON

func (r *ValueResult) JSON() (string, error)

JSON returns the JSON encoding of the stored value.

func (*ValueResult) Load

func (r *ValueResult) Load(v interface{}) error

Load injects the saved value to v.

Jump to

Keyboard shortcuts

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