Skip to content

Latest commit

 

History

History
69 lines (41 loc) · 2.09 KB

README.md

File metadata and controls

69 lines (41 loc) · 2.09 KB

Petruchio - a Redis ring buffer in Ruby and Lua

We will have rings, and things, and fine array.

-Petruchio (The Taming of the Shrew)

What?

Need a Redis ring buffer? Here's a simple one in Ruby and Lua.

Supports resizing too if you later need to grow or shrink the buffer.

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add petruchio

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install petruchio

Usage

There's a bunch of reasons you might want to use a ring buffer. For example, you might need one to back a rate limiter. Give it your Redis instance, a name for the buffer, buffer size, and a default value each entry in the buffer is initialized with:

@ring = Petruchio::Ring.new(redis_instance, 'request_limiter', 10, 0.0)

Once you have a ring, pop will remove the oldest things from the buffer:

previous_request_time = @ring.pop.to_f

push will add the newest things:

@ring.push(future_request_time.to_f)

The buffer doesn't monitor its own constant size (i.e. it won't currently raise an error if you push to a full buffer). So you could push and pop simultaneously.

previous_request_time = @ring.push_pop(future_request_time.to_f)

Or lock around your push/pop operations. (And if you need a lock stay tuned)

Todos

Potentially nice things this could have:

  • Error when trying to add to the buffer and its full
  • Force push to the buffer

License

The gem is available as open source under the terms of the MIT License.

Feedback

Source code available on Github. Feedback and pull requests are greatly appreciated. Let us know if we can improve this.

From

👋 The folks at Census originally put this together. Have data? We'll sync your data warehouse with your CRM and other apps critical to your team. Interested in what we do? Come work with us.