cache

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Driver

type Driver[V any] interface {
	// Add Store an item in the cache if the key doesn't exist.
	Add(key string, value V, t time.Duration) error
	// Put Store an item in the cache for a given number of seconds.
	Put(key string, value V, t time.Duration) error
	// PutMany Store multiple items in the cache for a given number of seconds.
	PutMany(many []Many[V]) error
	// Forever Store an item in the cache indefinitely.
	Forever(key string, value V) error
	// Forget Remove an item from the cache.
	Forget(key string) error
	// Flush Remove all items from the cache.
	Flush() error
	// Get Retrieve an item from the cache by key.
	Get(key string) (V, error)
	// Has Determine if an item exists in the cache.
	Has(key string) (bool, error)
	// Many Retrieve multiple items from the cache by key.
	// Items not found in the cache will have a nil value.
	Many(keys []string) (map[string]V, error)
	// SetInt64 set the int64 value of an item in the cache.
	SetInt64(key string, value int64, t time.Duration) error
	// IncrementInt64 Increment the value of an item in the cache.
	IncrementInt64(key string, value int64) (int64, error)
	// DecrementInt64 Decrement the value of an item in the cache.
	DecrementInt64(key string, value int64) (int64, error)
	// Remember Get an item from the cache, or execute the given Closure and store the result.
	Remember(key string, ttl time.Duration, callback func() (V, error)) (V, error)
	// RememberForever Get an item from the cache, or execute the given Closure and store the result forever.
	RememberForever(key string, callback func() (V, error)) (V, error)
}

func New

func New[V any](driver DriverType, optionFns ...OptionFunc) (Driver[V], error)

type DriverType

type DriverType string
const (
	DriverRedis  DriverType = "redis"
	DriverMemory DriverType = "memory"
)

type GoCacheDriver

type GoCacheDriver[V any] struct {
	// contains filtered or unexported fields
}

func (*GoCacheDriver[V]) Add

func (g *GoCacheDriver[V]) Add(key string, value V, t time.Duration) error

func (*GoCacheDriver[V]) DecrementInt64

func (g *GoCacheDriver[V]) DecrementInt64(key string, value int64) (int64, error)

func (*GoCacheDriver[V]) Flush

func (g *GoCacheDriver[V]) Flush() error

func (*GoCacheDriver[V]) Forever

func (g *GoCacheDriver[V]) Forever(key string, value V) error

func (*GoCacheDriver[V]) Forget

func (g *GoCacheDriver[V]) Forget(key string) error

func (*GoCacheDriver[V]) Get

func (g *GoCacheDriver[V]) Get(key string) (result V, err error)

func (*GoCacheDriver[V]) Has

func (g *GoCacheDriver[V]) Has(key string) (bool, error)

func (*GoCacheDriver[V]) IncrementInt64

func (g *GoCacheDriver[V]) IncrementInt64(key string, value int64) (int64, error)

func (*GoCacheDriver[V]) Many

func (c *GoCacheDriver[V]) Many(keys []string) (map[string]V, error)

func (*GoCacheDriver[V]) Put

func (c *GoCacheDriver[V]) Put(key string, value V, t time.Duration) error

func (*GoCacheDriver[V]) PutMany

func (c *GoCacheDriver[V]) PutMany(many []Many[V]) error

func (*GoCacheDriver[V]) Remember

func (g *GoCacheDriver[V]) Remember(key string, ttl time.Duration, callback func() (V, error)) (result V, err error)

func (*GoCacheDriver[V]) RememberForever

func (g *GoCacheDriver[V]) RememberForever(key string, callback func() (V, error)) (V, error)

func (*GoCacheDriver[V]) SetInt64

func (g *GoCacheDriver[V]) SetInt64(key string, value int64, t time.Duration) error

type JSONSerializer

type JSONSerializer struct{}

func (*JSONSerializer) Serialize

func (d *JSONSerializer) Serialize(v any) ([]byte, error)

Serialize

@Description: Serialize data
@receiver d
@param v
@return []byte
@return error

func (*JSONSerializer) UnSerialize

func (d *JSONSerializer) UnSerialize(data []byte, v any) error

UnSerialize

@Description: UnSerialize data
@receiver d
@param data
@param v
@return error

type Many

type Many[V any] struct {
	Key   string
	Value V
	TTL   time.Duration
}

type OptionFunc

type OptionFunc func(driver *baseDriver) error

func WithCtx

func WithCtx(ctx context.Context) OptionFunc

WithCtx

@Description:
@param ctx
@return OptionFunc

func WithMemCache

func WithMemCache(memCache *gocache.Cache) OptionFunc

WithMemCache

@Description:
@param memCache
@return OptionFunc

func WithPrefix

func WithPrefix(prefix string) OptionFunc

WithPrefix

@Description:
@param prefix
@return OptionFunc

func WithRedisClient

func WithRedisClient(redis *redis.Client) OptionFunc

WithRedisClient

@Description:
@param redis
@return OptionFunc

func WithSerializer

func WithSerializer(serializer Serializer) OptionFunc

WithSerializer

@Description: set a cache serializer
@param serializer
@return OptionFunc

type RedisDriver

type RedisDriver[V any] struct {
	// contains filtered or unexported fields
}

func (*RedisDriver[V]) Add

func (rc *RedisDriver[V]) Add(key string, value V, t time.Duration) error

func (*RedisDriver[V]) DecrementInt64

func (rc *RedisDriver[V]) DecrementInt64(key string, value int64) (int64, error)

func (*RedisDriver[V]) Flush

func (rc *RedisDriver[V]) Flush() error

func (*RedisDriver[V]) Forever

func (rc *RedisDriver[V]) Forever(key string, value V) error

func (*RedisDriver[V]) Forget

func (rc *RedisDriver[V]) Forget(key string) error

func (*RedisDriver[V]) Get

func (rc *RedisDriver[V]) Get(key string) (V, error)

func (*RedisDriver[V]) Has

func (rc *RedisDriver[V]) Has(key string) (bool, error)

func (*RedisDriver[V]) IncrementInt64

func (rc *RedisDriver[V]) IncrementInt64(key string, value int64) (int64, error)

func (*RedisDriver[V]) Many

func (rc *RedisDriver[V]) Many(keys []string) (map[string]V, error)

func (*RedisDriver[V]) Put

func (rc *RedisDriver[V]) Put(key string, value V, t time.Duration) error

func (*RedisDriver[V]) PutMany

func (rc *RedisDriver[V]) PutMany(many []Many[V]) error

func (*RedisDriver[V]) Remember

func (rc *RedisDriver[V]) Remember(key string, ttl time.Duration, callback func() (V, error)) (result V, err error)

func (*RedisDriver[V]) RememberForever

func (rc *RedisDriver[V]) RememberForever(key string, callback func() (V, error)) (V, error)

func (*RedisDriver[V]) SetInt64

func (rc *RedisDriver[V]) SetInt64(key string, value int64, t time.Duration) error

type Serializer

type Serializer interface {
	Serialize(v any) ([]byte, error)
	UnSerialize(data []byte, v any) error
}

Jump to

Keyboard shortcuts

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