redisx

package
v0.0.33 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package redisx - an extension of redis library.

Index

Constants

View Source
const (
	KeyAddrs    = "server.redis.address"
	KeyMaster   = "server.redis.master.name"
	KeyClient   = "server.redis.client.name"
	KeyUsername = "server.redis.username"
	KeyPassword = "server.redis.password"
	KeyDatabase = "server.redis.database"

	KeyReadTimeout  = "server.redis.read.timeout"
	KeyWriteTimeout = "server.redis.read.timeout"
)

Variables

View Source
var (
	Client redis.UniversalClient
)

Functions

func Del

func Del(keys ...string)

Del - deletes keys in cache server.

func Get

func Get[T any](key string) (*T, error)

Get - retrieves the value of given key stored in redis server. This function call jsonx function to decode string values to given type.

func GetEx

func GetEx[T any](key string, ttl time.Duration) (*T, error)

GetEx - retrieves the value stored in redis server and prolong the TTL of the given key.

func GetExWithProvider added in v0.0.17

func GetExWithProvider[T any](key string, ttl time.Duration, provider Provider[T]) (*T, error)

GetExWithProvider - retrieves value of given key as given type. If the given key is not existed, this function will call given provider function to generate the new data, and set the new generated data to cache storage with given TTL.

This function will exit when the data provider returns an error.

func GetWithProvider added in v0.0.17

func GetWithProvider[T any](key string, ttl time.Duration, provider Provider[T]) (*T, error)

GetWithProvider - retrieves value of given key as given type. If the given key is not existed, this function will call given provider function to generate the new data, and set the new generated data to cache storage with given TTL.

This function will exit when the data provider returns an error.

func NewClient

func NewClient(cfg *Config) (redis.UniversalClient, error)

NewClient - creates a new redis Universal Client. See redis documents for more about redis.UniversalClient.

func Set

func Set(key string, value any, ttl time.Duration) error

Set - stores key-value with given TTL to cache storage.

Types

type Config

type Config struct {
	Addrs        []string
	MasterName   string
	ClientName   string
	Username     string
	Password     string
	Database     int
	ReadTimeout  time.Duration
	WriteTimeout time.Duration
}

Config - a structure holds all configurations.

var (
	Cfg    *Config
	DefCfg = &Config{
		ReadTimeout:  30 * time.Second,
		WriteTimeout: 30 * time.Second,
	}
)

type Provider

type Provider[T any] func() (*T, error)

Provider - a callback function definition for data provider. When we call GetWithProvider() functions we need to specify a provider function to generate data while the cached value is not found.

Jump to

Keyboard shortcuts

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