提交 756d7762 编写于 作者: H Hongli Lai (Phusion) 提交者: José Valim

Allow instrumentation of cache hits and misses. [#4888 state:resolved]

Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 f8166669
......@@ -267,10 +267,16 @@ def self.instrument
# :bar
# end
# cache.fetch("foo") # => "bar"
def fetch(name, options = nil, &block)
def fetch(name, options = nil)
if block_given?
options = merged_options(options)
key = namespaced_key(name, options)
entry = instrument(:read, name, options) { read_entry(key, options) } unless options[:force]
unless options[:force]
entry = instrument(:read, name, options) do |payload|
payload[:super_operation] = :fetch if payload
read_entry(key, options)
end
end
if entry && entry.expired?
race_ttl = options[:race_condition_ttl].to_f
if race_ttl and Time.now.to_f - entry.expires_at <= race_ttl
......@@ -283,12 +289,18 @@ def fetch(name, options = nil, &block)
end
if entry
instrument(:fetch_hit, name, options) { |payload| }
entry.value
elsif block_given?
result = instrument(:generate, name, options, &block)
else
result = instrument(:generate, name, options) do |payload|
yield
end
write(name, result, options)
result
end
else
read(name, options)
end
end
# Fetches data from the cache, using the given key. If there is data in
......@@ -299,16 +311,19 @@ def fetch(name, options = nil, &block)
def read(name, options = nil)
options = merged_options(options)
key = namespaced_key(name, options)
instrument(:read, name, options) do
instrument(:read, name, options) do |payload|
entry = read_entry(key, options)
if entry
if entry.expired?
delete_entry(key, options)
payload[:hit] = false if payload
nil
else
payload[:hit] = true if payload
entry.value
end
else
payload[:hit] = false if payload
nil
end
end
......@@ -345,7 +360,7 @@ def read_multi(*names)
# +options+.
def write(name, value, options = nil)
options = merged_options(options)
instrument(:write, name, options) do
instrument(:write, name, options) do |payload|
entry = Entry.new(value, options)
write_entry(namespaced_key(name, options), entry, options)
end
......@@ -356,7 +371,7 @@ def write(name, value, options = nil)
# Options are passed to the underlying cache implementation.
def delete(name, options = nil)
options = merged_options(options)
instrument(:delete, name) do
instrument(:delete, name) do |payload|
delete_entry(namespaced_key(name, options), options)
end
end
......@@ -366,7 +381,7 @@ def delete(name, options = nil)
# Options are passed to the underlying cache implementation.
def exist?(name, options = nil)
options = merged_options(options)
instrument(:exist?, name) do
instrument(:exist?, name) do |payload|
entry = read_entry(namespaced_key(name, options), options)
if entry && !entry.expired?
true
......@@ -502,9 +517,9 @@ def instrument(operation, key, options = nil)
if self.class.instrument
payload = { :key => key }
payload.merge!(options) if options.is_a?(Hash)
ActiveSupport::Notifications.instrument("cache_#{operation}.active_support", payload){ yield }
ActiveSupport::Notifications.instrument("cache_#{operation}.active_support", payload){ yield(payload) }
else
yield
yield(nil)
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册