mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-18 21:06:40 +00:00
Make Model.expire act like the other options, and add tests for it
This commit is contained in:
@@ -46,8 +46,12 @@ module Redistat
|
||||
|
||||
alias :class_name :scope
|
||||
|
||||
def expire(exp)
|
||||
options[:expire] = exp.is_a?(Hash) ? exp : Hash.new(exp)
|
||||
def expire(exp = nil)
|
||||
if !exp.nil?
|
||||
options[:expire] = exp.is_a?(Hash) ? exp : Hash.new(exp)
|
||||
else
|
||||
options[:expire]
|
||||
end
|
||||
end
|
||||
|
||||
def connect_to(opts = {})
|
||||
|
||||
@@ -26,5 +26,6 @@ class ModelHelper4
|
||||
include Redistat::Model
|
||||
|
||||
scope "FancyHelper"
|
||||
expire :hour => 24*3600
|
||||
|
||||
end
|
||||
|
||||
@@ -38,6 +38,7 @@ describe Redistat::Model do
|
||||
ModelHelper2.store_event.should == true
|
||||
ModelHelper2.hashed_label.should == true
|
||||
ModelHelper2.scope.should be_nil
|
||||
ModelHelper2.expire.should be_nil
|
||||
|
||||
ModelHelper1.depth.should == nil
|
||||
ModelHelper1.store_event.should == nil
|
||||
@@ -57,6 +58,7 @@ describe Redistat::Model do
|
||||
|
||||
ModelHelper4.scope.should == "FancyHelper"
|
||||
ModelHelper4.send(:name).should == "FancyHelper"
|
||||
ModelHelper4.expire.should == {:hour => 24*3600}
|
||||
end
|
||||
|
||||
it "should store and fetch stats" do
|
||||
|
||||
@@ -10,6 +10,7 @@ describe Redistat::Summary do
|
||||
@date = Time.now
|
||||
@key = Redistat::Key.new(@scope, @label, @date, {:depth => :day})
|
||||
@stats = {"views" => 3, "visitors" => 2}
|
||||
@expire = {:hour => 24*3600}
|
||||
end
|
||||
|
||||
it "should update a single summary properly" do
|
||||
@@ -32,6 +33,20 @@ describe Redistat::Summary do
|
||||
summary["visitors"].should == "1"
|
||||
end
|
||||
|
||||
it "should set key expiry properly" do
|
||||
Redistat::Summary.update_all(@key, @stats, :hour,{:expire => @expire})
|
||||
((24*3600)-1..(24*3600)+1).should include(db.ttl(@key.to_s(:hour)))
|
||||
[:day, :month, :year].each do |depth|
|
||||
db.ttl(@key.to_s(depth)).should == -1
|
||||
end
|
||||
|
||||
db.flushdb
|
||||
Redistat::Summary.update_all(@key, @stats, :hour, {:expire => {}})
|
||||
[:hour, :day, :month, :year].each do |depth|
|
||||
db.ttl(@key.to_s(depth)).should == -1
|
||||
end
|
||||
end
|
||||
|
||||
it "should update all summaries properly" do
|
||||
Redistat::Summary.update_all(@key, @stats, :sec)
|
||||
[:year, :month, :day, :hour, :min, :sec, :usec].each do |depth|
|
||||
|
||||
Reference in New Issue
Block a user