Sha256: e054bfdffc927cc9e872f8a1be496685a92eaa9d1252ee8506dabb6d2ba896da

Contents?: true

Size: 1.01 KB

Versions: 14

Compression:

Stored size: 1.01 KB

Contents

module MontageRails
  class QueryCache
    attr_reader :cache
    attr_accessor :no_cache

    def initialize(no_cache = false)
      @cache = {}
      @no_cache = no_cache
    end

    def get_or_set_query(klass, query)
      cached = cache.keys.include?("#{klass}/#{query}") && !no_cache
      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

14 entries across 14 versions & 1 rubygems

Version Path
montage_rails-1.0.0 lib/montage_rails/query_cache.rb
montage_rails-0.7.4 lib/montage_rails/query_cache.rb
montage_rails-0.7.3 lib/montage_rails/query_cache.rb
montage_rails-0.7.2 lib/montage_rails/query_cache.rb
montage_rails-0.7.1 lib/montage_rails/query_cache.rb
montage_rails-0.7 lib/montage_rails/query_cache.rb
montage_rails-0.6.4 lib/montage_rails/query_cache.rb
montage_rails-0.6.3 lib/montage_rails/query_cache.rb
montage_rails-0.6.2 lib/montage_rails/query_cache.rb
montage_rails-0.6.1 lib/montage_rails/query_cache.rb
montage_rails-0.6.0 lib/montage_rails/query_cache.rb
montage_rails-0.5.0 lib/montage_rails/query_cache.rb
montage_rails-0.4.11 lib/montage_rails/query_cache.rb
montage_rails-0.4.10 lib/montage_rails/query_cache.rb