conn

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Connection

type Connection struct {
	Host string
	Port string

	ServerInfo map[string]string
	// contains filtered or unexported fields
}

Connection represents a TCP connection to a Redis server.

C#:

public class Connection : IDisposable {
    private TcpClient _client;
    private StreamReader _reader;
    public Dictionary<string, string> ServerInfo { get; private set; }
}

func Connect

func Connect(host, port, user, pass string) (*Connection, error)

Connect establishes a TCP connection to Redis and performs authentication if required.

C#: public Connection(string host, int port, string password = null)

Go: We return (*Connection, error) instead of throwing exceptions in a constructor.

func (*Connection) Close

func (c *Connection) Close() error

Close terminates the TCP connection.

func (*Connection) FetchServerCommands

func (c *Connection) FetchServerCommands() ([]command.ServerCommand, error)

getServerInfo sends the INFO command and parses the result into the ServerInfo map.

C#:

private void GetServerInfo() {
    Send("INFO");
    var response = Receive();
    // ... parse string into Dictionary
}

FetchServerCommands sends the COMMAND command to the Redis server and parses the response into a slice of ServerCommand for registry merging. Returns nil, nil if the server does not support COMMAND.

func (*Connection) GetKeyValue

func (c *Connection) GetKeyValue(key string) (typeName string, single resp.RedisValue, collection iter.Seq[resp.RedisValue], err error)

GetKeyValue determines the type of a key and returns either its single value or an iterator for its collection.

C#: public (string typeName, IRedisValue single, IEnumerable<IRedisValue> collection) GetKeyValue(string key)

func (*Connection) Receive

func (c *Connection) Receive(timeout time.Duration) (resp.RedisValue, error)

Receive reads a single RESP value from the server, optionally with a timeout.

func (*Connection) SafeHash

func (c *Connection) SafeHash(key string) iter.Seq[resp.RedisValue]

SafeHash iterates over all fields and values of a Hash using the HSCAN command.

C# Bug Fix: The C# version iterated over the top-level array, yielding the cursor string and the sub-array. We correctly iterate over the sub-array (Values[1]).

func (*Connection) SafeKeys

func (c *Connection) SafeKeys(pattern string) iter.Seq[resp.RedisValue]

SafeKeys iterates over all keys matching a pattern using the SCAN command.

C#:

public IEnumerable<RedisValue> SafeKeys(string pattern) {
    string cursor = "0";
    while (true) { ... yield return key; ... }
}

Go: We use Go 1.23's iter.Seq. If an error occurs, we yield a RedisError and stop.

func (*Connection) SafeList

func (c *Connection) SafeList(key string) iter.Seq[resp.RedisValue]

SafeList iterates over all elements of a List using the LRANGE command.

C# Bug Fix: The C# version used overlapping indices (i to i+100) and an unnecessary LLEN call. We use start to start+99 and stop when the returned array is empty.

func (*Connection) SafeSets

func (c *Connection) SafeSets(key string) iter.Seq[resp.RedisValue]

SafeSets iterates over all members of a Set using the SSCAN command.

func (*Connection) SafeSortedSets

func (c *Connection) SafeSortedSets(key string) iter.Seq[resp.RedisValue]

SafeSortedSets iterates over all members and scores of a Sorted Set using the ZSCAN command.

func (*Connection) SafeStream

func (c *Connection) SafeStream(key string) iter.Seq[resp.RedisValue]

SafeStream iterates over all entries of a Stream using the XRANGE command.

C# Bug Fix: The C# version used the FIRST element's ID as the next cursor, causing an O(N^2) loop. We use the LAST element's ID and prepend `(` to make it exclusive (Redis 6.2+).

func (*Connection) Send

func (c *Connection) Send(cmd *command.ParsedCommand) error

Send writes a parsed command to the Redis server.

func (*Connection) SendRaw

func (c *Connection) SendRaw(args ...string) error

SendRaw writes a RESP command directly from raw string arguments, bypassing command.Parse(). This avoids quoting issues for values that contain spaces, quotes, newlines, or binary data — RESP bulk strings are length-prefixed, so any byte sequence is safe.

C#: No direct equivalent — the C# version always routed through the parser.

func (*Connection) Subscribe

func (c *Connection) Subscribe(ctx context.Context) iter.Seq[resp.RedisValue]

Subscribe listens for messages on a subscribed channel until the context is cancelled.

C#: public IEnumerable<IRedisValue> Subscribe(CancellationToken token)

Go: We use context.Context for cancellation. We set a short read deadline in a loop so we can periodically check ctx.Err() without blocking forever on Receive.

Jump to

Keyboard shortcuts

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