Sha256: 299c68d3a1a43f05ec6737d7fe2d7e0df82148dd705644e74a37424be8d9647b

Contents?: true

Size: 920 Bytes

Versions: 34

Compression:

Stored size: 920 Bytes

Contents

module Mongocore

  # # # # # # # #
  # The Cache class keeps track of cache entries.
  #
  # Every query is cached, used the state as the cache key. This is a
  # very aggressive strategy, where arrays won't get updated on update or delete.
  #

  class Cache

    # Accessors
    attr_accessor :query, :cache, :key, :type

    # Init
    def initialize(q)
      @query = q
      @cache = (RequestStore[:cache] ||= {})
      @key = Digest::MD5.hexdigest(@query.key)
    end

    # Get the cache key
    def get(t)
      @cache[t = key + t.to_s].tap{|d| stat(d, t) if Mongocore.debug}
    end

    # Set the cache key
    def set(t, v = nil)
      t = key + t.to_s; v ? cache[t] = v : cache.delete(t)
    end

    private

    # Stats for debug and cache
    def stat(d, t)
      puts('Cache ' + (d ? 'Hit!' : 'Miss') + ': ' + t)
      RequestStore[d ? :h : :m] = (RequestStore[d ? :h : :m] || 0) + 1
    end

  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
mongocore-0.5.1 lib/mongocore/cache.rb
mongocore-0.5.0 lib/mongocore/cache.rb
mongocore-0.4.5 lib/mongocore/cache.rb
mongocore-0.4.4 lib/mongocore/cache.rb
mongocore-0.4.3 lib/mongocore/cache.rb
mongocore-0.4.2 lib/mongocore/cache.rb
mongocore-0.4.1 lib/mongocore/cache.rb
mongocore-0.4.0 lib/mongocore/cache.rb
mongocore-0.3.2 lib/mongocore/cache.rb
mongocore-0.3.1.1 lib/mongocore/cache.rb
mongocore-0.3.1 lib/mongocore/cache.rb
mongocore-0.3.0 lib/mongocore/cache.rb
mongocore-0.2.5 lib/mongocore/cache.rb
mongocore-0.2.3 lib/mongocore/cache.rb
mongocore-0.2.2 lib/mongocore/cache.rb
mongocore-0.2.1 lib/mongocore/cache.rb
mongocore-0.2.0 lib/mongocore/cache.rb
mongocore-0.1.10 lib/mongocore/cache.rb
mongocore-0.1.9 lib/mongocore/cache.rb
mongocore-0.1.8 lib/mongocore/cache.rb