Sha256: 8dbce5c24bf75a1068dcc9898ece6ea765a06338759b5b3c6ef2f6d0adda5c74

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require 'singleton'

module MeterCat
  class Cache < Hash
    include Singleton

    def initialize
      unless Rails.env.test?
        at_exit { MeterCat::Cache.instance.flush_all }
      end
    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.7 app/models/meter_cat/cache.rb