Cacchern
operate Redis with validation by ActiveModel
Installation
Add this line to your application's Gemfile:
gem 'cacchern'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cacchern
Usage
- available classes
- Object(String)
- Set
- SortedSet
Object
Cacchern::Object
is simple key-value class
- Create classes
Create class that overrides Cacchern::Object
.
class Token < Cacchern::Object
validates :value, presence: true
end
- Add value
Token.new(1, 'some token...').save
If you want to save with ttl.
Token.new(1, 'some token...').save(expires_in: 1.hours)
If you make raise Error
with save, you can use save!
method.
3.Get value
token = Token.find(1)
print token.value # 'some token...'
SortedSet
- Create classes
Create class that overrides Cacchern::SortableMember
class Score < Cacchern::SortableMember
validates :key, presence: true, numericality: { only_integer: true }
validates :value, presence: true, numericality: true
end
Create class that overrides Cacchern::SortableMember
class ScoreSet < Cacchern::SortedSet
contain_class Score
end
- Add value
base_set = ScoreSet.new('base')
#=> #<ScoreSet:0x000056544559a520 @key="score_set:base">
score = Score.new(1, 100)
#=> #<Score:0x00005654456194b0
# @errors=#<ActiveModel::Errors:0x0000565445619050 @base=#<Score:0x00005654456194b0 ...>, @details={}, @messages={}>,
# @key=1,
# @validation_context=nil,
# @value=100>
base_set.add(score)
base_set.add(Score.new(2,50))
base_set.add_all([Score.new(3,50),Score.new(4,40)])
- Get Values
base_set.order(:desc)
#=> [#<Score:0x000056544570ebb8
# @errors=#<ActiveModel::Errors:0x000056544570ea28 @base=#<Score:0x000056544570ebb8 ...>, @details={}, @messages={}>,
# @key="1",
# @validation_context=nil,
# @value=100.0>,
# #<Score:0x000056544570e230
# @errors=#<ActiveModel::Errors:0x000056544570e0a0 @base=#<Score:0x000056544570e230 ...>, @details={}, @messages={}>,
# @key="2",
# @validation_context=nil,
# @value=50.0>]
base_set.order(:asc)
#=> [#<Score:0x000056544579aaa0
# @errors=#<ActiveModel::Errors:0x000056544579a910 @base=#<Score:0x000056544579aaa0 ...>, @details={}, @messages={}>,
# @key="2",
# @validation_context=nil,
# @value=50.0>,
# #<Score:0x000056544579a168
# @errors=#<ActiveModel::Errors:0x0000565445799fd8 @base=#<Score:0x000056544579a168 ...>, @details={}, @messages={}>,
# @key="1",
# @validation_context=nil,
# @value=100.0>]
- Remove value
base_set.remove(2)
#=> true
base_set.order(:asc)
#=> [#<Score:0x0000565445850990
# @errors=#<ActiveModel::Errors:0x0000565445850800 @base=#<Score:0x0000565445850990 ...>, @details={}, @messages={}>,
# @key="1",
# @validation_context=nil,
# @value=100.0>]
base_set.remove_all
- IncrementableSortedSet
class IScoreSet < Cacchern::IncrementableSortedSet
contain_class Score
end
base_set = IScoreSet.new('base')
base_set.add(Score.new(2,50))
base_set.increment(Score.new(2,50))
Development
After checking out the repo, run bin/setup
to install dependencies. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/shuyuhey/cacchern.
License
The gem is available as open source under the terms of the MIT License.