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