redis

package
v0.0.0-...-cbf21e1 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2019 License: BSD-3-Clause, BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

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
	// DefaultDelim ths redis delim option, "-".
	DefaultDelim = "-"
)

Variables

View Source
var (
	// ErrRedisClosed an error with message 'Redis is already closed'
	ErrRedisClosed = errors.New("Redis is already closed")
	// ErrKeyNotFound an error with message 'Key $thekey doesn't found'
	ErrKeyNotFound = errors.New("Key '%s' doesn't found")
)

Functions

This section is empty.

Types

type Config

type Config 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
	// MaxActive. Defaults to 10.
	MaxActive int
	// Timeout for connect, write and read, defaults to 30 seconds, 0 means no timeout.
	Timeout time.Duration
	// Prefix "myprefix-for-this-website". Defaults to "".
	Prefix string
	// Delim the delimeter for the keys on the sessiondb. Defaults to "-".
	Delim string

	// Driver supports `Redigo()` or `Radix()` go clients for redis.
	// Configure each driver by the return value of their constructors.
	//
	// Defaults to `Redigo()`.
	Driver Driver
}

Config the redis configuration used inside sessions

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default configuration for Redis service.

type Database

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

Database the redis back-end session database for the sessions.

func New

func New(cfg ...Config) *Database

New returns a new redis database.

func (*Database) Acquire

func (db *Database) Acquire(sid string, expires time.Duration) sessions.LifeTime

Acquire receives a session's lifetime from the database, if the return value is LifeTime{} then the session manager sets the life time based on the expiration duration lives in configuration.

func (*Database) Clear

func (db *Database) Clear(sid string)

Clear removes all session key values but it keeps the session entry.

func (*Database) Close

func (db *Database) Close() error

Close terminates the redis connection.

func (*Database) Config

func (db *Database) Config() *Config

Config returns the configuration for the redis server bridge, you can change them.

func (*Database) Delete

func (db *Database) Delete(sid string, key string) (deleted bool)

Delete removes a session key value based on its key.

func (*Database) Get

func (db *Database) Get(sid string, key string) (value interface{})

Get retrieves a session value based on the key.

func (*Database) Len

func (db *Database) Len(sid string) (n int)

Len returns the length of the session's entries (keys).

func (*Database) OnUpdateExpiration

func (db *Database) OnUpdateExpiration(sid string, newExpires time.Duration) error

OnUpdateExpiration will re-set the database's session's entry ttl. https://redis.io/commands/expire#refreshing-expires

func (*Database) Release

func (db *Database) Release(sid string)

Release destroys the session, it clears and removes the session entry, session manager will create a new session ID on the next request after this call.

func (*Database) Set

func (db *Database) Set(sid string, lifetime sessions.LifeTime, key string, value interface{}, immutable bool)

Set sets a key value of a specific session. Ignore the "immutable".

func (*Database) Visit

func (db *Database) Visit(sid string, cb func(key string, value interface{}))

Visit loops through all session keys and values.

type Driver

type Driver interface {
	Connect(c Config) error
	PingPong() (bool, error)
	CloseConnection() error
	Set(key string, value interface{}, secondsLifetime int64) error
	Get(key string) (interface{}, error)
	TTL(key string) (seconds int64, hasExpiration bool, found bool)
	UpdateTTL(key string, newSecondsLifeTime int64) error
	UpdateTTLMany(prefix string, newSecondsLifeTime int64) error
	GetAll() (interface{}, error)
	GetKeys(prefix string) ([]string, error)
	Delete(key string) error
}

Driver is the interface which each supported redis client should support in order to be used in the redis session database.

type RadixDriver

type RadixDriver struct {
	// Connected is true when the Service has already connected
	Connected bool
	// Config the read-only redis database config.
	Config Config
	// contains filtered or unexported fields
}

RadixDriver the Redis service based on the radix go client, contains the config and the redis pool.

func Radix

func Radix() *RadixDriver

Radix returns the driver for the radix go redis client.

func (*RadixDriver) CloseConnection

func (r *RadixDriver) CloseConnection() error

CloseConnection closes the redis connection.

func (*RadixDriver) Connect

func (r *RadixDriver) Connect(c Config) error

Connect connects to the redis, called only once

func (*RadixDriver) Delete

func (r *RadixDriver) Delete(key string) error

Delete removes redis entry by specific key

func (*RadixDriver) Get

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

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

func (*RadixDriver) GetAll

func (r *RadixDriver) GetAll() (interface{}, error)

GetAll returns all redis entries using the "SCAN" command (2.8+).

func (*RadixDriver) GetKeys

func (r *RadixDriver) GetKeys(prefix string) ([]string, error)

GetKeys returns all redis keys using the "SCAN" with MATCH command. Read more at: https://redis.io/commands/scan#the-match-option.

func (*RadixDriver) PingPong

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

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

func (*RadixDriver) Set

func (r *RadixDriver) Set(key string, value interface{}, secondsLifetime int64) error

Set sets a key-value to the redis store. The expiration is setted by the secondsLifetime.

func (*RadixDriver) TTL

func (r *RadixDriver) TTL(key string) (seconds int64, hasExpiration bool, found bool)

TTL returns the seconds to expire, if the key has expiration and error if action failed. Read more at: https://redis.io/commands/ttl

func (*RadixDriver) UpdateTTL

func (r *RadixDriver) UpdateTTL(key string, newSecondsLifeTime int64) error

UpdateTTL will update the ttl of a key. Using the "EXPIRE" command. Read more at: https://redis.io/commands/expire#refreshing-expires

func (*RadixDriver) UpdateTTLMany

func (r *RadixDriver) UpdateTTLMany(prefix string, newSecondsLifeTime int64) error

UpdateTTLMany like `UpdateTTL` but for all keys starting with that "prefix", it is a bit faster operation if you need to update all sessions keys (although it can be even faster if we used hash but this will limit other features), look the `sessions/Database#OnUpdateExpiration` for example.

type RedigoDriver

type RedigoDriver struct {
	// Config the read-only redis database config.
	Config Config

	// Maximum number of idle connections in the pool.
	MaxIdle 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

	// Connected is true when the Service has already connected
	Connected bool
	// contains filtered or unexported fields
}

RedigoDriver is the redigo Redis go client, contains the config and the redis pool

func Redigo

func Redigo() *RedigoDriver

Redigo returns the driver for the redigo go redis client. Which is the default one. You can customize further any specific driver's properties.

func (*RedigoDriver) CloseConnection

func (r *RedigoDriver) CloseConnection() error

CloseConnection closes the redis connection.

func (*RedigoDriver) Connect

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

Connect connects to the redis, called only once

func (*RedigoDriver) Delete

func (r *RedigoDriver) Delete(key string) error

Delete removes redis entry by specific key

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) GetAll

func (r *RedigoDriver) GetAll() (interface{}, error)

GetAll returns all redis entries using the "SCAN" command (2.8+).

func (*RedigoDriver) GetBytes

func (r *RedigoDriver) GetBytes(key string) ([]byte, error)

GetBytes returns value, err by its key you can use utils.Deserialize((.GetBytes("yourkey"),&theobject{}) returns nil and a filled error if something wrong happens

func (*RedigoDriver) GetKeys

func (r *RedigoDriver) GetKeys(prefix string) ([]string, error)

GetKeys returns all redis keys using the "SCAN" with MATCH command. Read more at: https://redis.io/commands/scan#the-match-option.

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{}, secondsLifetime int64) (err error)

Set sets a key-value to the redis store. The expiration is setted by the secondsLifetime.

func (*RedigoDriver) TTL

func (r *RedigoDriver) TTL(key string) (seconds int64, hasExpiration bool, found bool)

TTL returns the seconds to expire, if the key has expiration and error if action failed. Read more at: https://redis.io/commands/ttl

func (*RedigoDriver) UpdateTTL

func (r *RedigoDriver) UpdateTTL(key string, newSecondsLifeTime int64) error

UpdateTTL will update the ttl of a key. Using the "EXPIRE" command. Read more at: https://redis.io/commands/expire#refreshing-expires

func (*RedigoDriver) UpdateTTLMany

func (r *RedigoDriver) UpdateTTLMany(prefix string, newSecondsLifeTime int64) error

UpdateTTLMany like `UpdateTTL` but for all keys starting with that "prefix", it is a bit faster operation if you need to update all sessions keys (although it can be even faster if we used hash but this will limit other features), look the `sessions/Database#OnUpdateExpiration` for example.

Jump to

Keyboard shortcuts

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