redissource

package
v3.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package redissource loads parser-kit data from a Redis key.

It lives in its own package so that importing the root package does not drag go-redis -- and with it cespare/xxhash and go.uber.org/atomic -- into binaries that never read from Redis. A service whose rules live on disk or behind HTTP pays nothing for Redis support existing; only importing this package links it in.

loader, err := parserkit.NewLoader[Rule](nil)
rules, err := loader.Load(ctx,
	parserkit.At(0, redissource.New(rdb, "app:rules")),
	parserkit.At(1, parserkit.File("/etc/app/rules.json")),
)

The source is a translation layer and nothing more: decoding, priority, merge semantics and the byte budget all live in the root package.

Example

Redis is the primary source, the file on disk the fallback.

package main

import (
	"context"
	"fmt"

	"github.com/alicebob/miniredis/v2"
	"github.com/redis/go-redis/v9"

	parserkit "github.com/soulteary/parser-kit/v3"
	"github.com/soulteary/parser-kit/v3/redissource"
)

type Rule struct {
	ID     string `json:"id"`
	Action string `json:"action"`
}

func main() {
	mr, err := miniredis.Run() // stand-in for a real server
	if err != nil {
		panic(err)
	}
	defer mr.Close()
	_ = mr.Set("app:rules", `[{"id":"r1","action":"allow"}]`)

	rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
	defer func() { _ = rdb.Close() }()

	loader, err := parserkit.NewLoader[Rule](nil)
	if err != nil {
		panic(err)
	}

	rules, err := loader.Load(context.Background(),
		parserkit.At(0, redissource.New(rdb, "app:rules")),
		parserkit.At(1, parserkit.File("/etc/app/rules.json").AllowMissing()),
	)
	if err != nil {
		panic(err)
	}

	fmt.Println(rules[0].ID, rules[0].Action)
}
Output:
r1 allow

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrClientNil = errors.New("redissource: redis client is nil")

ErrClientNil reports that there is no Redis client to read from.

Functions

This section is empty.

Types

type Fetcher

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

Fetcher reads a JSON payload from one Redis key. It implements parserkit.Fetcher.

func New

func New(client Getter, key string) *Fetcher

New returns a Fetcher reading key from client.

func (*Fetcher) AllowMissing

func (f *Fetcher) AllowMissing() *Fetcher

AllowMissing makes a key that does not exist report an absent source rather than an error, so the loader moves on to the next source without the missing key showing up as the reason everything failed.

A key that exists but holds an empty string is still empty data, not an absent source, and a connection failure is still an error.

func (*Fetcher) Fetch

func (f *Fetcher) Fetch(ctx context.Context, maxBytes int64) ([]byte, error)

Fetch reads the key, subject to maxBytes.

The size is checked with STRLEN before the value is read, so an oversized value is refused rather than pulled into memory first.

func (*Fetcher) Key

func (f *Fetcher) Key() string

Key returns the Redis key this fetcher reads.

type Getter

type Getter interface {
	Get(ctx context.Context, key string) *redis.StringCmd
	StrLen(ctx context.Context, key string) *redis.IntCmd
}

Getter is the part of a go-redis client this source uses. *redis.Client, *redis.ClusterClient, *redis.Ring and redis.UniversalClient all satisfy it, so a source written against it works the same on a standalone server, a cluster and a Sentinel failover setup.

This replaces v1's `RedisClient interface{}`, which accepted anything, gave up at run time on a *redis.ClusterClient, and told the compiler nothing.

Example

Getter is the part of a go-redis client the source uses -- two commands -- so one source works on a standalone server, a Ring and a Sentinel or cluster setup behind redis.UniversalClient, with nothing to switch on.

package main

import (
	"context"
	"fmt"

	"github.com/alicebob/miniredis/v2"
	"github.com/redis/go-redis/v9"

	parserkit "github.com/soulteary/parser-kit/v3"
	"github.com/soulteary/parser-kit/v3/redissource"
)

func main() {
	mr, err := miniredis.Run()
	if err != nil {
		panic(err)
	}
	defer mr.Close()
	_ = mr.Set("app:rules", `[{"id":"r1","action":"allow"}]`)

	clients := []struct {
		shape  string
		client redissource.Getter
	}{
		{"Client", redis.NewClient(&redis.Options{Addr: mr.Addr()})},
		{"Ring", redis.NewRing(&redis.RingOptions{Addrs: map[string]string{"one": mr.Addr()}})},
		{"UniversalClient", redis.NewUniversalClient(&redis.UniversalOptions{Addrs: []string{mr.Addr()}})},
	}

	for _, c := range clients {
		raw, err := redissource.New(c.client, "app:rules").Fetch(context.Background(), parserkit.DefaultMaxBytes)
		if err != nil {
			panic(err)
		}
		fmt.Printf("%s: %s\n", c.shape, raw)
	}
}
Output:
Client: [{"id":"r1","action":"allow"}]
Ring: [{"id":"r1","action":"allow"}]
UniversalClient: [{"id":"r1","action":"allow"}]

Jump to

Keyboard shortcuts

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