Sha256: e66092dfe6c8037c0ffd46672161a4af16ff1a40fd42d3e44ab5dffdafac0136
Contents?: true
Size: 1.25 KB
Versions: 1
Compression:
Stored size: 1.25 KB
Contents
require 'singleton' module MeterCat class Cache < Hash include Singleton def initialize at_exit { MeterCat::Cache.instance.flush_all } end # Adds the given value to the hash # Flushes expired data to DB def add( name, value, created_on ) meter = fetch( name, nil ) # If the name isn't cached, cache it and return return cache( name, value, created_on ) unless meter # If the cached value is for a different day, flush it, cache the new value and return if meter.created_on != created_on flush( name ) cache( name, value, created_on ) return end # Add the new value to the cached value and flush if expired meter.value += value flush( name ) if meter.expired? end # Creates a new Meter and stores is in the hash def cache( name, value, created_on ) meter = Meter.new( :name => name, :value => value, :created_on => created_on, :created_at => Time.now ) store( name, meter ) end # Flushes data to the DB and removes it from the hash def flush( name ) return unless meter = delete( name ) meter.add_with_retry end # Flushes all keys def flush_all keys.each { |key| flush( key ) } end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
meter_cat-0.0.6 | app/models/meter_cat/cache.rb |