gousuredis

package module
v2.0.0-...-b942c1c Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 12 Imported by: 0

README

Redis-Integration for Go Universal Service Utilities

Full docu for go-gousu on https://github.com/indece-official/go-gousu

Usage

TODO

Documentation

Index

Constants

View Source
const ServiceName = "redis"

ServiceName defines the name of redis service used for dependency injection

Variables

View Source
var ErrNil = redis.ErrNil

ErrNil is the error returned if no matching data was found

Functions

func NewService

func NewService(ctx gousu.IContext) gousu.IService

NewService is the ServiceFactory for redis service

Types

type IService

type IService interface {
	gousu.IService

	NewMutex(name string, options ...redsync.Option) *redsync.Mutex
	GetPool() *redis.Pool
	Get(key string) ([]byte, error)
	Set(key string, data []byte) error
	SetNXPX(key string, data []byte, timeoutMS int) error
	SetPX(key string, data []byte, timeoutMS int) error
	Del(key string) error
	Exists(key string) (bool, error)
	Scan(pattern string, cursor int) (int, []string, error)
	ScanAll(pattern string, limit null.Int) ([]string, error)
	RPush(key string, data []byte) (int, error)
	LPush(key string, data []byte) (int, error)
	LRange(key string, start int, stop int) ([][]byte, error)
	LRem(key string, count int, data []byte) (int, error)
	LPop(key string) ([]byte, error)
	RPop(key string) ([]byte, error)
	BLPop(key string, timeout int) ([]byte, error)
	HGet(key string, field string) ([]byte, error)
	HSet(key string, field string, data []byte) error
	HScan(key string, cursor int) (int, map[string][]byte, error)
	HKeys(key string) ([][]byte, error)
	HDel(key string, field string) error
	HLen(key string) (int, error)
	LIndex(key string, position int) ([]byte, error)
	LLen(key string) (int, error)
	Subscribe(channels []string) (chan Message, ISubscription, error)
	Publish(channel string, data []byte) error
	XAdd(key string, data map[string]string) (string, error)
	XGroupCreate(groupName string, key string, offset XGroupCreateOffset, mkStream bool, ignoreBusy bool) error
	XReadGroup(groupName string, consumerName string, key string, timeout time.Duration, streamID XReadGroupStreamID) (*XEvent, error)
	XAck(groupName string, key string, id string) (int, error)
}

IService defines the interface of the redis service

type ISubscription

type ISubscription interface {
	Subscribe(channel ...interface{}) error
	Unsubscribe(channel ...interface{}) error
	Close() error
}

ISubscription defines the interface of Subscription

type Message

type Message struct {
	Error   error
	Channel string
	Data    []byte
}

Message is emitted after subscribing to a channel

func (*Message) IsError

func (m *Message) IsError() bool

IsError returns if an error occured

type MockService

type MockService struct {
	gousu.MockService

	NewMutexFunc           func(name string, options ...redsync.Option) *redsync.Mutex
	GetPoolFunc            func() *redis.Pool
	GetFunc                func(key string) ([]byte, error)
	SetFunc                func(key string, data []byte) error
	SetNXPXFunc            func(key string, data []byte, timeoutMS int) error
	SetPXFunc              func(key string, data []byte, timeoutMS int) error
	DelFunc                func(key string) error
	ExistsFunc             func(key string) (bool, error)
	ScanFunc               func(pattern string, cursor int) (int, []string, error)
	ScanAllFunc            func(pattern string, limit null.Int) ([]string, error)
	RPushFunc              func(key string, data []byte) (int, error)
	LPushFunc              func(key string, data []byte) (int, error)
	LRangeFunc             func(key string, start int, stop int) ([][]byte, error)
	LRemFunc               func(key string, count int, data []byte) (int, error)
	LPopFunc               func(key string) ([]byte, error)
	RPopFunc               func(key string) ([]byte, error)
	BLPopFunc              func(key string, timeout int) ([]byte, error)
	HGetFunc               func(key string, field string) ([]byte, error)
	HSetFunc               func(key string, field string, data []byte) error
	HScanFunc              func(key string, cursor int) (int, map[string][]byte, error)
	HKeysFunc              func(key string) ([][]byte, error)
	HDelFunc               func(key string, field string) error
	HLenFunc               func(key string) (int, error)
	LIndexFunc             func(key string, position int) ([]byte, error)
	LLenFunc               func(key string) (int, error)
	SubscribeFunc          func(channels []string) (chan Message, ISubscription, error)
	PublishFunc            func(channel string, data []byte) error
	XAddFunc               func(key string, data map[string]string) (string, error)
	XGroupCreateFunc       func(groupName string, key string, offset XGroupCreateOffset, mkStream bool, ignoreBusy bool) error
	XReadGroupFunc         func(groupName string, consumerName string, key string, timeout time.Duration, streamID XReadGroupStreamID) (*XEvent, error)
	XAckFunc               func(groupName string, key string, id string) (int, error)
	NewMutexFuncCalled     int
	GetPoolFuncCalled      int
	GetFuncCalled          int
	SetFuncCalled          int
	SetNXPXFuncCalled      int
	SetPXFuncCalled        int
	DelFuncCalled          int
	ExistsFuncCalled       int
	ScanFuncCalled         int
	ScanAllFuncCalled      int
	RPushFuncCalled        int
	LPushFuncCalled        int
	LRangeFuncCalled       int
	LRemFuncCalled         int
	LPopFuncCalled         int
	RPopFuncCalled         int
	BLPopFuncCalled        int
	HGetFuncCalled         int
	HSetFuncCalled         int
	HScanFuncCalled        int
	HKeysFuncCalled        int
	HDelFuncCalled         int
	HLenFuncCalled         int
	LIndexFuncCalled       int
	LLenFuncCalled         int
	SubscribeFuncCalled    int
	PublishFuncCalled      int
	XAddFuncCalled         int
	XGroupCreateFuncCalled int
	XReadGroupFuncCalled   int
	XAckFuncCalled         int
}

MockService for simply mocking IService

func NewMockService

func NewMockService() *MockService

NewMockService creates a new initialized instance of MockService

func (*MockService) BLPop

func (s *MockService) BLPop(key string, timeout int) ([]byte, error)

BLPop calls BLPopFunc and increases BLPopFuncCalled

func (*MockService) Del

func (s *MockService) Del(key string) error

Del calls DelFunc and increases DelFuncCalled

func (*MockService) Exists

func (s *MockService) Exists(key string) (bool, error)

Exists calls ExistsFunc and increases ExistsFuncCalled

func (*MockService) Get

func (s *MockService) Get(key string) ([]byte, error)

Get calls GetFunc and increases GetFuncCalled

func (*MockService) GetPool

func (s *MockService) GetPool() *redis.Pool

GetPool calls GetPoolFunc and increases GetPoolFuncCalled

func (*MockService) HDel

func (s *MockService) HDel(key string, field string) error

HDel calls HDelFunc and increases HDelFuncCalled

func (*MockService) HGet

func (s *MockService) HGet(key string, field string) ([]byte, error)

HGet calls GetFunc and increases GetFuncCalled

func (*MockService) HKeys

func (s *MockService) HKeys(key string) ([][]byte, error)

HKeys calls HKeysFunc and increases HKeysFuncCalled

func (*MockService) HLen

func (s *MockService) HLen(key string) (int, error)

HLen calls HLenFunc and increases HLenFuncCalled

func (*MockService) HScan

func (s *MockService) HScan(key string, cursor int) (int, map[string][]byte, error)

HScan calls HScanFunc and increases HScanFuncCalled

func (*MockService) HSet

func (s *MockService) HSet(key string, field string, data []byte) error

HSet calls SetFunc and increases SetFuncCalled

func (*MockService) LIndex

func (s *MockService) LIndex(key string, position int) ([]byte, error)

LIndex calls LIndexFunc and increases LIndexFuncCalled

func (*MockService) LLen

func (s *MockService) LLen(key string) (int, error)

LLen calls LLenFunc and increases LLenFuncCalled

func (*MockService) LPop

func (s *MockService) LPop(key string) ([]byte, error)

LPop calls LPopFunc and increases LPopFuncCalled

func (*MockService) LPush

func (s *MockService) LPush(key string, data []byte) (int, error)

LPush calls LPushFunc and increases LPushFuncCalled

func (*MockService) LRange

func (s *MockService) LRange(key string, start int, stop int) ([][]byte, error)

LRange calls LRangeFunc and increases LRangeFuncCalled

func (*MockService) LRem

func (s *MockService) LRem(key string, count int, data []byte) (int, error)

LRem calls LRemFunc and increases LRemFuncCalled

func (*MockService) NewMutex

func (s *MockService) NewMutex(name string, options ...redsync.Option) *redsync.Mutex

NewMutex calls NewMutexFunc and increases NewMutexFuncCalled

func (*MockService) Publish

func (s *MockService) Publish(channel string, data []byte) error

Publish calls PublishFunc and increases PublishFuncCalled

func (*MockService) RPop

func (s *MockService) RPop(key string) ([]byte, error)

RPop calls RPopFunc and increases RPopFuncCalled

func (*MockService) RPush

func (s *MockService) RPush(key string, data []byte) (int, error)

RPush calls RPushFunc and increases RPushFuncCalled

func (*MockService) Scan

func (s *MockService) Scan(pattern string, cursor int) (int, []string, error)

Scan calls ScanFunc and increases ScanFuncCalled

func (*MockService) ScanAll

func (s *MockService) ScanAll(pattern string, limit null.Int) ([]string, error)

ScanAll calls ScanAllFunc and increases ScanAllFuncCalled

func (*MockService) Set

func (s *MockService) Set(key string, data []byte) error

Set calls SetFunc and increases SetFuncCalled

func (*MockService) SetNXPX

func (s *MockService) SetNXPX(key string, data []byte, timeoutMS int) error

SetNXPX calls SetNXPXFunc and increases SetNXPXFuncCalled

func (*MockService) SetPX

func (s *MockService) SetPX(key string, data []byte, timeoutMS int) error

SetPX calls SetPXFunc and increases SetPXFuncCalled

func (*MockService) Subscribe

func (s *MockService) Subscribe(channels []string) (chan Message, ISubscription, error)

Subscribe calls SubscribeFunc and increases SubscribeFunc

func (*MockService) XAck

func (s *MockService) XAck(groupName string, key string, id string) (int, error)

XAck calls XAckFunc and increases XAckFuncCalled

func (*MockService) XAdd

func (s *MockService) XAdd(key string, data map[string]string) (string, error)

XAdd calls XAddFunc and increases XAddFuncCalled

func (*MockService) XGroupCreate

func (s *MockService) XGroupCreate(groupName string, key string, offset XGroupCreateOffset, mkStream bool, ignoreBusy bool) error

XGroupCreate calls XGroupCreateFunc and increases XGroupCreateFuncCalled

func (*MockService) XReadGroup

func (s *MockService) XReadGroup(groupName string, consumerName string, key string, timeout time.Duration, streamID XReadGroupStreamID) (*XEvent, error)

XReadGroup calls XReadGroupFunc and increases XReadGroupFuncCalled

type Service

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

Service provides a service for basic redis client functionality

Used flags:

  • redis_host Hostname of redis service
  • redis_port Port of redis service

func (*Service) BLPop

func (s *Service) BLPop(key string, timeout int) ([]byte, error)

BLPop waits for a new item in a list (blocking with timeout)

func (*Service) Del

func (s *Service) Del(key string) error

Del deletes a key from redis

func (*Service) Exists

func (s *Service) Exists(key string) (bool, error)

Exists checks if a key exists in redis

func (*Service) Get

func (s *Service) Get(key string) ([]byte, error)

Get retrieves a key's value from redis

func (*Service) GetPool

func (s *Service) GetPool() *redis.Pool

GetPool returns the redis connection pool if not in cluster mode, else nil

func (*Service) HDel

func (s *Service) HDel(key string, field string) error

HDel deletes a field from a hash

func (*Service) HGet

func (s *Service) HGet(key string, field string) ([]byte, error)

HGet retrieves a hash value from redis

func (*Service) HKeys

func (s *Service) HKeys(key string) ([][]byte, error)

HKeys gets all field names in the hash stored at key

func (*Service) HLen

func (s *Service) HLen(key string) (int, error)

HLen gets the length of the map stored at key

func (*Service) HScan

func (s *Service) HScan(key string, cursor int) (int, map[string][]byte, error)

HScan scans a hash map and returns a list of field-value-tupples

func (*Service) HSet

func (s *Service) HSet(key string, field string, data []byte) error

HSet stores a key and its value in a hash in redis

func (*Service) Health

func (s *Service) Health() error

Health checks the health of the Service by pinging the redis database

func (*Service) LIndex

func (s *Service) LIndex(key string, position int) ([]byte, error)

LIndex gets the element at index in the list stored at key

func (*Service) LLen

func (s *Service) LLen(key string) (int, error)

LLen gets the length of the list stored at key

func (*Service) LPop

func (s *Service) LPop(key string) ([]byte, error)

LPop returns the newest item from a list (non-blocking)

func (*Service) LPush

func (s *Service) LPush(key string, data []byte) (int, error)

LPush prepends an item to a list

func (*Service) LRange

func (s *Service) LRange(key string, start int, stop int) ([][]byte, error)

LRange loads elements from a list

func (*Service) LRem

func (s *Service) LRem(key string, count int, data []byte) (int, error)

LRem removes n matching items from a list

func (*Service) Name

func (s *Service) Name() string

Name returns the name of redis service from ServiceName

func (*Service) NewMutex

func (s *Service) NewMutex(name string, options ...redsync.Option) *redsync.Mutex

NewMutex creates a new redsync mutex

func (*Service) Publish

func (s *Service) Publish(channel string, data []byte) error

Publish emits a message on a channel

func (*Service) RPop

func (s *Service) RPop(key string) ([]byte, error)

RPop returns the oldest item from a list (non-blocking)

func (*Service) RPush

func (s *Service) RPush(key string, data []byte) (int, error)

RPush appends an item to a list

func (*Service) Scan

func (s *Service) Scan(pattern string, cursor int) (int, []string, error)

Scan scans keys for a specific pattern and returns a list of keys Important: Running Scan(...) in a cluster can return only partial results

func (*Service) ScanAll

func (s *Service) ScanAll(pattern string, limit null.Int) ([]string, error)

ScanAll scans all keys for a specific pattern and returns a list of keys Supports being run in a cluster Important: The resulting list is unordered

func (*Service) Set

func (s *Service) Set(key string, data []byte) error

Set stores a key and its value in redis

func (*Service) SetNXPX

func (s *Service) SetNXPX(key string, data []byte, timeoutMS int) error

SetNXPX stores a key and its value if it does not exist with expiration time in redis

func (*Service) SetPX

func (s *Service) SetPX(key string, data []byte, timeoutMS int) error

SetPX stores a key and its value with expiration time in redis

func (*Service) Start

func (s *Service) Start() error

Start connects to the redis pool

func (*Service) Stop

func (s *Service) Stop() error

Stop closes all redis pool connections

func (*Service) Subscribe

func (s *Service) Subscribe(channels []string) (chan Message, ISubscription, error)

Subscribe subscribes to channels and returns a subscription

func (*Service) XAck

func (s *Service) XAck(groupName string, key string, id string) (int, error)

XAck acknowledges stream event

func (*Service) XAdd

func (s *Service) XAdd(key string, data map[string]string) (string, error)

XAdd adds an stream event

func (*Service) XGroupCreate

func (s *Service) XGroupCreate(groupName string, key string, offset XGroupCreateOffset, mkStream bool, ignoreBusy bool) error

XGroupCreate adds an stream event

func (*Service) XReadGroup

func (s *Service) XReadGroup(groupName string, consumerName string, key string, timeout time.Duration, streamID XReadGroupStreamID) (*XEvent, error)

XReadGroup waits for one new item in a stream (blocking with timeout)

type Subscription

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

Subscription is used to track a subscription to a channel via Subscribe(...)

func (*Subscription) Close

func (s *Subscription) Close() error

Close unsubscribes from all subscriptions and closes the connection

func (*Subscription) Subscribe

func (s *Subscription) Subscribe(channel ...interface{}) error

Subscribe subscribes to one or multiple channels

func (*Subscription) Unsubscribe

func (s *Subscription) Unsubscribe(channel ...interface{}) error

Unsubscribe unsubscribes from one or multiple channels

type XEvent

type XEvent struct {
	Key  string
	ID   string
	Data map[string]string
}

type XGroupCreateOffset

type XGroupCreateOffset = string
const (
	XGroupCreateOffsetLast  XGroupCreateOffset = "$"
	XGroupCreateOffsetFirst XGroupCreateOffset = "0"
)

type XReadGroupStreamID

type XReadGroupStreamID = string
const (
	XReadGroupIDStreamNew     XReadGroupStreamID = ">"
	XReadGroupIDStreamPending XReadGroupStreamID = "0"
)

Jump to

Keyboard shortcuts

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