cache

package
v0.27.11 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2022 License: AGPL-3.0, AGPL-3.0-only Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalMany added in v0.27.6

func MarshalMany[M ~map[K]V, K comparable, V any, F func(t K) string](data M, fn F) map[string]any

func UnmarshalMany added in v0.27.6

func UnmarshalMany[T any, ID comparable, F func(t T) ID](result GetManyResult, fn F) (map[ID]T, error)

Types

type GetManyResult added in v0.27.6

type GetManyResult struct {
	Result map[string][]byte
	Err    error
	// contains filtered or unexported fields
}

type MemoryCache added in v0.27.4

type MemoryCache[K comparable, T any] struct {
	// contains filtered or unexported fields
}

MemoryCache store data in memory, will be used to cache user group permission rule.

func NewMemoryCache

func NewMemoryCache[K comparable, V any]() *MemoryCache[K, V]

NewMemoryCache 不对缓存的对象进行序列化,直接用 sync.Map 保存在内存里。

过期的缓存不会从内存中自动回收,不能用来缓存值空间非常大的数据如条目或用户, 用于缓存用户组权限这样的值空间比较小的数据。

func (*MemoryCache[K, T]) Get added in v0.27.4

func (c *MemoryCache[K, T]) Get(_ context.Context, key K) (T, bool)

func (*MemoryCache[K, T]) Set added in v0.27.4

func (c *MemoryCache[K, T]) Set(_ context.Context, key K, value T, ttl time.Duration)

type RedisCache added in v0.27.4

type RedisCache interface {
	Get(ctx context.Context, key string, value any) (bool, error)
	Set(ctx context.Context, key string, value any, ttl time.Duration) error
	Del(ctx context.Context, keys ...string) error
	SetMany(ctx context.Context, data map[string]any, ttl time.Duration) error
	GetMany(ctx context.Context, keys []string) GetManyResult
}

RedisCache

var s model.Subject
c.Get(ctx, key, &s)
c.Set(ctx, key, s, time.Minute)

SetMany...

err := c.SetMany(ctx, cache.MarshalMany(notCachedSubjects, cachekey.Subject), time.Minute)

GetMany...

result := c.GetMany(ctx, slice.Map(subjectIDs, cachekey.Subject))
var cached map[model.SubjectID]model.Subject
cached, err = cache.UnmarshalMany(result, model.Subject.GetID)

func NewNoop added in v0.27.4

func NewNoop() RedisCache

func NewRedisCache

func NewRedisCache(cli *redis.Client) RedisCache

NewRedisCache create a redis backed cache.

Jump to

Keyboard shortcuts

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