redis

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2025 License: MIT Imports: 8 Imported by: 0

README

Redis CLI

Go Reference Go Report Card

A standalone, feature-rich Redis interactive CLI client for Go applications.

Features

  • 🚀 Full Redis protocol support
  • 🔧 Customizable connection parameters
  • 🔒 TLS/SSL support
  • 💾 Connection pooling with configurable size
  • ⏱️ Query timing
  • 🔄 Transaction support (MULTI/EXEC/DISCARD)
  • 📝 Command history
  • 🎯 Database selection
  • 👤 Redis 6.0+ ACL username support

Installation

go get github.com/binrchq/redis-cli

Quick Start

package main

import (
    "log"
    "os"
    
    rediscli "github.com/binrchq/redis-cli"
)

func main() {
    cli := rediscli.NewCLI(
        os.Stdin,
        "localhost",
        6379,
        "password",
    )
    
    if err := cli.Connect(); err != nil {
        log.Fatal(err)
    }
    defer cli.Close()
    
    if err := cli.Start(); err != nil {
        log.Fatal(err)
    }
}

Advanced Configuration

config := &rediscli.Config{
    Host:         "localhost",
    Port:         6379,
    Password:     "password",
    DB:           0,
    DialTimeout:  15 * time.Second,
    ReadTimeout:  60 * time.Second,
    WriteTimeout: 60 * time.Second,
    PoolSize:     20,
    MinIdleConns: 5,
    MaxRetries:   5,
    Username:     "default", // Redis 6.0+
    TLSEnabled:   true,
}

cli := rediscli.NewCLIWithConfig(terminal, config)

Supported Commands

All Redis commands are supported including:

String Commands
  • GET, SET, INCR, DECR, APPEND
Hash Commands
  • HGET, HSET, HGETALL, HDEL
List Commands
  • LPUSH, RPUSH, LPOP, RPOP, LRANGE
Set Commands
  • SADD, SREM, SMEMBERS, SISMEMBER
Sorted Set Commands
  • ZADD, ZRANGE, ZRANK, ZSCORE
Key Commands
  • KEYS, DEL, EXISTS, TTL, EXPIRE
Transaction Commands
  • MULTI, EXEC, DISCARD

Special Commands

  • help - Show help
  • exit, quit - Exit CLI
  • timing - Toggle timing
  • SELECT <db> - Switch database
  • conninfo - Show connection info

Requirements

  • Go 1.21 or higher
  • Redis 5.0 or higher

Dependencies

License

MIT License - see LICENSE file for details.

Author

Maintained by binrc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CLI

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

CLI Redis 交互式命令行客户端

func NewCLI

func NewCLI(term Terminal, host string, port int, password string) *CLI

NewCLI 创建新的 Redis CLI 实例(兼容旧接口)

func NewCLIWithConfig

func NewCLIWithConfig(term Terminal, config *Config) *CLI

NewCLIWithConfig 使用配置创建 Redis CLI 实例

func (*CLI) Close

func (c *CLI) Close() error

Close 关闭 Redis 连接

func (*CLI) Connect

func (c *CLI) Connect() error

Connect 连接到 Redis

func (*CLI) Start

func (c *CLI) Start() error

Start 启动交互式命令行

type Config

type Config struct {
	Host         string
	Port         int
	Password     string
	DB           int                    // 数据库编号,默认 0
	DialTimeout  time.Duration          // 拨号超时,默认 10s
	ReadTimeout  time.Duration          // 读超时,默认 30s
	WriteTimeout time.Duration          // 写超时,默认 30s
	PoolSize     int                    // 连接池大小,默认 10
	MinIdleConns int                    // 最小空闲连接,默认 2
	MaxRetries   int                    // 最大重试次数,默认 3
	PoolTimeout  time.Duration          // 连接池超时,默认 4s
	IdleTimeout  time.Duration          // 空闲连接超时,默认 5min
	Username     string                 // Redis 6.0+ 用户名
	TLSEnabled   bool                   // 是否启用 TLS
	CustomParams map[string]interface{} // 自定义参数
}

Config Redis 连接配置

type ReadWriteCloser added in v0.1.1

type ReadWriteCloser struct {
	io.ReadWriter
}

ReadWriteCloser wraps io.ReadWriter to add a no-op Close method

func (*ReadWriteCloser) Close added in v0.1.1

func (rwc *ReadWriteCloser) Close() error

type Reader

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

Reader 从终端读取输入(使用 readline 以支持SSH session)

func NewReader

func NewReader(term io.ReadWriter) *Reader

NewReader 创建新的 Reader

func (*Reader) Close added in v0.1.1

func (r *Reader) Close() error

Close 关闭读取器

func (*Reader) ReadLine

func (r *Reader) ReadLine() (string, error)

ReadLine 读取一行输入

type ServerInfo

type ServerInfo struct {
	Version          string
	Mode             string // standalone, sentinel, cluster
	Role             string // master, slave
	ConnectedClients int
	UsedMemory       string
	Uptime           int64
}

ServerInfo Redis 服务器信息

type Terminal

type Terminal interface {
	io.Reader
	io.Writer
}

Terminal 终端接口,用于输入输出

Jump to

Keyboard shortcuts

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