mirror of
https://github.com/jimeh/redistat.git
synced 2026-02-18 21:06:40 +00:00
Initial work to get key expiry working
This commit is contained in:
@@ -46,6 +46,10 @@ module Redistat
|
||||
|
||||
alias :class_name :scope
|
||||
|
||||
def expire(exp)
|
||||
options[:expire] = exp.is_a?(Hash) ? exp : Hash.new(exp)
|
||||
end
|
||||
|
||||
def connect_to(opts = {})
|
||||
Connection.create(opts.merge(:ref => name))
|
||||
options[:connection_ref] = name
|
||||
|
||||
@@ -5,9 +5,12 @@ module Redistat
|
||||
class << self
|
||||
|
||||
def default_options
|
||||
{ :enable_grouping => true,
|
||||
:label_indexing => true,
|
||||
:connection_ref => nil }
|
||||
{
|
||||
:enable_grouping => true,
|
||||
:label_indexing => true,
|
||||
:connection_ref => nil,
|
||||
:expire => {}
|
||||
}
|
||||
end
|
||||
|
||||
def buffer
|
||||
@@ -33,26 +36,30 @@ module Redistat
|
||||
if opts[:enable_grouping]
|
||||
stats = inject_group_summaries(stats)
|
||||
key.groups.each do |k|
|
||||
update_key(k, stats, depth_limit, opts[:connection_ref])
|
||||
update_key(k, stats, depth_limit, opts)
|
||||
k.update_index if opts[:label_indexing]
|
||||
end
|
||||
else
|
||||
update_key(key, stats, depth_limit, opts[:connection_ref])
|
||||
update_key(key, stats, depth_limit, opts)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_key(key, stats, depth_limit, connection_ref)
|
||||
def update_key(key, stats, depth_limit, opts)
|
||||
Date::DEPTHS.each do |depth|
|
||||
update_fields(key, stats, depth, connection_ref)
|
||||
update_fields(key, stats, depth, opts)
|
||||
break if depth == depth_limit
|
||||
end
|
||||
end
|
||||
|
||||
def update_fields(key, stats, depth, connection_ref = nil)
|
||||
def update_fields(key, stats, depth, opts)
|
||||
stats.each do |field, value|
|
||||
db(connection_ref).hincrby key.to_s(depth), field, value
|
||||
db(opts[:connection_ref]).hincrby key.to_s(depth), field, value
|
||||
end
|
||||
|
||||
if opts[:expire] && !opts[:expire][depth].nil?
|
||||
db(opts[:connection_ref]).expire key.to_s(depth), opts[:expire][depth]
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user