Skip to content

Commit

Permalink
fix caching of source attributes. #306
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed May 13, 2015
1 parent f219749 commit b74ad5b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
48 changes: 24 additions & 24 deletions app/models/concerns/countable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ def event_count
cache_read("event_count", retrieval_statuses.sum(:total))
end

def event_count=
cache_write("event_count", retrieval_statuses.sum(:total))
def event_count=(time)
cache_write("event_count", retrieval_statuses.sum(:total), time)
end

def work_count
cache_read("work_count", works.has_events.size)
end

def work_count=
cache_write("work_count", works.has_events.size)
def work_count=(time)
cache_write("work_count", works.has_events.size, time)
end

def relative_work_count
Expand All @@ -35,40 +35,40 @@ def queued_count
cache_read("queued_count", retrieval_statuses.queued.size)
end

def queued_count=
cache_write("queued_count", retrieval_statuses.queued.size)
def queued_count=(time)
cache_write("queued_count", retrieval_statuses.queued.size, time)
end

def stale_count
cache_read("stale_count", retrieval_statuses.stale.size)
end

def stale_count=
cache_write("stale_count", retrieval_statuses.stale.size)
def stale_count=(time)
cache_write("stale_count", retrieval_statuses.stale.size, time)
end

def response_count
cache_read("response_count", api_responses.total(24).size)
end

def response_count=
cache_write("response_count/", api_responses.total(24).size)
def response_count=(time)
cache_write("response_count/", api_responses.total(24).size, time)
end

def average_count
cache_read("average_count", api_responses.total(1).average("duration").to_i)
end

def average_count=
cache_write("average_count", api_responses.total(1).average("duration"))
def average_count=(time)
cache_write("average_count", api_responses.total(1).average("duration"), time)
end

def maximum_count
cache_read("maximum_count", api_responses.total(1).maximum("duration").to_i)
end

def maximum_count=
cache_write("maximum_count", api_responses.total(1).maximum("duration"))
def maximum_count=(time)
cache_write("maximum_count", api_responses.total(1).maximum("duration"), time)
end

def error_count
Expand All @@ -79,32 +79,32 @@ def with_events_by_day_count
cache_read("with_events_by_day_count", retrieval_statuses.with_events.last_x_days(1).size)
end

def with_events_by_day_count=
cache_write("with_events_by_day_count", retrieval_statuses.with_events.last_x_days(1).size)
def with_events_by_day_count=(time)
cache_write("with_events_by_day_count", retrieval_statuses.with_events.last_x_days(1).size, time)
end

def without_events_by_day_count
cache_read("without_events_by_day_count", retrieval_statuses.without_events.last_x_days(1).size)
end

def without_events_by_day_count=
cache_write("without_events_by_day_count", retrieval_statuses.without_events.last_x_days(1).size)
def without_events_by_day_count=(time)
cache_write("without_events_by_day_count", retrieval_statuses.without_events.last_x_days(1).size, time)
end

def with_events_by_month_count
cache_read("with_events_by_month_count", retrieval_statuses.with_events.last_x_days(31).size)
end

def with_events_by_month_count=
cache_write("with_events_by_month_count", retrieval_statuses.with_events.last_x_days(31).size)
def with_events_by_month_count=(time)
cache_write("with_events_by_month_count", retrieval_statuses.with_events.last_x_days(31).size, time)
end

def without_events_by_month_count
cache_read("without_events_by_month_count", retrieval_statuses.without_events.last_x_days(31).size)
end

def without_events_by_month_count=
cache_write("without_events_by_month_count", retrieval_statuses.without_events.last_x_days(31).size)
def without_events_by_month_count=(time)
cache_write("without_events_by_month_count", retrieval_statuses.without_events.last_x_days(31).size, time)
end

def cache_read(id, value)
Expand All @@ -115,8 +115,8 @@ def cache_read(id, value)
end
end

def cache_write(id, value)
Rails.cache.write("#{name}/#{id}/#{timestamp}", value)
def cache_write(id, value, time)
Rails.cache.write("#{name}/#{id}/#{time}", value)
end
end
end
2 changes: 1 addition & 1 deletion app/models/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def write_cache
:with_events_by_day_count,
:without_events_by_day_count,
:with_events_by_month_count,
:without_events_by_month_count].each { |cached_attr| send("#{cached_attr}=") }
:without_events_by_month_count].each { |cached_attr| send("#{cached_attr}=#{now.utc.iso8601}") }

update_column(:cached_at, now)
end
Expand Down

0 comments on commit b74ad5b

Please sign in to comment.