sabi_redis

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2026 License: MIT Imports: 5 Imported by: 0

README

sabi_redis Go Reference CI Status MIT License

sabi_redis is a library that provides data access for Redis within the sabi framework. It allows for a streamlined and consistent way to access various Redis configurations while adhering to the architectural patterns of the sabi framework.

Key Features

  • Multi-Configuration Support: Specialized implementations for Standalone Redis, Redis Sentinel, and Redis Cluster.
  • Consistency Management: Since Redis lacks native rollbacks, this library provides mechanisms to manage data integrity:
    • Pre-commit: Logic executed before the main commit phase.
    • Post-commit: Logic executed after a successful commit.
    • Force-back: A mechanism to register "revert" logic that executes if a transaction fails.

Installation

go get github.com/sttk/sabi_redis

Usage

1. Define a Data Access

Implement a data access struct that retrieves the Redis connection from the sabi context.

type RedisSampleDataAcc struct {
    sabi.DataAcc
}

func (da *RedisSampleDataAcc) SetValue(key, val string) errs.Err {
    ctx := da.Context()
    dc, err := sabi.GetDataConn[*sabi_redis.RedisDataConn](da, "redis")
    if err.IsNotOk() {
        return err
    }
    
    redisConn := dc.GetConnection()
    e := redisConn.Set(ctx, key, val, 0).Err()
    if e != nil {
        return errs.New(FailToSetValue{}, e)
    }

    // Register a force-back logic to delete the key if the transaction fails
    dc.AddForceBack(func(conn *redis.Conn) errs.Err {
        return errs.NewFromErr(conn.Del(ctx, key).Err())
    })

    return errs.Ok()
}
2. Register and Setup the Data Source

Register the appropriate Redis data source.

// Standalone
ds := sabi_redis.NewRedisDataSrc(&redis.Options{Addr: "localhost:6379"})

// Or Sentinel
// ds := sabi_redis.NewRedisSentinelDataSrc(&redis.FailoverOptions{...})

// Or Cluster
// ds := sabi_redis.NewRedisClusterDataSrc(&redis.ClusterOptions{...})
3. Define logic and its data interface

Define the business logic and the interface it requires.

type SampleData interface {
    SetValue(key, val string) errs.Err
}

func sampleLogic(data SampleData) errs.Err {
    return data.SetValue("mykey", "myvalue")
}
4. Integrate DataAcc into DataHub

Create a DataHub that integrates the DataAcc.

type SampleDataHub struct {
    sabi.DataHub
    *RedisSampleDataAcc
}

func NewSampleDataHub() sabi.DataHub {
    hub := sabi.NewDataHub()
    return SampleDataHub{
        DataHub:            hub,
        RedisSampleDataAcc: &RedisSampleDataAcc{DataAcc: hub},
    }
}

var _ SampleData = (*SampleDataHub)(nil) // Check if SampleDataHub implements SampleData at compile time
5. Run within a transaction

Execute your business logic using the DataHub.

ctx := context.Background()
hub := NewSampleDataHub()
hub.Uses("redis", ds)

err := sabi.Run(hub, ctx, sampleLogic)

Supporting Go versions

This library supports Go 1.23 or later.

Actual test results for each Go version:
% go-fav 1.23.12 1.24.13 1.25.8 1.26.1
go version go1.23.12 darwin/amd64
ok  	github.com/sttk/sabi_redis	9.414s	coverage: 77.9% of statements

go version go1.24.13 darwin/amd64
ok  	github.com/sttk/sabi_redis	9.253s	coverage: 77.9% of statements

go version go1.25.8 darwin/amd64
ok  	github.com/sttk/sabi_redis	9.356s	coverage: 77.9% of statements

go version go1.26.1 darwin/amd64
ok  	github.com/sttk/sabi_redis	9.295s	coverage: 77.9% of statements

License

Copyright (C) 2026 Takayuki Sato

This program is free software under MIT License.
See the file LICENSE in this distribution for more details.

Documentation

Overview

Package sabi_redis is a library that provides data access for Redis within the sabi framework.

This library allows for a streamlined and consistent way to access various Redis configurations while adhering to the architectural patterns of the sabi framework.

Key Features

  • Multi-Configuration Support: Provides specialized implementations for Standalone Redis servers, Redis Sentinel for high availability, and Redis Cluster for horizontal scaling.
  • Consistency Management: Since Redis lacks native rollbacks, this library provides mechanisms to manage data integrity during failures:
  • Pre-commit: Logic executed before the main commit phase.
  • Post-commit: Logic executed after a successful commit.
  • Force-back: A mechanism to register "revert" logic that executes if a transaction fails.

Integration with the sabi framework

This library integrates with sabi by implementing its core data access abstractions:

  • DataSrc & DataConn: It provides RedisDataSrc (and variants like RedisSentinelDataSrc and RedisClusterDataSrc) to handle connection pooling and configuration, and RedisDataConn (and its variants) to provide the actual connection to business logic.
  • Registration & Setup: Data sources are registered using the framework's "Uses" function and initialized during the setup phase.
  • Transaction Management: Redis operations are typically performed within a sabi transaction (Dab.Txn).
  • Error Handling: It utilizes the framework's transaction lifecycle to trigger "ForceBack" operations, allowing developers to attempt to undo Redis changes if a transaction fails.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisClusterDataConn

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

RedisClusterDataConn is a data connection for Redis Cluster.

func (*RedisClusterDataConn) AddForceBack

func (dc *RedisClusterDataConn) AddForceBack(fn func(*redis.ClusterClient) errs.Err)

AddForceBack adds a function to be executed on force back.

func (*RedisClusterDataConn) AddPostCommit

func (dc *RedisClusterDataConn) AddPostCommit(fn func(*redis.ClusterClient) errs.Err)

AddPostCommit adds a function to be executed after commit.

func (*RedisClusterDataConn) AddPreCommit

func (dc *RedisClusterDataConn) AddPreCommit(fn func(*redis.ClusterClient) errs.Err)

AddPreCommit adds a function to be executed before commit.

func (*RedisClusterDataConn) Close

func (dc *RedisClusterDataConn) Close()

Close does nothing for this data connection.

func (*RedisClusterDataConn) Commit

func (dc *RedisClusterDataConn) Commit(ag *sabi.AsyncGroup) errs.Err

Commit does nothing for this data connection.

func (*RedisClusterDataConn) ForceBack

func (dc *RedisClusterDataConn) ForceBack(ag *sabi.AsyncGroup)

ForceBack executes force-back functions in reverse order.

func (*RedisClusterDataConn) GetConnection

func (dc *RedisClusterDataConn) GetConnection() *redis.ClusterClient

GetConnection returns the underlying redis cluster client.

func (*RedisClusterDataConn) PostCommit

func (dc *RedisClusterDataConn) PostCommit(ag *sabi.AsyncGroup)

PostCommit executes post-commit functions.

func (*RedisClusterDataConn) PreCommit

func (dc *RedisClusterDataConn) PreCommit(ag *sabi.AsyncGroup) errs.Err

PreCommit executes pre-commit functions.

func (*RedisClusterDataConn) Rollback

func (dc *RedisClusterDataConn) Rollback(ag *sabi.AsyncGroup)

Rollback does nothing for this data connection.

func (*RedisClusterDataConn) ShouldForceBack

func (dc *RedisClusterDataConn) ShouldForceBack() bool

ShouldForceBack returns true to indicate that this data connection should be forced back.

type RedisClusterDataSrc

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

RedisClusterDataSrc is a data source for Redis Cluster.

func NewRedisClusterDataSrc

func NewRedisClusterDataSrc(opt *redis.ClusterOptions) *RedisClusterDataSrc

NewRedisClusterDataSrc creates a new RedisClusterDataSrc.

func (*RedisClusterDataSrc) Close

func (ds *RedisClusterDataSrc) Close()

Close closes the Redis cluster client.

func (*RedisClusterDataSrc) CreateDataConn

func (ds *RedisClusterDataSrc) CreateDataConn() (sabi.DataConn, errs.Err)

CreateDataConn creates a new RedisClusterDataConn.

func (*RedisClusterDataSrc) Setup

func (ds *RedisClusterDataSrc) Setup(ag *sabi.AsyncGroup) errs.Err

Setup initializes the Redis cluster client and pings the cluster.

type RedisClusterDataSrcAlreadySetup

type RedisClusterDataSrcAlreadySetup struct{}

RedisClusterDataSrcAlreadySetup is an error reason which indicates that the data source is already setup.

type RedisClusterDataSrcFailToPing

type RedisClusterDataSrcFailToPing struct {
	Options *redis.ClusterOptions
}

RedisClusterDataSrcFailToPing is an error reason which indicates that the data source failed to ping.

type RedisClusterDataSrcNotSetupYet

type RedisClusterDataSrcNotSetupYet struct{}

RedisClusterDataSrcNotSetupYet is an error reason which indicates that the data source is not setup yet.

type RedisDataConn

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

RedisDataConn is a data connection for Redis.

func (*RedisDataConn) AddForceBack

func (dc *RedisDataConn) AddForceBack(fn func(*redis.Conn) errs.Err)

AddForceBack adds a function to be executed on force back.

func (*RedisDataConn) AddPostCommit

func (dc *RedisDataConn) AddPostCommit(fn func(*redis.Conn) errs.Err)

AddPostCommit adds a function to be executed after commit.

func (*RedisDataConn) AddPreCommit

func (dc *RedisDataConn) AddPreCommit(fn func(*redis.Conn) errs.Err)

AddPreCommit adds a function to be executed before commit.

func (*RedisDataConn) Close

func (dc *RedisDataConn) Close()

Close does nothing for this data connection.

func (*RedisDataConn) Commit

func (dc *RedisDataConn) Commit(ag *sabi.AsyncGroup) errs.Err

Commit does nothing for this data connection.

func (*RedisDataConn) ForceBack

func (dc *RedisDataConn) ForceBack(ag *sabi.AsyncGroup)

ForceBack executes force-back functions in reverse order.

func (*RedisDataConn) GetConnection

func (dc *RedisDataConn) GetConnection() *redis.Conn

GetConnection returns the underlying redis connection.

func (*RedisDataConn) PostCommit

func (dc *RedisDataConn) PostCommit(ag *sabi.AsyncGroup)

PostCommit executes post-commit functions.

func (*RedisDataConn) PreCommit

func (dc *RedisDataConn) PreCommit(ag *sabi.AsyncGroup) errs.Err

PreCommit executes pre-commit functions.

func (*RedisDataConn) Rollback

func (dc *RedisDataConn) Rollback(ag *sabi.AsyncGroup)

Rollback does nothing for this data connection.

func (*RedisDataConn) ShouldForceBack

func (dc *RedisDataConn) ShouldForceBack() bool

ShouldForceBack returns true to indicate that this data connection should be forced back.

type RedisDataSrc

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

RedisDataSrc is a data source for Redis.

func NewRedisDataSrc

func NewRedisDataSrc(opt *redis.Options) *RedisDataSrc

NewRedisDataSrc creates a new RedisDataSrc.

func (*RedisDataSrc) Close

func (ds *RedisDataSrc) Close()

Close closes the Redis client.

func (*RedisDataSrc) CreateDataConn

func (ds *RedisDataSrc) CreateDataConn() (sabi.DataConn, errs.Err)

CreateDataConn creates a new RedisDataConn.

func (*RedisDataSrc) Setup

func (ds *RedisDataSrc) Setup(ag *sabi.AsyncGroup) errs.Err

Setup initializes the Redis client and pings the server.

type RedisDataSrcAlreadySetup

type RedisDataSrcAlreadySetup struct{}

RedisDataSrcAlreadySetup is an error reason which indicates that the data source is already setup.

type RedisDataSrcFailToPing

type RedisDataSrcFailToPing struct {
	Options *redis.Options
}

RedisDataSrcFailToPing is an error reason which indicates that the data source failed to ping.

type RedisDataSrcNotSetupYet

type RedisDataSrcNotSetupYet struct{}

RedisDataSrcNotSetupYet is an error reason which indicates that the data source is not setup yet.

type RedisSentinelDataConn

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

RedisSentinelDataConn is a data connection for Redis Sentinel.

func (*RedisSentinelDataConn) AddForceBack

func (dc *RedisSentinelDataConn) AddForceBack(fn func(*redis.Conn) errs.Err)

AddForceBack adds a function to be executed on force back.

func (*RedisSentinelDataConn) AddPostCommit

func (dc *RedisSentinelDataConn) AddPostCommit(fn func(*redis.Conn) errs.Err)

AddPostCommit adds a function to be executed after commit.

func (*RedisSentinelDataConn) AddPreCommit

func (dc *RedisSentinelDataConn) AddPreCommit(fn func(*redis.Conn) errs.Err)

AddPreCommit adds a function to be executed before commit.

func (*RedisSentinelDataConn) Close

func (dc *RedisSentinelDataConn) Close()

Close does nothing for this data connection.

func (*RedisSentinelDataConn) Commit

func (dc *RedisSentinelDataConn) Commit(ag *sabi.AsyncGroup) errs.Err

Commit does nothing for this data connection.

func (*RedisSentinelDataConn) ForceBack

func (dc *RedisSentinelDataConn) ForceBack(ag *sabi.AsyncGroup)

ForceBack executes force-back functions in reverse order.

func (*RedisSentinelDataConn) GetConnection

func (dc *RedisSentinelDataConn) GetConnection() *redis.Conn

GetConnection returns the underlying redis connection.

func (*RedisSentinelDataConn) PostCommit

func (dc *RedisSentinelDataConn) PostCommit(ag *sabi.AsyncGroup)

PostCommit executes post-commit functions.

func (*RedisSentinelDataConn) PreCommit

func (dc *RedisSentinelDataConn) PreCommit(ag *sabi.AsyncGroup) errs.Err

PreCommit executes pre-commit functions.

func (*RedisSentinelDataConn) Rollback

func (dc *RedisSentinelDataConn) Rollback(ag *sabi.AsyncGroup)

Rollback does nothing for this data connection.

func (*RedisSentinelDataConn) ShouldForceBack

func (dc *RedisSentinelDataConn) ShouldForceBack() bool

ShouldForceBack returns true to indicate that this data connection should be forced back.

type RedisSentinelDataSrc

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

RedisSentinelDataSrc is a data source for Redis Sentinel.

func NewRedisSentinelDataSrc

func NewRedisSentinelDataSrc(opt *redis.FailoverOptions) *RedisSentinelDataSrc

NewRedisSentinelDataSrc creates a new RedisSentinelDataSrc.

func (*RedisSentinelDataSrc) Close

func (ds *RedisSentinelDataSrc) Close()

Close closes the Redis client.

func (*RedisSentinelDataSrc) CreateDataConn

func (ds *RedisSentinelDataSrc) CreateDataConn() (sabi.DataConn, errs.Err)

CreateDataConn creates a new RedisSentinelDataConn.

func (*RedisSentinelDataSrc) Setup

func (ds *RedisSentinelDataSrc) Setup(ag *sabi.AsyncGroup) errs.Err

Setup initializes the Redis client and pings the server.

type RedisSentinelDataSrcAlreadySetup

type RedisSentinelDataSrcAlreadySetup struct{}

RedisSentinelDataSrcAlreadySetup is an error reason which indicates that the data source is already setup.

type RedisSentinelDataSrcFailToPing

type RedisSentinelDataSrcFailToPing struct {
	Options *redis.Options
}

RedisSentinelDataSrcFailToPing is an error reason which indicates that the data source failed to ping.

type RedisSentinelDataSrcNotSetupYet

type RedisSentinelDataSrcNotSetupYet struct{}

RedisSentinelDataSrcNotSetupYet is an error reason which indicates that the data source is not setup yet.

Jump to

Keyboard shortcuts

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