### Receive and Print Redis Messages Source: https://github.com/ra1u/redis-dart/blob/master/README.md Example demonstrating how to connect to Redis, create a PubSub instance, subscribe to a channel, and process incoming messages from the returned stream. It includes error handling and connection closing logic. ```dart import 'dart:async'; import 'package:redis/redis.dart'; Future rx() async { Command cmd = await RedisConnection().connect('localhost', 6379); final pubsub = PubSub(cmd); pubsub.subscribe(["monkey"]); final stream = pubsub.getStream(); var streamWithoutErrors = stream.handleError((e) => print("error $e")); await for (final msg in streamWithoutErrors) { var kind = msg[0]; var food = msg[2]; if (kind == "message") { print("monkey got ${food}"); if (food == "cucumber") { print("monkey does not like cucumber"); cmd.get_connection().close(); } } else { print("received non-message ${msg}"); } } } ``` -------------------------------- ### Run Local Tests with Docker Compose Source: https://github.com/ra1u/redis-dart/blob/master/test/docker/Readme.txt Executes the local testing environment defined in docker-compose.yml. It starts containers and ensures they complete, with a note on cleaning up pubspec.lock if issues arise. ```shell docker-compose up --abort-on-container-exit && echo ok ``` -------------------------------- ### Publish Redis Messages Source: https://github.com/ra1u/redis-dart/blob/master/README.md Example demonstrating how to connect to Redis and publish messages to a specific channel. This function sends multiple messages and then closes the connection. ```dart import 'package:redis/redis.dart'; Future tx() async { Command cmd = await RedisConnection().connect('localhost', 6379); await cmd.send_object(["PUBLISH", "monkey", "banana"]); await cmd.send_object(["PUBLISH", "monkey", "apple"]); await cmd.send_object(["PUBLISH", "monkey", "peanut"]); await cmd.send_object(["PUBLISH", "monkey", "cucumber"]); cmd.get_connection().close(); } ``` -------------------------------- ### Redis CAS (Check-And-Set) Implementation Source: https://github.com/ra1u/redis-dart/blob/master/README.md Provides an example of implementing Compare-And-Swap (CAS) using the `Cas` class. This is crucial for conditional updates in transactions when the result of a previous command is needed. ```dart Cas cas = new Cas(command); cas.watch(["key"], (){ command.send_object(["GET","key"]).then((String val){ int i = int.parse(val); i++; cas.multiAndExec((Transaction trans){ trans.send_object(["SET","key",i.toString()]); }); }); }); ``` -------------------------------- ### Redis Transactions with Helpers Source: https://github.com/ra1u/redis-dart/blob/master/README.md Illustrates how to use the library's transaction helpers (`multi`, `Transaction`, `exec`) to group commands atomically. This example performs 200,000 INCR operations within a transaction. ```dart import 'package:redis/redis.dart'; ... final conn = RedisConnection(); conn.connect('localhost',6379).then((Command command){ command.multi().then((Transaction trans){ trans.send_object(["SET","val","0"]); for(int i=0;i<200000;++i){ trans.send_object(["INCR","val"]).then((v){ assert(i==v); }); } trans.send_object(["GET","val"]).then((v){ print("number is now $v"); }); trans.exec(); }); }); ``` -------------------------------- ### Publish Single Redis Message Source: https://github.com/ra1u/redis-dart/blob/master/README.md A simple example of sending a single message to a Redis channel using the Command object's send_object method. ```dart command.send_object(["PUBLISH","monkey","banana"]); ``` -------------------------------- ### High-Performance INCR Operations Source: https://github.com/ra1u/redis-dart/blob/master/README.md Demonstrates achieving high throughput by enabling Nagle's algorithm for pipelined commands. This example tests performance by executing 200,000 INCR operations and verifying each result. ```dart const int N = 200000; int start; final conn = RedisConnection(); conn.connect('localhost',6379).then((Command command){ print("test started, please wait ..."); start = DateTime.now().millisecondsSinceEpoch; command.pipe_start(); command.send_object(["SET","test","0"]); for(int i=1;i<=N;i++){ command.send_object(["INCR","test"]) .then((v){ if(i != v) throw("wrong received value, we got $v"); }); } //last command will be executed and then processed last command.send_object(["GET","test"]).then((v){ print(v); double diff = (new DateTime.now().millisecondsSinceEpoch - start)/1000.0; double perf = N/diff; print("$N operations done in $diff s\nperformance $perf/s"); }); command.pipe_end(); }); ``` -------------------------------- ### Connect and Execute Sequential Commands Source: https://github.com/ra1u/redis-dart/blob/master/README.md Shows how to establish a connection to a Redis server and execute a series of commands sequentially, waiting for each command's response before proceeding. This ensures ordered execution and response handling. ```dart import 'package:redis/redis.dart'; final conn = RedisConnection(); conn.connect('localhost', 6379).then((Command command){ command.send_object(["SET","key","0"]).then((var response){ assert(response == 'OK'); return command.send_object(["INCR","key"]); }) .then((var response){ assert(response == 1); return command.send_object(["INCR","key"]); }) .then((var response){ assert(response == 2); return command.send_object(["INCR","key"]); }) .then((var response){ assert(response == 3); return command.send_object(["GET","key"]); }) .then((var response){ return print(response); // 3 }); }); ``` -------------------------------- ### Handling Binary Data with Redis Source: https://github.com/ra1u/redis-dart/blob/master/README.md Shows how to send and receive binary data using the redis-dart library. It demonstrates setting a key with binary data and retrieving it, ensuring the data remains intact. ```dart final conn = RedisConnection(); Command cmd = await conn.connect('localhost',6379); Command cmd_bin = Command.from(cmd).setParser(RedisParserBulkBinary()); List d = [1,2,3,4,5,6,7,8,9]; // send binary await cmd_bin.send_object(["SET", key, RedisBulk(d)]); // receive binary from binary command handler var r = await cmd_bin.send_object(["GET", key]) // r is now same as d ``` -------------------------------- ### Run Local Tests with Podman Script Source: https://github.com/ra1u/redis-dart/blob/master/test/docker/Readme.txt Provides an alternative method for running local tests using a shell script designed for rootless execution with Podman. This script replaces the need for Docker. ```shell ./run_podman.sh ``` -------------------------------- ### Basic Command Execution Source: https://github.com/ra1u/redis-dart/blob/master/README.md Demonstrates sending a simple Redis command using `send_object` with a list representing the command and its arguments. This is the fundamental way to interact with Redis. ```dart Future f = command.send_object(["SET","key","value"]); ``` -------------------------------- ### Initialize PubSub Helper Source: https://github.com/ra1u/redis-dart/blob/master/README.md Creates a PubSub helper instance from an existing Command object. Note that the Command object is invalidated after creating the PubSub instance and should not be used on the same connection. ```dart final pubsub = PubSub(command); ``` -------------------------------- ### Nested Command Execution with EVAL Source: https://github.com/ra1u/redis-dart/blob/master/README.md Demonstrates executing a complex Redis command like EVAL, which involves nested structures for keys, arguments, and return values. This showcases the library's ability to handle arbitrarily nested Redis responses. ```dart command.send_object(["EVAL","return {KEYS[1],{KEYS[2],{ARGV[1]},ARGV[2]},2}","2","key1","key2","first","second"]) .then((response){ print(response); }); ``` -------------------------------- ### Secure Connection (TLS) with AUTH Source: https://github.com/ra1u/redis-dart/blob/master/README.md Explains how to establish a secure TLS connection to a Redis server using `connectSecure`. It also shows how to authenticate using the AUTH command before executing other Redis operations. ```dart import 'package:redis/redis.dart'; final conn = RedisConnection(); conn.connectSecure('localhost', 6379).then((Command command) { command .send_object(["AUTH", "username", "password"]).then((var response) { print(response); command.send_object(["SET", "key", "0"]).then( (var response) => print(response)); }); }); ``` -------------------------------- ### Redis Pub/Sub API Commands Source: https://github.com/ra1u/redis-dart/blob/master/README.md Defines the core commands for interacting with Redis Publish/Subscribe functionality, including subscribing, unsubscribing, and managing message streams. The getStream() method returns a Dart Stream for receiving messages. ```APIDOC PubSub Commands: subscribe(List channels) - Subscribes the client to the given channels. psubscribe(List channels) - Subscribes the client to channels matching the given patterns. unsubscribe(List channels) - Unsubscribes the client from the given channels. punsubscribe(List channels) - Unsubscribes the client from channels matching the given patterns. Stream getStream() - Returns a Dart Stream that emits received messages. ``` -------------------------------- ### Orchestrate Publisher and Subscriber Source: https://github.com/ra1u/redis-dart/blob/master/README.md Main function to run both the message publishing (tx) and message receiving (rx) asynchronous operations concurrently. It waits for both to complete. ```dart import 'dart:async'; // Assume rx() and tx() functions are defined as above // Future rx() { ... } // Future tx() { ... } void main() async { var frx = rx(); var ftx = tx(); await ftx; await frx; } ``` -------------------------------- ### Execute Commands Concurrently Source: https://github.com/ra1u/redis-dart/blob/master/README.md Illustrates sending multiple Redis commands without waiting for individual responses, relying on Futures to manage the order of response handling. This can improve performance by overlapping I/O operations. ```dart import 'package:redis/redis.dart'; final conn = RedisConnection(); conn.connect('localhost',6379).then((Command command){ command.send_object(["SET","key","0"]) .then((var response){ assert(response == 'OK'); }); command.send_object(["INCR","key"]) .then((var response){ assert(response == 1); }); command.send_object(["INCR","key"]) .then((var response){ assert(response == 2); }); command.send_object(["INCR","key"]) .then((var response){ assert(response == 3); }); command.send_object(["GET","key"]) .then((var response){ print(response); // 3 }); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.