Golang
The DiceDB Go SDK provides support for all the standard Redis commands plus specialized DiceDB watch commands for monitoring changes to data in real-time.
Installation
To install the DiceDB Go SDK, use go get
:
Basic Usage
The SDK maintains compatibility with go-redis for standard Redis operations. If you’re using go-redis, you can transparently switch to dicedb-go with minimal changes to your application code.
Watch Commands
Creating a Watch Connection
WatchConn
creates a new watch connection. You can listen on results of different commands using this connection.
Watching Commands
There are two ways to watch commands using a WatchConn
object:
1. Generic Watch method
The first argument is the context, followed by the command and its arguments:
Note: Ensure that the command you’re watching is supports reactivity.
2. Command-specific convenience methods:
The first argument is the context, followed by the command’s arguments.
Each watch command returns a WatchResult
object containing the initial result of the watched command.
WatchResult Structure
The WatchResult
object contains the updated data along with the command being watched and a unique fingerprint to identify the source of the update:
The Data
field contains the result of the watched command, and can be type-asserted based on the command being watched.
For example, the Data
field for:
ZRANGE.WATCH
command will be of type[]dicedb.Z
.GET.WATCH
command will be of typestring
.
Channel-based Monitoring
To receive subsequent updates to the result set, use the Channel method:
Note: The Channel
method must be called after the initial watch command has been executed.
Channel configuration options:
WithWChannelSize(size int)
: Set channel buffer size (default: 100)WithWChannelHealthCheckInterval(d time.Duration)
: Set health check interval (default: 3s)WithWChannelSendTimeout(d time.Duration)
: Set message send timeout (default: 60s)