redis

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 9 Imported by: 0

README

Redis Source · Redis 脚本来源

Go Version

基于 Redis KV 存储的脚本来源,支持值比对热更新检测

中文 · English · 日本語


概述

Redis Source 使用 go-redis/v9 从 Redis 或兼容服务器(Valkey、DragonflyDB、KeyDB 等)读取脚本。通过值比对轮询检测热更新。

特性
特性 说明
底层库 github.com/redis/go-redis/v9
热更新 客户端值比对轮询
Key 前缀 支持 WithPrefix 命名空间隔离
认证 支持密码 / ACL 用户名+密码
兼容性 Redis、Valkey、DragonflyDB、KeyDB
接口 实现 source.ReadWatcher

安装

go get github.com/tx7do/go-scripts/source/redis

配置选项

选项 默认值 说明
WithAddr(addr) localhost:6379 Redis 服务器地址
WithPassword(pass) 认证密码
WithUsername(user) ACL 用户名(Redis 6.0+)
WithDB(db) 0 逻辑数据库索引
WithPrefix(prefix) Key 前缀

快速开始

package main

import (
    "context"
    "fmt"
    redisSrc "github.com/tx7do/go-scripts/source/redis"
)

func main() {
    ctx := context.Background()

    src, err := redisSrc.New(ctx,
        redisSrc.WithAddr("localhost:6379"),
        redisSrc.WithPrefix("scripts:lua:"),
    )
    if err != nil { panic(err) }
    defer src.Close()

    code, err := src.Load(ctx, "hello.lua")
    if err != nil { panic(err) }
    fmt.Println(code)
}

测试

cd source/redis && go test -v ./...

相关文档

License

MIT License

Documentation

Overview

Package redis provides a source.Reader implementation that reads scripts from a Redis key-value store (or any Redis-compatible server such as Valkey, DragonflyDB, KeyDB, ...).

Construction:

src, err := redis.New(ctx,
    redis.WithAddr("localhost:6379"),
    redis.WithPrefix("scripts:lua:"),
)

Hot-reload detection uses client-side polling: the value returned by the most recent Load is kept in memory, and Watch polls compare the current value against the stored one.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("redis source: key not found")

ErrNotFound is returned (wrapped) by Load when the requested key does not exist in Redis. Detect with errors.Is(err, ErrNotFound) or the convenience helper IsNotFound.

Functions

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err represents a "key not found" response from Redis. Equivalent to errors.Is(err, ErrNotFound).

Types

type Option

type Option func(*configOptions)

Option configures a Reader. Pass to New.

func WithAddr

func WithAddr(addr string) Option

WithAddr sets the Redis server address (default "localhost:6379").

func WithDB

func WithDB(db int) Option

WithDB selects the Redis logical database index (default 0).

func WithPassword

func WithPassword(password string) Option

WithPassword sets the Redis password for AUTH.

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix sets a key prefix that is transparently prepended to every key before it is resolved against Redis. Useful when all scripts share a common namespace (e.g. WithPrefix("scripts:lua:")).

Leading slashes are stripped; no other normalization is applied so callers can use any separator (":", "/", etc.).

func WithUsername

func WithUsername(username string) Option

WithUsername sets the Redis username (Redis ACL, since Redis 6.0).

type Reader

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

Reader reads scripts from Redis.

All exported methods are safe for concurrent use. Reader implements the source.ReadWatcher interface.

func New

func New(_ context.Context, opts ...Option) (*Reader, error)

New creates a Redis-backed Reader. All settings are optional; defaults are "localhost:6379", DB 0, no password.

Credentials can be supplied via WithPassword / WithUsername. For Redis ACL users, set both username and password.

func (*Reader) Close

func (r *Reader) Close() error

Close releases the underlying Redis client.

func (*Reader) Load

func (r *Reader) Load(ctx context.Context, key string) (string, error)

Load fetches the value from Redis and returns it as a string. Context cancellation propagates to the underlying request.

A redis.Nil response is reported as a wrapped ErrNotFound. Other errors are wrapped with the key for easier debugging.

func (*Reader) Watch

func (r *Reader) Watch(ctx context.Context, key string) (<-chan struct{}, error)

Watch returns a channel that signals when the value identified by `key` changes. It polls the value every 5 seconds and sends a signal on the channel when the value differs from the one recorded during the last Load.

The returned channel is closed when the context is cancelled. Callers should re-Load the script after receiving from the channel.

Jump to

Keyboard shortcuts

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