rueidishook

package
v0.0.0-...-c14f4f2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

rueidishook

With rueidishook.WithHook, users can easily intercept rueidis.Client by implementing custom rueidishook.Hook handler.

This can be useful to change the behavior of rueidis.Client or add other integrations such as observability, APM etc.

Example

package main

import (
	"context"
	"time"

	"github.com/rueian/rueidis"
	"github.com/rueian/rueidis/rueidishook"
)

type hook struct{}

func (h *hook) Do(client rueidis.Client, ctx context.Context, cmd rueidishook.Completed) (resp rueidis.RedisResult) {
	// do whatever you want before client.Do
	resp = client.Do(ctx, cmd)
	// do whatever you want after client.Do
	return
}

func (h *hook) DoMulti(client rueidis.Client, ctx context.Context, multi ...rueidishook.Completed) (resps []rueidis.RedisResult) {
	// do whatever you want before client.DoMulti
	resps = client.DoMulti(ctx, multi...)
	// do whatever you want after client.DoMulti
	return
}

func (h *hook) DoCache(client rueidis.Client, ctx context.Context, cmd rueidishook.Cacheable, ttl time.Duration) (resp rueidis.RedisResult) {
	// do whatever you want before client.DoCache
	resp = client.DoCache(ctx, cmd, ttl)
	// do whatever you want after client.DoCache
	return
}

func (h *hook) DoMultiCache(client rueidis.Client, ctx context.Context, multi ...rueidis.CacheableTTL) (resps []rueidis.RedisResult) {
	// do whatever you want before client.DoMultiCache
	resps = client.DoMultiCache(ctx, multi...)
	// do whatever you want after client.DoMultiCache
	return
}

func (h *hook) Receive(client rueidis.Client, ctx context.Context, subscribe rueidishook.Completed, fn func(msg rueidis.PubSubMessage)) (err error) {
	// do whatever you want before client.Receive
	err = client.Receive(ctx, subscribe, fn)
	// do whatever you want after client.Receive
	return
}

func main() {
	client, err := rueidis.NewClient(rueidis.ClientOption{InitAddress: []string{"127.0.0.1:6379"}})
	if err != nil {
		panic(err)
	}
	client = rueidishook.WithHook(client, &hook{})
	defer client.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithHook

func WithHook(client rueidis.Client, hook Hook) rueidis.Client

WithHook wraps rueidis.Client with Hook and allows user to intercept rueidis.Client

Types

type Cacheable

type Cacheable = cmds.Cacheable

Cacheable is alias to internal cmds.Cacheable

type Completed

type Completed = cmds.Completed

Completed is alias to internal cmds.Completed

type Hook

type Hook interface {
	Do(client rueidis.Client, ctx context.Context, cmd Completed) (resp rueidis.RedisResult)
	DoMulti(client rueidis.Client, ctx context.Context, multi ...Completed) (resps []rueidis.RedisResult)
	DoCache(client rueidis.Client, ctx context.Context, cmd Cacheable, ttl time.Duration) (resp rueidis.RedisResult)
	DoMultiCache(client rueidis.Client, ctx context.Context, multi ...rueidis.CacheableTTL) (resps []rueidis.RedisResult)
	Receive(client rueidis.Client, ctx context.Context, subscribe Completed, fn func(msg rueidis.PubSubMessage)) (err error)
}

Hook allows user to intercept rueidis.Client by using WithHook

Jump to

Keyboard shortcuts

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