RedisBackedModel
Provides useful functions to objects that are backed by a Redis store instead of ActiveRecord.
Installation
Add this line to your application's Gemfile:
gem 'redis_backed_model'
And then execute:
$ bundle
Or install it yourself as:
$ gem install redis_backed_model
Usage
Subclass your models from RedisBackedModel::RedisBackedModel
class Person < RedisBackedModel::RedisBackedModel
end
When initializing a person, pass in a hash of attributes
p = Person.new({:id => 2, :first_name => "Bill", :last_name => "Smith"})
RBM will create instance variables as needed
p.instance_variables => [:@id, :@first_name, :@last_name]
You can use RBM to get Redis commands that will save your object as a hash
p.to_redis => ["sadd|person_ids|2", "hset|person:2|id|2", "hset|person:2|first_name|Bill", "hset|person:2|last_name|Smith"]
If the value of a variable is nil, it is not included in the to_redis commands
p = Person.new({:id => 2, :first_name => "Bill", :last_name => nil})
p.to_redis => ["sadd|person_ids|2", "hset|person:2|id|2", "hset|person:2|first_name|Bill"]
You can parse the to_redis results and pass them to Redis yourself or use the gem 'redis_pipeline': https://github.com/SeniorServiceAmerica/redis_pipeline
Once your data is in Redis, you can use RBM to find and instantiate objects:
p = Person.find(2) => #<Person:0x00000104023a00 @id=2, @first_name=Bill, @last_name=Smith>
You can also find multiple records:
p = Person.find([1,2,3]) => [person,person,person]
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request