From e640dc315f2acaaba520b381488d40ac9dd054df Mon Sep 17 00:00:00 2001 From: Garcia Fabien Date: Wed, 25 Sep 2024 15:12:48 +0200 Subject: [PATCH] [FIX] Memory usage: CAP the stream (#6) * [FIX] Memory usage CAO the stream Make sure we keep our stream in check using the maxlen option. According to the documentation # Documentation https://redis.io/docs/latest/commands/xadd/ ## Capped streams XADD incorporates the same semantics as the XTRIM command - refer to its documentation page for more information. This allows adding new entries and keeping the stream's size in check with a single call to XADD, effectively capping the stream with an arbitrary threshold. Although exact trimming is possible and is the default, due to the internal representation of steams it is more efficient to add an entry and trim stream with XADD using almost exact trimming (the ~ argument). For example, calling XADD in the following form: ``` XADD mystream MAXLEN ~ 1000 * ... entry fields here ... ``` Will add a new entry but will also evict old entries so that the stream will contain only 1000 entries, or at most a few tens more. * formatting --- Gemfile.lock | 2 +- lib/redis_stream/publisher.rb | 2 +- lib/redis_stream/version.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 2661c7e..efce514 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - redis_stream (0.1.4) + redis_stream (0.1.5) GEM remote: https://rubygems.org/ diff --git a/lib/redis_stream/publisher.rb b/lib/redis_stream/publisher.rb index 932ac57..9df78b3 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) + RedisStream.client.xadd(@stream_key, data, maxlen: 1000, approximate: true) end end end diff --git a/lib/redis_stream/version.rb b/lib/redis_stream/version.rb index 381e447..125fd8d 100644 --- a/lib/redis_stream/version.rb +++ b/lib/redis_stream/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module RedisStream - VERSION = "0.1.4" + VERSION = "0.1.5" end