Skip to content

Commit

Permalink
Merge pull request #1326 from Ortuna/block-cache-key
Browse files Browse the repository at this point in the history
ability for cache_key to be a block
  • Loading branch information
DAddYE committed Jul 1, 2013
2 parents d7ebd4d + aa37ef8 commit 8444f39
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
37 changes: 28 additions & 9 deletions padrino-cache/lib/padrino-cache/helpers/page.rb
Expand Up @@ -70,6 +70,8 @@ def expires_in(time)
#
# @param [Symbol] name
# cache key
# @param [Proc] block
# block to be evaluated to cache key
#
# @example
# controller '/blog', :cache => true do
Expand All @@ -80,18 +82,27 @@ def expires_in(time)
# end
# end
#
# @example
# get '/foo', :cache => true do
# cache_key { param[:id] }
# "my id is #{param[:id}"
# end
# end
#
# @api public
def cache_key(name)
@route.cache_key = name
def cache_key(name = nil, &block)
raise "Can not provide both cache_key and a block" if name && block
@route.cache_key = block_given? ? block : name
end

# @private
def self.padrino_route_added(route, verb, path, args, options, block) # @private
if route.cache and %w(GET HEAD).include?(verb)
route.before_filters do
if settings.caching?
began_at = Time.now
value = settings.cache.get(@route.cache_key || env['PATH_INFO'])
began_at = Time.now

value = settings.cache.get(resolve_cache_key || env['PATH_INFO'])
logger.debug "GET Cache", began_at, @route.cache_key || env['PATH_INFO'] if defined?(logger) && value

if value
Expand All @@ -103,22 +114,30 @@ def self.padrino_route_added(route, verb, path, args, options, block) # @private

route.after_filters do
if settings.caching? && @_response_buffer.kind_of?(String)
began_at = Time.now

content = @_response_buffer
began_at = Time.now
content = @_response_buffer

if @_last_expires_in
settings.cache.set(@route.cache_key || env['PATH_INFO'], content, :expires_in => @_last_expires_in)
settings.cache.set(resolve_cache_key || env['PATH_INFO'], content, :expires_in => @_last_expires_in)
@_last_expires_in = nil
else
settings.cache.set(@route.cache_key || env['PATH_INFO'], content)
settings.cache.set(resolve_cache_key || env['PATH_INFO'], content)
end

logger.debug "SET Cache", began_at, @route.cache_key || env['PATH_INFO'] if defined?(logger)
end
end
end
end

private
##
# Resolve the cache_key when it's a block in the correct context
#@api private
def resolve_cache_key
@route.cache_key.is_a?(Proc) ? instance_eval(&@route.cache_key) : @route.cache_key
end

end # Page
end # Helpers
end # Cache
Expand Down
36 changes: 36 additions & 0 deletions padrino-cache/test/test_padrino_cache.rb
Expand Up @@ -270,4 +270,40 @@
assert_equal 'foo?yes', body
assert_equal 2, call_count
end

should 'resolve block cache keys' do
call_count = 0
mock_app do
register Padrino::Cache
enable :caching

get '/foo', :cache => true do
cache_key { "key #{params[:id]}" }
call_count += 1
params[:id]
end
end

get '/foo?id=1'
get '/foo?id=2'
get '/foo?id=2'
get '/foo?id=1&something_else=42'
get '/foo?id=3&something_else=42'

assert_equal 3, call_count
end

should 'raise an error if providing both a cache_key and block' do
mock_app do
register Padrino::Cache
enable :caching

get '/foo', :cache => true do
cache_key(:some_key) { "key #{params[:id]}" }
end
end

assert_raises(RuntimeError) { get '/foo' }
end

end

0 comments on commit 8444f39

Please sign in to comment.