redis

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: Apache-2.0 Imports: 6 Imported by: 5

Documentation

Overview

Package redis provides a Hord database driver for Redis.

Redis is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. To use this driver, import it as follows:

import (
    "github.com/madflojo/hord"
    "github.com/madflojo/hord/redis"
)

Connecting to the Database

Use the Dial() function to create a new client for interacting with Redis.

var db hord.Database
db, err := redis.Dial(redis.Config{})
if err != nil {
    // Handle connection error
}

Initialize database

Hord provides a Setup() function for preparing a database. This function is safe to execute after every Dial().

err := db.Setup()
if err != nil {
    // Handle setup error
}

Database Operations

Hord provides a simple abstraction for working with Redis, with easy-to-use methods such as Get() and Set() to read and write values.

// Connect to the Redis database
db, err := redis.Dial(redis.Config{})
if err != nil {
    // Handle connection error
}

err := db.Setup()
if err != nil {
    // Handle setup error
}

// Set a value
err = db.Set("key", []byte("value"))
if err != nil {
    // Handle error
}

// Retrieve a value
value, err := db.Get("key")
if err != nil {
    // Handle error
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// ConnectTimeout is used to specify a global connection timeout value.
	ConnectTimeout time.Duration

	// Database specifies the Redis database to connect to and use. If not set, default is 0.
	Database int

	// IdleTimeout will close idle connections that have remained idle beyond the specified time duration.
	IdleTimeout time.Duration

	// KeepAlive defines the TCP Keep-Alive interval for Redis connections. By default this set to 5 minutes, this
	// setting is useful for detecting TCP sessions that are stale.
	KeepAlive time.Duration

	// MaxActive is the maximum number of connections that can be allocated and used for the Redis connection pool.
	MaxActive int

	// MaxConnLifetime will set a maximum lifespan for connections. This setting will close connections in the pool
	// after the time duration is exceeded, regardless of whether the connection is active or not.
	MaxConnLifetime time.Duration

	// MaxIdle sets the maximum number of idle connections the Redis connection pool will allow.
	MaxIdle int

	// Password specifies the AUTH token to be used for Redis Authentication.
	Password string

	// ReadTimeout is used to specify a global read timeout for each Redis command.
	ReadTimeout time.Duration

	// SentinelConfig is used to configure Sentinel connection details. If not using Redis Sentinel or Discovery
	// Service, leave this blank.
	SentinelConfig SentinelConfig

	// Server specifies the Redis Server to connect to. If using Redis Sentinel or Discovery Service leave this
	// blank.
	Server string

	// SkipTLSVerify will disable the TLS hostname checking. Warning, using this setting opens the risk of
	// man-in-the-middle attacks.
	SkipTLSVerify bool

	// TLSConfig allows users to specify TLS settings for connecting to Redis. This is a standard TLS configuration
	// and can be used to configure 2-way TLS for Redis and Redis Sentinel.
	TLSConfig *tls.Config

	// WriteTimeout is used to specify a global write timeout for each Redis command.
	WriteTimeout time.Duration
}

Config provides configuration options for connecting to and controlling the behavior of Redis.

type Database

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

Database is used to interface with Redis. It also satisfies the Hord Database interface.

func Dial

func Dial(conf Config) (*Database, error)

Dial will establish a Redis connection pool using the configuration provided. It provides back an interface that satisfies the hord.Database interface.

func (*Database) Close

func (db *Database) Close()

Close will close all connections to Redis and clean up the pool.

func (*Database) Delete

func (db *Database) Delete(key string) error

Delete is called when data within the database needs to be deleted. This function will delete the data stored within the database for the specified key.

func (*Database) Get

func (db *Database) Get(key string) ([]byte, error)

Get is called to retrieve data from the database. This function will take in a key and return the data or any errors received from querying the database.

func (*Database) HealthCheck

func (db *Database) HealthCheck() error

HealthCheck is used to verify connectivity and health of the database. This function simply runs a generic ping against the database. If the ping errors in any fashion this function will return an error.

func (*Database) Keys

func (db *Database) Keys() ([]string, error)

Keys is called to retrieve a list of keys stored within the database. This function will query the database returning all keys used within the hord database.

func (*Database) Set

func (db *Database) Set(key string, data []byte) error

Set is called when data within the database needs to be updated or inserted. This function will take the data provided and create an entry within the database using the key as a lookup value.

func (*Database) Setup

func (db *Database) Setup() error

Setup does nothing with Redis, this is only here to meet interface requirements.

type SentinelConfig

type SentinelConfig struct {
	// Servers is a list of Sentinel servers to connect to and use for master discovery.
	Servers []string

	// Master is the name of the Redis master that the Sentinel Servers monitor.
	Master string
}

SentinelConfig can be used to configure the Redis client to connect using Redis Sentinel or Enterprise Redis Discovery Service.

Jump to

Keyboard shortcuts

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