Petruchio
a Redis ring buffer in Ruby and Lua
“We will have rings, and things, and fine array.”
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.)
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’re doing? Come work with us.