redis

package
v12.2.11 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: BSD-3-Clause Imports: 11 Imported by: 18

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
)
View Source
const SessionIDKey = "session_id"

SessionIDKey the session ID stored to the redis session itself.

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 ClusterOptions added in v12.2.0

type ClusterOptions = redis.ClusterOptions

ClusterOptions is just a type alias for the go-redis Cluster Client Options.

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 and Redis clusters feature is used instead.
	// Note that this field is ignored when setgging a custom `GoRedisClient`.
	Clusters []string
	// Use the specified Username to authenticate the current connection
	// with one of the connections defined in the ACL list when connecting
	// to a Redis 6.0 instance, or greater, that is using the Redis ACL system.
	Username string
	// Optional password. Must match the password specified in the
	// requirepass server configuration option (if connecting to a Redis 5.0 instance, or lower),
	// or the User Password when connecting to a Redis 6.0 instance, or greater,
	// that is using the Redis ACL system.
	Password string
	// If Database is empty "" then no 'SELECT'. Defaults to "".
	Database string
	// Maximum number of socket connections.
	// Default is 10 connections per every CPU as reported by runtime.NumCPU.
	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

	// 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

	// A Driver should support be a go client for redis communication.
	// It can be set to a custom one or a mock one (for testing).
	//
	// Defaults to `GoRedis()`.
	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 sessions database.

func (*Database) Acquire

func (db *Database) Acquire(sid string, expires time.Duration) memstore.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) error

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) Decode added in v12.2.0

func (db *Database) Decode(sid, key string, outPtr interface{}) error

Decode binds the "outPtr" to the value associated to the provided "key".

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

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, key string, value interface{}, _ time.Duration, _ bool) error

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

func (*Database) SetLogger added in v12.2.0

func (db *Database) SetLogger(logger *golog.Logger)

SetLogger sets the logger once before server ran. By default the Iris one is injected.

func (*Database) Visit

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

Visit loops through all session keys and values.

type Driver

type Driver interface {
	Connect(c Config) error
	PingPong() (bool, error)
	CloseConnection() error
	Set(sid, key string, value interface{}) error
	Get(sid, key string) (interface{}, error)
	Exists(sid string) bool
	TTL(sid string) time.Duration
	UpdateTTL(sid string, newLifetime time.Duration) error
	GetAll(sid string) (map[string]string, error)
	GetKeys(sid string) ([]string, error)
	Len(sid string) int
	Delete(sid, 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 GoRedisClient added in v12.2.0

type GoRedisClient interface {
	redis.Cmdable // Commands.
	io.Closer     // CloseConnection.
}

GoRedisClient is the interface which both go-redis's Client and Cluster Client implements.

type GoRedisDriver added in v12.2.0

type GoRedisDriver struct {
	// Both Client and ClusterClient implements this interface.
	// Custom one can be directly passed but if so, the
	// Connect method does nothing (so all connection and client settings are ignored).
	Client GoRedisClient
	// Customize any go-redis fields manually
	// before Connect.
	ClientOptions  Options
	ClusterOptions ClusterOptions
}

GoRedisDriver implements the Sessions Database Driver for the go-redis redis driver. See driver.go file.

func GoRedis added in v12.2.0

func GoRedis() *GoRedisDriver

GoRedis returns the default Driver for the redis sessions database It's the go-redis client. Learn more at: https://github.com/go-redis/redis.

func (*GoRedisDriver) CloseConnection added in v12.2.0

func (r *GoRedisDriver) CloseConnection() error

CloseConnection terminates the underline redis connection.

func (*GoRedisDriver) Connect added in v12.2.0

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

Connect initializes the redis client.

func (*GoRedisDriver) Delete added in v12.2.0

func (r *GoRedisDriver) Delete(sid, key string) error

Delete removes a value from the redis store.

func (*GoRedisDriver) Exists added in v12.2.0

func (r *GoRedisDriver) Exists(sid string) bool

Exists reports whether a session exists or not.

func (*GoRedisDriver) Get added in v12.2.0

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

Get returns the associated value of the session's given "key".

func (*GoRedisDriver) GetAll added in v12.2.0

func (r *GoRedisDriver) GetAll(sid string) (map[string]string, error)

GetAll returns all the key values under the session.

func (*GoRedisDriver) GetKeys added in v12.2.0

func (r *GoRedisDriver) GetKeys(sid string) ([]string, error)

GetKeys returns all keys under the session.

func (*GoRedisDriver) Len added in v12.2.0

func (r *GoRedisDriver) Len(sid string) int

Len returns the total length of key-values of the session.

func (*GoRedisDriver) PingPong added in v12.2.0

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

PingPong sends a ping message and reports whether the PONG message received successfully.

func (*GoRedisDriver) Set added in v12.2.0

func (r *GoRedisDriver) Set(sid, key string, value interface{}) error

Set stores a "value" based on the session's "key". The value should be type of []byte, so unmarshal can happen.

func (*GoRedisDriver) SetClient added in v12.2.0

func (r *GoRedisDriver) SetClient(goRedisClient GoRedisClient) *GoRedisDriver

SetClient sets an existing go redis client to the sessions redis driver.

Returns itself.

func (*GoRedisDriver) TTL added in v12.2.0

func (r *GoRedisDriver) TTL(sid string) time.Duration

TTL returns any TTL value of the session.

func (*GoRedisDriver) UpdateTTL added in v12.2.0

func (r *GoRedisDriver) UpdateTTL(sid string, newLifetime time.Duration) error

UpdateTTL sets expiration duration of the session.

type Options added in v12.2.0

type Options = redis.Options

Options is just a type alias for the go-redis Client Options.

Jump to

Keyboard shortcuts

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