redis

package
v1.101.15 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package redis provides a simple and basic wrapper client for interacting with Redis (https://redis.io), an in-memory data store.

Based on https://github.com/redis/go-redis, it abstracts away the complexities of the Redis protocol and provides a simplified interface.

This package includes functions for setting, getting, and deleting key/value entries. Additionally, it supports sending and receiving messages from channels.

It allows to specify custom message encoding and decoding functions, including serialization and encryption.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultMessageDecodeFunc

func DefaultMessageDecodeFunc(_ context.Context, msg string, data any) error

DefaultMessageDecodeFunc is the default function to decode a message for ReceiveData(). The value underlying data must be a pointer to the correct type for the next data item received.

func DefaultMessageEncodeFunc

func DefaultMessageEncodeFunc(_ context.Context, data any) (string, error)

DefaultMessageEncodeFunc is the default function to encode and serialize the input data for SendData().

func MessageDecode

func MessageDecode(msg string, data any) error

MessageDecode decodes a message encoded with MessageEncode to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

func MessageEncode

func MessageEncode(data any) (string, error)

MessageEncode encodes and serialize the input data to a string.

Types

type ChannelOption

type ChannelOption = libredis.ChannelOption

ChannelOption is an alias for the parent library ChannelOption.

type Client

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

Client is a wrapper for the Redis Client.

func New

func New(ctx context.Context, srvopt *SrvOptions, opts ...Option) (*Client, error)

New creates a new instance of the Redis client wrapper.

func (*Client) Close

func (c *Client) Close() error

Close closes the parent client, releasing any open resources.

func (*Client) Del

func (c *Client) Del(ctx context.Context, key string) error

Del deletes the specified key from the datastore.

func (*Client) Get

func (c *Client) Get(ctx context.Context, key string, value any) error

Get retrieves the raw value of the specified key and extract its content in the value parameter.

func (*Client) GetData

func (c *Client) GetData(ctx context.Context, key string, data any) error

GetData retrieves an encoded value of the specified key and extract its content in the data parameter.

func (*Client) HealthCheck

func (c *Client) HealthCheck(ctx context.Context) error

HealthCheck checks if the current data-store is alive.

func (*Client) Receive

func (c *Client) Receive(ctx context.Context) (string, string, error)

Receive receives a raw string message from a subscribed channel. Returns the channel name and the message value.

func (*Client) ReceiveData

func (c *Client) ReceiveData(ctx context.Context, data any) (string, error)

ReceiveData receives an encoded message from a subscribed channel, and extract its content in the data parameter. Returns the channel name in case of success.

func (*Client) Send

func (c *Client) Send(ctx context.Context, channel string, message any) error

Send publish a raw value to the specified channel.

func (*Client) SendData

func (c *Client) SendData(ctx context.Context, channel string, data any) error

SendData publish an encoded value to the specified channel.

func (*Client) Set

func (c *Client) Set(ctx context.Context, key string, value any, exp time.Duration) error

Set a raw value for the specified key with an expiration time. Zero expiration means the key has no expiration time.

func (*Client) SetData

func (c *Client) SetData(ctx context.Context, key string, data any, exp time.Duration) error

SetData sets an encoded value for the specified key with an expiration time. Zero expiration means the key has no expiration time.

type Option

type Option func(*cfg)

Option is a type to allow setting custom client options.

func WithMessageDecodeFunc

func WithMessageDecodeFunc(f TDecodeFunc) Option

WithMessageDecodeFunc allow to replace DefaultMessageDecodeFunc(). This function used by ReceiveData() to decode a message encoded with messageEncodeFunc to the provided data object. The value underlying data must be a pointer to the correct type for the next data item received.

func WithMessageEncodeFunc

func WithMessageEncodeFunc(f TEncodeFunc) Option

WithMessageEncodeFunc allow to replace DefaultMessageEncodeFunc. This function used by SendData() to encode and serialize the input data to a string.

func WithSubscrChannelOptions

func WithSubscrChannelOptions(opts ...ChannelOption) Option

WithSubscrChannelOptions sets options for the subscribed channels.

func WithSubscrChannels

func WithSubscrChannels(channels ...string) Option

WithSubscrChannels sets the channels to subscribe to and receive data from.

type RClient

type RClient interface {
	Close() error
	Del(ctx context.Context, keys ...string) *libredis.IntCmd
	Get(ctx context.Context, key string) *libredis.StringCmd
	Ping(ctx context.Context) *libredis.StatusCmd // this function is used by the HealthCheck
	Publish(ctx context.Context, channel string, message any) *libredis.IntCmd
	Set(ctx context.Context, key string, value any, expiration time.Duration) *libredis.StatusCmd
	Subscribe(ctx context.Context, channels ...string) *libredis.PubSub
}

RClient represents the mockable functions in the parent Redis Client.

type RMessage

type RMessage = libredis.Message

RMessage is an alias for the parent library Message type.

type RPubSub

type RPubSub interface {
	Channel(opts ...libredis.ChannelOption) <-chan *libredis.Message
	Close() error
}

RPubSub represents the mockable functions in the parent Redis PubSub.

type SrvOptions

type SrvOptions = libredis.Options

SrvOptions is an alias for the parent library client options.

type TDecodeFunc

type TDecodeFunc func(ctx context.Context, msg string, data any) error

TDecodeFunc is the type of function used to replace the default message decoding function used by ReceiveData().

type TEncodeFunc

type TEncodeFunc func(ctx context.Context, data any) (string, error)

TEncodeFunc is the type of function used to replace the default message encoding function used by SendData().

Jump to

Keyboard shortcuts

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