Skip to content

Commit

Permalink
[FIX] Memory usage: CAP the stream (#6)
Browse files Browse the repository at this point in the history
* [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
garciaf authored Sep 25, 2024
1 parent 16e177a commit e640dc3
Showing 3 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
redis_stream (0.1.4)
redis_stream (0.1.5)

GEM
remote: https://rubygems.org/
2 changes: 1 addition & 1 deletion lib/redis_stream/publisher.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/redis_stream/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module RedisStream
VERSION = "0.1.4"
VERSION = "0.1.5"
end

0 comments on commit e640dc3

Please sign in to comment.