Sha256: 6935e178ea84bfb010ac9895ca4d406fe84be87aaf5824c8a825a0eb517dbe26

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require 'singleton'

module MeterCat
  class Cache < Hash
    include Singleton

    def initialize
      return if Rails.env.test?
      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)
      meter = delete(name)
      return unless meter
      meter.add_with_retry
    end

    # Flushes all keys

    def flush_all
      keys.each { |key| flush(key) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
meter_cat-5.0.1 app/models/meter_cat/cache.rb
meter_cat-5.0.0 app/models/meter_cat/cache.rb