cache

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 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 added in v1.1.3

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 added in v1.1.3

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 added in v1.1.3

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 added in v1.1.3

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

JSONResult stores a JSON string.

func (*JSONResult) JSON added in v1.1.3

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

JSON returns the JSON encoding of the stored value.

func (*JSONResult) Load added in v1.1.3

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 added in v1.1.3

type OptionArg struct {
	ExpireAfterWrite time.Duration
}

type Result

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

Result stores a value.

func NewJSONResult added in v1.1.3

func NewJSONResult(v string) Result

NewJSONResult returns a Result which stores a JSON string.

func NewValueResult added in v1.1.3

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 added in v1.1.3

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

A Shardable cache can improve the performance of concurrency.

type Storage added in v1.1.3

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

Storage is a Shardable cache implementation.

func NewStorage added in v1.1.3

func NewStorage(size int, hash HashFunc) *Storage

NewStorage returns a new *Storage.

func (*Storage) Reset added in v1.1.3

func (s *Storage) Reset()

Reset resets the Storage.

func (*Storage) Sharding added in v1.1.3

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

Sharding returns the shard of the key.

type ValueResult added in v1.1.3

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

ValueResult stores a `reflect.Value`.

func (*ValueResult) JSON added in v1.1.3

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

JSON returns the JSON encoding of the stored value.

func (*ValueResult) Load added in v1.1.3

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