goredis

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2024 License: GPL-3.0 Imports: 10 Imported by: 4

README

GO Redis

Golang Redis Pool Implementation. This is a simple implementation of a Redis Pool in Golang. It is a simple wrapper around the github.com/gomodule/redigo/redis package.

Installation

To install the package, run the following command:

go get -u github.com/jbrewer/goredis

Basic Usage

Please find the basic usage example below:

package main

import (
	"github.com/gomodule/redigo/redis"
	goredis "github.com/jacobbrewer1/goredis/redis"
)

func main() {
	if err := goredis.NewPool(
		goredis.WithDefaultPool(),
		goredis.WithAddress("localhost:6379"),
		goredis.WithNetwork(goredis.NetworkTCP),
		goredis.WithDialOpts(
			redis.DialDatabase(0),
			redis.DialUsername("username"),
			redis.DialPassword("password"),
		),
	); err != nil {
		panic(err)
	}
}

Usage

Please find our usage examples in the examples directory.

If you are using a JSON configuration file, you can pass in a viper instance goredis.FromViper(viperInstance) connection option. This connection option will allow you to pass in a viper instance to read your configuration for the basic authentication and database to connect to, with the addition of the MaxIdle, MaxActive, IdleTimeout and Address.

Please find an example of a JSON configuration file below:

{
  "redis": {
    "address": "localhost:6379",
    "password": "password",
    "username": "username",
    "db": 0,
    "max_idle": 3,
    "max_active": 5,
    "idle_timeout_secs": 240
  }
}

You are able to pass additional options with the Connection option goredis.WithDialOpts. This option allows you to pass in a slice of redis.DialOption to the connection pool. This is useful for setting the read and write timeout.

Documentation

Index

Constants

View Source
const (
	NetworkTCP = "tcp"
)

Variables

View Source
var (

	// ErrRedisNotInitialised is returned when the redis connection pool is not initialised.
	ErrRedisNotInitialised = errors.New("redis connection pool not initialised")
)
View Source
var Latency = promauto.NewHistogramVec(
	prometheus.HistogramOpts{
		Name: "redis_latency",
		Help: "Duration of Redis queries",
	},
	[]string{"command"},
)

Latency is the duration of Redis queries.

Functions

func Conn

func Conn() redis.Conn

func Do

func Do(command string, args ...any) (reply any, err error)

func DoCtx

func DoCtx(ctx context.Context, command string, args ...any) (reply any, err error)

func NewPool

func NewPool(poolOpt PoolOption, connOpts ...ConnectionOption) error

NewPool returns a new Pool.

Types

type ConnectionOption

type ConnectionOption func(r *pool)

func FromViper

func FromViper(v *viper.Viper) []ConnectionOption

func WithAddress

func WithAddress(address string) ConnectionOption

func WithDialOpts

func WithDialOpts(dialOpts ...redis.DialOption) ConnectionOption

func WithIdleTimeout

func WithIdleTimeout(idleTimeout int) ConnectionOption

func WithMaxActive

func WithMaxActive(maxActive int) ConnectionOption

func WithMaxIdle

func WithMaxIdle(maxIdle int) ConnectionOption

func WithNetwork

func WithNetwork(network string) ConnectionOption

type MockConnectionOption

type MockConnectionOption struct {
	mock.Mock
}

MockConnectionOption is an autogenerated mock type for the ConnectionOption type

func NewMockConnectionOption

func NewMockConnectionOption(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockConnectionOption

NewMockConnectionOption creates a new instance of MockConnectionOption. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockConnectionOption) Execute

func (_m *MockConnectionOption) Execute(r *pool)

Execute provides a mock function with given fields: r

type MockPool

type MockPool struct {
	mock.Mock
}

MockPool is an autogenerated mock type for the Pool type

func NewMockPool

func NewMockPool(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockPool

NewMockPool creates a new instance of MockPool. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockPool) Conn

func (_m *MockPool) Conn() redis.Conn

Conn provides a mock function with given fields:

func (*MockPool) Do

func (_m *MockPool) Do(command string, args ...any) (any, error)

Do provides a mock function with given fields: command, args

func (*MockPool) DoCtx

func (_m *MockPool) DoCtx(ctx context.Context, command string, args ...any) (any, error)

DoCtx provides a mock function with given fields: ctx, command, args

type MockPoolOption

type MockPoolOption struct {
	mock.Mock
}

MockPoolOption is an autogenerated mock type for the PoolOption type

func NewMockPoolOption

func NewMockPoolOption(t interface {
	mock.TestingT
	Cleanup(func())
}) *MockPoolOption

NewMockPoolOption creates a new instance of MockPoolOption. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. The first argument is typically a *testing.T value.

func (*MockPoolOption) Execute

func (_m *MockPoolOption) Execute(p Pool)

Execute provides a mock function with given fields: p

type Pool

type Pool interface {
	// Do will send a command to the server and returns the received reply on a connection from the pool.
	Do(command string, args ...any) (reply any, err error)

	// DoCtx will send a command to the server with a context and returns the received reply on a connection from the pool.
	DoCtx(ctx context.Context, command string, args ...any) (reply any, err error)

	// Conn returns a redis connection from the pool.
	Conn() redis.Conn
}

type PoolOption

type PoolOption func(p Pool)

func WithDefaultPool

func WithDefaultPool() PoolOption

func WithInitializedPool

func WithInitializedPool(pool Pool) PoolOption

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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