cache

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const KeepExpiration = -1

KeepExpiration keeps current expiration for existing value

View Source
const Nil = redis.Nil

Nil error returned when key does not exist.

Variables

This section is empty.

Functions

func Deserialize

func Deserialize(byt []byte, ptr any) (err error)

Deserialize deserializes the passed []byte into the passed ptr any

func Serialize

func Serialize(value any) ([]byte, error)

Serialize returns a []byte representing the passed value

Types

type Client

type Client interface {
	// GetRawClient gets underlying client object
	GetRawClient() any

	// Add adds key if key exists. When key does not exist, no operation is performed.
	// Zero expiration means the key has no expiration time; KeepExpiration keeps existing expiration.
	// Returns whether the key is added.
	Add(ctx context.Context, key string, value any, expiration time.Duration) (bool, error)

	// Delete removes the keys. When key does not exist, no operation is performed.
	// Returns the number of keys that were removed.
	Delete(ctx context.Context, keys ...string) (int, error)

	// Exists checks if keys exist.
	// Returns the number of keys that exist.
	Exists(ctx context.Context, keys ...string) (int, error)

	// Expire updates the expiration of key. When key does not exist, no operation is performed.
	// Returns whether the expiration is updated.
	Expire(ctx context.Context, key string, expiration time.Duration) (bool, error)

	// Get gets value of key.
	// `value` should be initialized to a pointer to output data type.
	// If the key does not exist, returns `Nil` error
	Get(ctx context.Context, key string, value any) error

	// IncrBy increments the number stored at key by `value`.
	// If the key does not exist, it is set to 0 before performing the operation.
	// `value` can be either positive or negative integer.
	// Returns the value after increment.
	IncrBy(ctx context.Context, key string, value int64) (int64, error)

	// MGet gets values of multiple keys.
	// Map value should be initialized to pointer to output data type.
	// If the key does not exist, the value will be set to nil.
	MGet(ctx context.Context, kv map[string]any) error

	// MSet sets values of multiple keys.
	MSet(ctx context.Context, kv map[string]any) error

	// Set adds or updates key.
	// Zero expiration means the key has no expiration time; KeepExpiration keeps existing expiration.
	Set(ctx context.Context, key string, value any, expiration time.Duration) error

	// Update updates key if key does not exist. When key already holds a value, no operation is performed.
	// Zero expiration means the key has no expiration time; KeepExpiration keeps existing expiration.
	// Returns whether the key is updated.
	Update(ctx context.Context, key string, value any, expiration time.Duration) (bool, error)
}

Client is a Cache client representing a connection pool. It is safe for concurrent use by multiple goroutines.

func NewRedisClient

func NewRedisClient(config *Config, opts ...Option) (Client, error)

type ClientManager

type ClientManager interface {
	GetClient(name string) Client
}

func NewClientManager

func NewClientManager(configs []Config, opts ...Option) (ClientManager, error)

type Config

type Config struct {
	// Name of the client.
	Name string

	// Type of Cache client. Choices: "redis"
	Type string

	// host:port address.
	Address string

	// Optional. Use the specified Username to authenticate the current connection.
	Username string

	// Optional. Password for authentication.
	Password string

	// Database to be selected after connecting to the server.
	DB uint32
}

type Logging

type Logging interface {
	Printf(ctx context.Context, format string, v ...interface{})
}

func NewLoggerWithZerolog

func NewLoggerWithZerolog(logger *zerolog.Logger) Logging

NewLoggerWithZerolog creates a new logger which wraps the given zerolog.Logger

type Option

type Option func(*options)

func WithLogger

func WithLogger(logger *zerolog.Logger) Option

Jump to

Keyboard shortcuts

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