Sha256: 267e75e7e02ca05de34a0fff50564409cf565ebcf6a920aae064af45bb9bce22

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module Lawnchair
  module StorageEngine
    class Abstract
      class << self
        attr_reader :data_store
        
        def data_store
          @data_store ||= {}
        end
      
        def fetch(key, options={}, &block)
          start_time = Time.now
          if exists?(key)
            value = get(key, options)
            log("HIT", key, Time.now-start_time)
            return value
          else
            value = block.call
            set(key, value, options)
            log("MISS", key, Time.now-start_time)
            return value
          end
        end
      
        def get(key, options={})
          if options[:raw]
            data_store[computed_key(key)]
          else
            exists?(key) ? Marshal.load(data_store[computed_key(key)]) : nil
          end
        end
      
        def computed_key(key)
          raise "Missing key" if key.nil? || key.empty?
          key.gsub!(" ","") if key.match(/\s/)
          prefix = "Lawnchair"
          "#{prefix}:#{key}"
        end
        
        def db_connection?
          true
        end
        
        def log(message, key, elapsed)
          ActionController::Base.logger.info("Lawnchair Cache: #{message} (%0.6f secs): #{key}" % elapsed) if defined? ::ActionController::Base
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lawnchair-0.6.6 lib/storage_engine/abstract.rb