From 4f486f65a12fd219602252f0f3a199656964c167 Mon Sep 17 00:00:00 2001 From: Dmytro Havrysh Date: Thu, 14 May 2026 16:58:34 +0200 Subject: [PATCH] [FEATURE] Expose maxlen configuration option and update publisher to use dynamic max length --- README.md | 11 +++++++++-- lib/redis_stream/configuration.rb | 7 ++++++- lib/redis_stream/publisher.rb | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 160b40e..3d03899 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ Welcome to your new gem! In this directory, you'll find the files you need to be ## Installation Install the gem and add to the application's Gemfile by executing: + ```ruby # Gemfile $ gem "redis_stream", git: "https://github.com/test-IO/redis_stream" @@ -12,17 +13,23 @@ Install the gem and add to the application's Gemfile by executing: ## Usage -Initialize the gem with the Redis client you want to use in you `config/initializers/ +Initialize the gem with the Redis client you want to use in you `config/initializers/` ```ruby RedisStream.configure do |config| - config.redis(Redis.new(url: "redis://localhost:6379/0")) + config.redis_url("redis://localhost:6379/0") config.group("group_name") config.consumer("consumer_name") config.stream("stream_name") + config.logging_stream("logging_stream_name") + config.maxlen(200) # optional, defaults to 100 end ``` + +`maxlen` is optional and caps the stream length when publishing (passed to Redis `XADD` with the approximate flag). If omitted, the default of 100 is used. + ### Subscribe + To subscribe you will always need to provide the name of the stream you want to listen to, you can also provide an array of stream. ```ruby diff --git a/lib/redis_stream/configuration.rb b/lib/redis_stream/configuration.rb index 3fce76e..26ae297 100644 --- a/lib/redis_stream/configuration.rb +++ b/lib/redis_stream/configuration.rb @@ -1,12 +1,13 @@ module RedisStream class Configuration - attr_accessor :client, :group_id, :consumer_id, :stream_key, :logging_stream_key + attr_accessor :client, :group_id, :consumer_id, :stream_key, :logging_stream_key, :max_length def initialize @group = "group" @consumer = "consumer" @stream_key = "stream" @logging_stream_key = "logging_stream" + @max_length = 100 end def redis(client) @@ -32,5 +33,9 @@ def stream(stream) def logging_stream(logging_stream) @logging_stream_key = logging_stream end + + def maxlen(max_length) + @max_length = max_length + end end end diff --git a/lib/redis_stream/publisher.rb b/lib/redis_stream/publisher.rb index 9df78b3..c4450f3 100644 --- a/lib/redis_stream/publisher.rb +++ b/lib/redis_stream/publisher.rb @@ -10,7 +10,7 @@ def initialize(stream_key) def publish(name, data = {}) data = {"name" => name, "json" => JSON.generate(data)} - RedisStream.client.xadd(@stream_key, data, maxlen: 1000, approximate: true) + RedisStream.client.xadd(@stream_key, data, maxlen: RedisStream.config.max_length, approximate: true) end end end