Redis Model
Redis model is basic implementation of few methods for creating model which store data in Redis
Instalation
Just in console
gem install redis-model-extension
Or put into Gemfile
gem "redis-model-extension"
and somewhere before use (not rails - they will require gem automaticaly)
require "redis-model-extension"
Initialization
You can use yml config file redis_config.yml
in config
directory:
test:
host: "127.0.0.1"
port: 6379
db: 3
other omnited...
Or you can setup directly in initializer (or before any of redis call) redis instance directly:
RedisModelExtension::Database.redis = Redis.new(host: "127.0.0.1", port: 6379, db: 0)
Or last way to set redis is by providing only config arguments:
Use this in your initializer and when you use Forks or resque workers this will work very nicely,
unless you will call to redis before workers are initialized.
Then you will get error Redis instance cannot be used in Forks.
You can fix this by calling in the start of worker: RedisModelExtension::Database.redis = nil
This will remove old not possible to use redis instance and in first call to redis will use new config and create new redis instance.
RedisModelExtension::Database.redis_config(:host => "127.0.0.1", :port => 6379, :db => 0)
Usage
Your class needs to include RedisModel and there is your testing configuration:
(old initialization - still working :))
Lots of aditional informations can be found in WIKI or directly:
class TestRedisModel
include RedisModelExtension
redis_field :field1, :integer
redis_field :field2, :bool
redis_field :field3, :string, "Default string"
redis_field :field4, :symbol
redis_field :field5, :array
redis_field :field6, :hash
redis_field :field7, :time
redis_field :field8, :date
redis_key :field1, :field2
redis_validate :field1, :field3
reddis_key_normalize :transliterate, :downcase
redis_alias :token, [:field4]
end
foo = TestRedisModel.new()
foo.field3
foo.field3 = "bar"
foo.field3
foo.to_args
foo.to_json
foo.update(:field1 => 234, :field3 => "bar")
foo.valid?
unless foo.save
puts foo.errors
end
bar = TestRedisModel.create field1: 123
if (bar = TestRedisModel.create field1: 123).errors.any?
puts bar.errors
end
TestRedisModel.find(:field3 => "foo")
TestRedisModel.get(:field3 => "foo", :field4=> true)
TestRedisModel.exists?(:field3 => "foo", :field4=> true)
TestRedisModel.find_by_alias :token, :field4=> true
TestRedisModel.find_by_token :field4=> true
Dirty
If you want to use ActiveModel::Dirty, i.e. methods like _changed?
, _was?
you can include
a Dirty module to your model (right after RedisModelExtension)
class MyModel
include RedisModelExtension
include RedisModelExtension::Dirty
end
Change log
- 0.4.2
- Add ActiveModel::Dirty
- Change work with attributes to ActiveModel::Attributes
- 0.4.1
- Fixed bugs in intialization
- Changed aliases to use key - array instead of key - value (enable find by category...) WIKI: Aliases
- Add better readme and filled WIKI
- 0.4.0
- Redesigned initialization method from: old one to: new one
- Added Before After Hooks
- Added Auto-increment IDs
- Added dynamic aliases
- Added Active Model Validations
- REFACTORED whole structure of redis model (similar methods moved to separate modules)
- Set default to save nil values to redis
- 0.3.8
- Allow to don't save nil values into redis
- 0.3.7
- Fix dependencies
- 0.3.6
- Fix problem with working in Forks
- for older look at Commit messages
Contributing to redis-model-extension
- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
- Fork the project.
- Start a feature/bugfix branch.
- Commit and push until you are happy with your contribution.
- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
- Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright
Copyright (c) 2012 Ondrej Bartas. See LICENSE.txt for
further details.