ArRedis allows you to easily create and remove nested, namespaced Redis keys under ActiveRecord objects and classes. The functionality is inspired by this RailsConf presentation by Obie Fernandez. Also, Nest is a similar gem that does not hook into ActiveRecord.
Add this line to the Gemfile of an application that uses ActiveRecord:
gem 'ar_redis'
And then execute:
$ bundle
If you are not using Redis in you application yet, setup the redis-rb gem.
All ActiveRecord models will be provided with the method #redis
.
Given:
class TestModel < ActiveRecord::Base
end
You can now create nested, namespaced Redis keys that have access to all Redis commands.
>> test_model = TestModel.create
>> #<TestModel:0x007fc08a466890 id: 1>
>> test_model.redis.key
=> "TestModel:1"
>> test_model.redis[:reports]["01/2018"].key
=> "TestModel:1:reports:01/2018"
>> test_model.redis[:reports]["01/2018"].call(:set, { clicks: 12, views: 100 }.to_json)
=> "OK"
>> JSON.parse(test_model.redis[:reports]["01/2018"].call(:get))
=> { clicks: 12, views: 100 }
Here's an example of how you can pass a specific Redis client to ArRedis
# initializers/redis.rb
$redis = Redis.new(host: "10.0.1.1", port: 6380, db: 15)
ArRedis.redis = $redis
Write some code!
- Fork ArRedis
- Create a topic branch -
git checkout -b my_branch
- Push to your branch -
git push origin my_branch
- Create a Pull Request from your branch
- That's it!
If you're not doing some sort of refactoring, a CHANGELOG entry is appropriate. Please include them in pull requests adding features or fixing bugs.
We use rspec for testing.
A simple bundle exec rspec
will run all the tests. Make sure they pass when
you submit a pull request.
The gem is available as open source under the terms of the MIT License.