lib/redstream.rb in redstream-0.0.1 vs lib/redstream.rb in redstream-0.1.0

- old
+ new

@@ -1,6 +1,5 @@ - require "active_support/inflector" require "connection_pool" require "redis" require "json" require "thread" @@ -57,21 +56,32 @@ def self.namespace @namespace end + # Returns the length of the specified stream. + # + # @param stream_name [String] The stream name + # @return [Integer] The length of the stream + + def self.stream_size(stream_name) + connection_pool.with do |redis| + redis.xlen(stream_key_name(stream_name)) + end + end + # Returns the max id of the specified stream, i.e. the id of the # last/newest message added. Returns nil for empty streams. # # @param stream_name [String] The stream name # @return [String, nil] The id of a stream's newest messages, or nil def self.max_stream_id(stream_name) connection_pool.with do |redis| message = redis.xrevrange(stream_key_name(stream_name), "+", "-", count: 1).first - return unless message + return nil unless message message[0] end end @@ -129,6 +139,5 @@ def self.base_key_name [namespace, "redstream"].compact.join(":") end end -