rueidis

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: MIT Imports: 10 Imported by: 0

README

Rueidis integration

This integration provides:

  • A small Provider that opens a rueidis.Client from Melody config parameters.
  • A Redis-backed Melody cache backend implemented on top of Rueidis.

Provider

Entry point: NewProvider

The provider reads parameters (address, username, password) using the names you pass to the constructor. If you provide a comma-separated list of addresses, each item is used as an init address.

Optional configuration:

  • ClientConfig (client name, DB selection, TLS, disable client-side cache, ping on start)
  • TimeoutConfig (connect / command timeouts)

Cache backend

Package: cache

Backend

Entry point: cache.NewBackend

Backend holds a rueidis.Client and a context.Context passed at construction time. All Redis operations use this internal context. It implements cache/contract.Backend and supports:

  • single get/set/delete/has
  • many/get multiple
  • set/delete multiple
  • increment/decrement
  • clear / clear-by-prefix
BackendService

Entry point: cache.NewBackendService

BackendService is a singleton wrapper intended for service container registration. It creates an internal Backend with context.Background() and delegates all cache/contract.Backend methods to it.

Use WithContext to obtain a *Backend bound to a specific context:

package example

backend := backendService.WithContext(runtimeInstance.Context())
backend.Get("my-key")
backend.ClearByPrefix("accessToken:")

BackendFromRuntime

Helper: cache.BackendFromRuntime

Returns a *Backend with the runtime request context, following the same pattern as Melody's repository FromRuntime helpers:

package example

backend := rueidiscache.BackendFromRuntime(runtimeInstance, ServiceCacheRueidis)
backend.Get("my-key")

Usage example

Service registration

Register the Redis client, cache backend service, and Melody's generic cache backend in your application bootstrap:

package example

import (
	"github.com/precision-soft/melody/application"
	"github.com/precision-soft/melody/cache"
	cachecontract "github.com/precision-soft/melody/cache/contract"
	"github.com/precision-soft/melody/config"
	"github.com/precision-soft/melody/container"
	containercontract "github.com/precision-soft/melody/container/contract"
	rueidisintegration "github.com/precision-soft/melody/integrations/rueidis"
	rueidiscache "github.com/precision-soft/melody/integrations/rueidis/cache"
	"github.com/redis/rueidis"
)

const (
	ServiceRedisClient  = "service.redis.client"
	ServiceCacheRueidis = "service.cache.rueidis"
)

func RegisterCacheServices(app *application.Application) {
	app.RegisterService(
		ServiceRedisClient,
		func(resolver containercontract.Resolver) (rueidis.Client, error) {
			provider := rueidisintegration.NewProvider(
				"CACHE_REDIS_ADDRESS",
				"CACHE_REDIS_USER",
				"CACHE_REDIS_PASSWORD",
			)

			return provider.Open(resolver)
		},
	)

	app.RegisterService(
		ServiceCacheRueidis,
		func(resolver containercontract.Resolver) (*rueidiscache.BackendService, error) {
			configuration := config.ConfigMustFromResolver(resolver)
			client := container.MustFromResolver[rueidis.Client](resolver, ServiceRedisClient)
			prefix := configuration.MustGet("CACHE_REDIS_PREFIX").String()

			return rueidiscache.NewBackendService(client, prefix, 0, 0)
		},
	)

	app.RegisterService(
		cache.ServiceCacheBackend,
		func(resolver containercontract.Resolver) (cachecontract.Backend, error) {
			return container.MustFromResolver[*rueidiscache.BackendService](
				resolver, ServiceCacheRueidis,
			), nil
		},
	)
}
Request-scoped context

Create a thin helper that binds the service name, then use it in handlers:

package example

func BackendFromRuntime(runtimeInstance runtimecontract.Runtime) *rueidiscache.Backend {
	return rueidiscache.BackendFromRuntime(runtimeInstance, ServiceCacheRueidis)
}
package example

func (instance *MyController) Handle(
	runtimeInstance runtimecontract.Runtime,
) (httpcontract.Response, error) {
	backend := BackendFromRuntime(runtimeInstance)

	payload, found, err := backend.Get("my-key")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientConfig

type ClientConfig struct {
	ClientName       string
	SelectDb         int
	DisableCache     bool
	TlsConfig        *tls.Config
	PingOnStart      bool
	DialTimeout      time.Duration
	ConnWriteTimeout time.Duration
}

func DefaultClientConfig

func DefaultClientConfig() *ClientConfig

type ConnectionConfig

type ConnectionConfig struct {
	Address  string
	User     string
	Password string
}

func NewConnectionConfig

func NewConnectionConfig(
	address string,
	user string,
	password string,
) *ConnectionConfig

func (*ConnectionConfig) SafeContext

func (instance *ConnectionConfig) SafeContext() exceptioncontract.Context

type Provider

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

func NewProvider

func NewProvider(
	addressParameterName string,
	userParameterName string,
	passwordParameterName string,
) *Provider

func NewProviderWithConfig

func NewProviderWithConfig(
	addressParameterName string,
	userParameterName string,
	passwordParameterName string,
	clientConfig *ClientConfig,
	timeoutConfig *TimeoutConfig,
) *Provider

func (*Provider) Close

func (instance *Provider) Close(client rueidis.Client) error

func (*Provider) Open

func (instance *Provider) Open(resolver containercontract.Resolver) (rueidis.Client, error)

func (*Provider) Ping

func (instance *Provider) Ping(client rueidis.Client) error

func (*Provider) WithClientConfig

func (instance *Provider) WithClientConfig(clientConfig *ClientConfig) *Provider

func (*Provider) WithTimeoutConfig

func (instance *Provider) WithTimeoutConfig(timeoutConfig *TimeoutConfig) *Provider

type TimeoutConfig

type TimeoutConfig struct {
	ConnectTimeout time.Duration
	CommandTimeout time.Duration
}

func DefaultTimeoutConfig

func DefaultTimeoutConfig() *TimeoutConfig

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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