RedisField is a simple Ruby gem that stores model-specific data to Redis just like normal ActiveRecord attributes.
Great for caches, API responses etc.
You will need ActiveRecord (>= 3.2).
Add this line to your application's Gemfile:
gem 'redis_field'
And then execute:
$ bundle
A custom key prefix in Redis (ar_redis_field is default)
RedisField::Base.path_prefix = 'my_custom_prefix'
Redis.new is default
RedisField::Base.redis = Redis.new(:url => "redis://:[email protected]:6380/15")
Your app's environment. It checks ENV['RAILS_ENV'] by default. If Rails env is not specified, default env is development
RedisField::Base.env = "staging"
class User < ActiveRecord::Base
redis_field :some_data
# redis_fields is just an alias to redis_field
redis_fields :facebook_friend_ids, :last_logged_in
end
user = User.last
user.some_data = "Testing redis_field"
user.save
user.reload.some_data # -> "Testing redis_field"
user.update_attributes(last_logged_in: Time.now)
user.reload.last_logged_in # -> 2014-04-05 01:00:00 +0000
NB This library is not an ORM for Redis, so it simply marshalls data before storing to Redis. There's no types coercion or anything like that.
More docs and features soon. ❤️
Just run
$ rake spec
See Appraisal for testing against different ActiveRecord versions.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request