cachepool

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultPrefix is cache key prefix, it is to distinguish which app the key belongs to.
	DefaultPrefix = ""
	// DefaultDelim is cache key delimitation option, "-".
	DefaultDelim = "-"
)
View Source
const (
	// DefaultRedisNetwork the redis network option, "tcp".
	DefaultRedisNetwork = "tcp"
	// DefaultRedisAddr the redis address option, "127.0.0.1:6379".
	DefaultRedisAddr = "127.0.0.1:6379"
	// DefaultRedisTimeout the redis idle timeout option, time.Duration(30) * time.Second
	DefaultRedisTimeout = time.Duration(30) * time.Second
)

Variables

View Source
var (
	// ErrRedisClosed an error with message 'redis: already closed'
	ErrRedisClosed = errors.New("redis: already closed")
	// ErrKeyNotFound a type of error of non-existing redis keys.
	// The producers(the library) of this error will dynamically wrap this error(fmt.Errorf) with the key name.
	// Usage:
	// if err != nil && errors.Is(err, ErrKeyNotFound) {
	// [...]
	// }
	ErrKeyNotFound = errors.New("key not found")
)

Functions

This section is empty.

Types

type Cache

type Cache struct {

	// Prefix "myprefix-for-this-website". Defaults to "".
	Prefix string
	// Delim the delimeter for the keys on the sessiondb. Defaults to "-".
	Delim string
	// contains filtered or unexported fields
}

Cache is cache pool client

func DefaultCache

func DefaultCache() *Cache

DefaultCache default cache pool client

func NewCache

func NewCache(config *Configuration, driver CacheDriver) *Cache

NewCache new a custom configuration cache

func (*Cache) Close

func (cache *Cache) Close() error

Close close cache pool

func (*Cache) Driver

func (cache *Cache) Driver() CacheDriver

Driver return cache's driver

func (*Cache) Get

func (cache *Cache) Get(key string) (interface{}, error)

Get retrive the key from cache storage

func (*Cache) Set

func (cache *Cache) Set(key string, value interface{}) error

Set set the cache key

func (*Cache) SetDelim

func (cache *Cache) SetDelim(delim string)

SetDelim set the cache key delimitation

func (*Cache) SetPrefix

func (cache *Cache) SetPrefix(prefix string)

SetPrefix set the cache key prefix

type CacheDriver

type CacheDriver interface {
	Connect(Configuration) error
	CloseConnection() error
	NativePool() *redis.Pool
	PingPong() (bool, error)
	Set(key string, value interface{}) error
	SetEX(key string, value interface{}, expirationtime int64) error
	Get(key string) (interface{}, error)
}

CacheDriver is cache pool driver interface

type Configuration added in v0.1.0

type Configuration struct {
	// Network protocol. Defaults to "tcp".
	Network string
	// Addr of a single redis server instance.
	// See "Clusters" field for clusters support.
	// Defaults to "127.0.0.1:6379".
	Addr string
	// Clusters a list of network addresses for clusters.
	// If not empty "Addr" is ignored.
	// Currently only Radix() Driver supports it.
	Clusters []string
	// Password string .If no password then no 'AUTH'. Defaults to "".
	Password string
	// If Database is empty "" then no 'SELECT'. Defaults to "".
	Database string

	// Timeout for connect, write and read, defaults to 30 seconds, 0 means no timeout.
	Timeout time.Duration

	// TLSConfig will cause Dial to perform a TLS handshake using the provided
	// config. If is nil then no TLS is used.
	// See https://golang.org/pkg/crypto/tls/#Config
	TLSConfig *tls.Config
}

Configuration the redis configuration

func DefaultConfig

func DefaultConfig() *Configuration

DefaultConfig is default cache pool configuration

func (*Configuration) Configure added in v0.1.0

func (config *Configuration) Configure(configurators ...Configurator)

Configure is to configure the Configuration struct once

func (*Configuration) SetAddr added in v0.1.0

func (config *Configuration) SetAddr(addr string)

SetAddr set redis address option, addr format is "127.0.0.1:6379".

func (*Configuration) SetDatabase added in v0.1.0

func (config *Configuration) SetDatabase(db string)

SetDatabase set redis db, use it by "select db"

func (*Configuration) SetNetWork added in v0.1.0

func (config *Configuration) SetNetWork(network string)

SetNetWork set redis network options, default is tcp

func (*Configuration) SetPassword added in v0.1.0

func (config *Configuration) SetPassword(password string)

SetPassword set password when connect redis

func (*Configuration) SetTimeOut added in v0.1.0

func (config *Configuration) SetTimeOut(timeout time.Duration)

SetTimeOut set redis connect/read/write timeout

type Configurator added in v0.1.0

type Configurator func(conf *Configuration)

func WithAddr added in v0.1.0

func WithAddr(addr string) Configurator

func WithDatabase added in v0.1.0

func WithDatabase(db string) Configurator

func WithNetWork added in v0.1.0

func WithNetWork(network string) Configurator

func WithPassword added in v0.1.0

func WithPassword(password string) Configurator

func WithTimeOut added in v0.1.0

func WithTimeOut(timeout time.Duration) Configurator

type RedigoDriver

type RedigoDriver struct {

	// Maximum number of idle connections in the pool.
	MaxIdle int
	// Maximum number of connections allocated by the pool at a given time.
	// When zero, there is no limit on the number of connections in the pool.
	MaxActive int

	// Close connections after remaining idle for this duration. If the value
	// is zero, then idle connections are not closed. Applications should set
	// the timeout to a value less than the server's timeout.
	IdleTimeout time.Duration

	// If Wait is true and the pool is at the MaxActive limit, then Get() waits
	// for a connection to be returned to the pool before returning.
	Wait bool

	// Close connections older than this duration. If the value is zero, then
	// the pool does not close connections based on age.
	MaxConnLifetime time.Duration
	// contains filtered or unexported fields
}

RedigoDriver implement CacheDriver interface, it is the redigo Redis go client, contains the config and the redis pool

func NewDriver

func NewDriver(r ...RedigoDriver) *RedigoDriver

NewDriver is for new redigo driver

func (*RedigoDriver) CloseConnection

func (r *RedigoDriver) CloseConnection() error

CloseConnection close driver pool

func (*RedigoDriver) Connect

func (r *RedigoDriver) Connect(c Configuration) error

Connect connects to the redis by redigo driver pool, called only once.

func (*RedigoDriver) Get

func (r *RedigoDriver) Get(key string) (interface{}, error)

Get returns value, err by its key returns nil and a filled error if something bad happened.

func (*RedigoDriver) NativePool added in v0.0.2

func (r *RedigoDriver) NativePool() *redis.Pool

NativePool is to get native redis pool

func (*RedigoDriver) PingPong

func (r *RedigoDriver) PingPong() (bool, error)

PingPong sends a ping and receives a pong, if no pong received then returns false and filled error

func (*RedigoDriver) Set

func (r *RedigoDriver) Set(key string, value interface{}) (err error)

Set sets a key-value to the redis store.

func (*RedigoDriver) SetEX

func (r *RedigoDriver) SetEX(key string, value interface{}, expirationtime int64) (err error)

SetEX sets a key-value to the redis store with expiration time.

Jump to

Keyboard shortcuts

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