redisc

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2023 License: MIT Imports: 16 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCacheMiss = errors.New("cache: key is missing")
)

Functions

This section is empty.

Types

type Cache

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

func NewCache

func NewCache(opt *Options) *Cache

func (*Cache) CleanLocalCache added in v0.1.0

func (cd *Cache) CleanLocalCache()

func (*Cache) Delete

func (cd *Cache) Delete(ctx context.Context, key string) error

func (*Cache) DeleteFromLocalCache

func (cd *Cache) DeleteFromLocalCache(key string)

func (*Cache) Exists

func (cd *Cache) Exists(ctx context.Context, key string) bool

Exists reports whether value for the given key exists. exists call get ,ttl is unknown

func (*Cache) Get

func (cd *Cache) Get(ctx context.Context, key string, value any) error

Get gets the value for the given key.

func (*Cache) GetSkip

func (cd *Cache) GetSkip(ctx context.Context, key string, value any, mode SkipMode) error

GetSkip gets the value for the given key skipping by special.

func (*Cache) GetSkippingLocalCache

func (cd *Cache) GetSkippingLocalCache(ctx context.Context, key string, value any) error

GetSkippingLocalCache gets the value for the given key skipping local cache.

func (*Cache) Marshal

func (cd *Cache) Marshal(value any) ([]byte, error)

func (*Cache) Once

func (cd *Cache) Once(item *Item) error

Once gets the item.Value for the given item.Key from the cache or executes, caches, and returns the results of the given item.Func, making sure that only one execution is in-flight for a given item.Key at a time. If a duplicate comes in, the duplicate caller waits for the original to complete and receives the same results.

func (*Cache) Set

func (cd *Cache) Set(item *Item) error

Set caches the item. Notice: If you use SkipXXX flags, you must Use GetSkip() to get the value. once you use unmatched method such as Get/Exists to get or check the value, the local cache don't know the flags and will cause mess .

func (*Cache) Stats

func (cd *Cache) Stats() *Stats

Stats returns cache statistics.

func (*Cache) Take

func (cd *Cache) Take(item *Item) error

Take gets the item.Value for the given item.Key from the cache or executes, caches, and returns the results of the given item.Func, unlike Once, Take doesn't care about the duplicate caller.

func (*Cache) Unmarshal

func (cd *Cache) Unmarshal(b []byte, value any) error

type Item

type Item struct {
	Ctx context.Context

	Key   string
	Value any

	// TTL is the cache expiration time.
	// Default TTL is 1 hour.
	TTL time.Duration

	// Do returns value to be cached.
	Do func(*Item) (any, error)

	// SetXX only sets the key if it already exists.
	SetXX bool

	// SetNX only sets the key if it does not already exist.
	SetNX bool

	// SkipFlags indicator skip level.
	Skip SkipMode
}

func (*Item) Context

func (item *Item) Context() context.Context

type LocalCache

type LocalCache interface {
	// Set sets a value for a key. ttl maybe has a rule in Local Cache
	Set(key string, data []byte, ttl time.Duration)
	Get(key string) ([]byte, bool)
	Del(key string)
	// Clean removes all items from the cache in memory.
	Clean()
}

LocalCache is a local cache to store a marshalled data in memory.

type MarshalFunc

type MarshalFunc func(interface{}) ([]byte, error)

------------------------------------------------------------------------------

type Option added in v0.1.0

type Option func(*Redisc)

func WithRedisClient added in v0.1.0

func WithRedisClient(cli redis.UniversalClient) Option

type Options

type Options struct {
	Redis         rediser
	LocalCache    LocalCache
	LocalCacheTTL time.Duration
	StatsEnabled  bool
	Marshal       MarshalFunc
	Unmarshal     UnmarshalFunc
}

type Redisc

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

Redisc implement github.com/tsingsun/woocoo/cache/Cache

if you want to register to cache manager, set a `driverName` in configuration

func New

func New(cfg *conf.Configuration, opts ...Option) *Redisc

func NewBuiltIn

func NewBuiltIn() *Redisc

func (*Redisc) Apply

func (c *Redisc) Apply(cfg *conf.Configuration)

Apply conf.configurable

func (*Redisc) Del

func (c *Redisc) Del(ctx context.Context, key string) error

Del deletes the given key.

func (*Redisc) Get

func (c *Redisc) Get(ctx context.Context, key string, v any) error

Get returns the value associated with the given key.

func (*Redisc) Has

func (c *Redisc) Has(ctx context.Context, key string) bool

Has returns true if the given key exists.

func (*Redisc) IsNotFound

func (c *Redisc) IsNotFound(err error) bool

IsNotFound returns true if the error is cache.ErrCacheMiss.

func (*Redisc) LocalCacheEnabled added in v0.1.0

func (c *Redisc) LocalCacheEnabled() bool

LocalCacheEnabled returns true if local cache is enabled.

func (*Redisc) Operator

func (c *Redisc) Operator() *Cache

Operator returns the underlying Redisc.

func (*Redisc) RedisClient

func (c *Redisc) RedisClient() redis.Cmdable

RedisClient returns the underlying redis client.

func (*Redisc) Register

func (c *Redisc) Register() error

func (*Redisc) Set

func (c *Redisc) Set(ctx context.Context, key string, v any, ttl time.Duration) error

Set sets the value associated with the given key. if ttl < 0 ,will not save to redis,but save to local cache if enabled

func (*Redisc) Take

func (c *Redisc) Take(ctx context.Context, v any, key string, ttl time.Duration, query func() (any, error)) error

Take returns the value associated with the given key. It Uses flight control to avoid concurrent requests.

type SkipMode

type SkipMode int
const (
	SkipLocal SkipMode = 1 << iota
	SkipRedis

	SkipAll = SkipLocal | SkipRedis
)

func (SkipMode) Any

func (f SkipMode) Any() bool

Any returns true if the skip annotation was set.

func (SkipMode) Is

func (f SkipMode) Is(mode SkipMode) bool

Is checks if the skip annotation has a specific flag.

type Stats

type Stats struct {
	Hits   uint64
	Misses uint64
}

Stats is the redis cache analyzer.

type TinyLFU

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

func NewTinyLFU

func NewTinyLFU(size int, ttl time.Duration) *TinyLFU

func (*TinyLFU) Clean added in v0.1.0

func (c *TinyLFU) Clean()

func (*TinyLFU) Del

func (c *TinyLFU) Del(key string)

func (*TinyLFU) Get

func (c *TinyLFU) Get(key string) ([]byte, bool)

func (*TinyLFU) Set

func (c *TinyLFU) Set(key string, b []byte, ttl time.Duration)

func (*TinyLFU) UseRandomizedTTL

func (c *TinyLFU) UseRandomizedTTL(offset time.Duration)

type UnmarshalFunc

type UnmarshalFunc func([]byte, interface{}) error

------------------------------------------------------------------------------

Jump to

Keyboard shortcuts

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