Sha256: bbe548ca3b92074c1838f421b75f2f61ef2660bc2a069afd33a0f22c684bbc7e

Contents?: true

Size: 945 Bytes

Versions: 9

Compression:

Stored size: 945 Bytes

Contents

module MontageRails
  class QueryCache
    attr_reader :cache

    def initialize
      @cache = {}
    end

    def get_or_set_query(klass, query)
      cached = cache.keys.include?("#{klass}/#{query}")
      ActiveSupport::Notifications.instrument("reql.montage_rails", notification_payload(query, klass, cached)) do
        if cached
          cache["#{klass}/#{query}"]
        else
          response = yield
          cache["#{klass}/#{query}"] = response
          response
        end
      end
    end

    # Clear the entire query cache
    #
    def clear
      @cache = {}
    end

    # Remove a certain key from the cache
    # Returns the removed value, or nil if nothin was found
    #
    def remove(key)
      cache.delete(key)
    end

  private

    def notification_payload(query, klass, cached = false)
      {
        reql: query,
        name: cached ? "#{klass} Load [CACHE]" : "#{klass} Load"
      }
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
montage_rails-0.4.9 lib/montage_rails/query_cache.rb
montage_rails-0.4.8 lib/montage_rails/query_cache.rb
montage_rails-0.4.7 lib/montage_rails/query_cache.rb
montage_rails-0.4.6 lib/montage_rails/query_cache.rb
montage_rails-0.4.5 lib/montage_rails/query_cache.rb
montage_rails-0.4.4 lib/montage_rails/query_cache.rb
montage_rails-0.4.3 lib/montage_rails/query_cache.rb
montage_rails-0.4.2 lib/montage_rails/query_cache.rb
montage_rails-0.4.1 lib/montage_rails/query_cache.rb