Skip to content

Commit

Permalink
Add fragment_exist? and exist? methods to cache stores. [#203 state:r…
Browse files Browse the repository at this point in the history
…esolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
josevalim authored and lifo committed May 19, 2008
1 parent 17d1319 commit 99860b7
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 26 deletions.
13 changes: 12 additions & 1 deletion actionpack/lib/action_controller/caching/fragments.rb
Expand Up @@ -98,6 +98,17 @@ def read_fragment(key, options = nil)
end
end

# Check if a cached fragment from the location signified by <tt>key</tt> exists (see <tt>expire_fragment</tt> for acceptable formats)
def fragment_exist?(key, options = nil)
return unless cache_configured?

key = fragment_cache_key(key)

self.class.benchmark "Cached fragment exists?: #{key}" do
cache_store.exist?(key, options)
end
end

# Name can take one of three forms:
# * String: This would normally take the form of a path like "pages/45/notes"
# * Hash: Is treated as an implicit call to url_for, like { :controller => "pages", :action => "notes", :id => 45 }
Expand All @@ -124,4 +135,4 @@ def expire_fragment(key, options = nil)
end
end
end
end
end
42 changes: 22 additions & 20 deletions actionpack/test/controller/caching_test.rb
Expand Up @@ -232,7 +232,7 @@ def test_simple_action_cache
get :index
cached_time = content_to_cache
assert_equal cached_time, @response.body
assert_cache_exists 'hostname.com/action_caching_test'
assert fragment_exist?('hostname.com/action_caching_test')
reset!

get :index
Expand All @@ -243,7 +243,7 @@ def test_simple_action_not_cached
get :destroy
cached_time = content_to_cache
assert_equal cached_time, @response.body
assert_cache_does_not_exist 'hostname.com/action_caching_test/destroy'
assert !fragment_exist?('hostname.com/action_caching_test/destroy')
reset!

get :destroy
Expand All @@ -254,7 +254,7 @@ def test_action_cache_with_layout
get :with_layout
cached_time = content_to_cache
assert_not_equal cached_time, @response.body
assert_cache_exists 'hostname.com/action_caching_test/with_layout'
assert fragment_exist?('hostname.com/action_caching_test/with_layout')
reset!

get :with_layout
Expand All @@ -266,14 +266,14 @@ def test_action_cache_with_layout
def test_action_cache_conditional_options
@request.env['HTTP_ACCEPT'] = 'application/json'
get :index
assert_cache_does_not_exist 'hostname.com/action_caching_test'
assert !fragment_exist?('hostname.com/action_caching_test')
end

def test_action_cache_with_custom_cache_path
get :show
cached_time = content_to_cache
assert_equal cached_time, @response.body
assert_cache_exists 'test.host/custom/show'
assert fragment_exist?('test.host/custom/show')
reset!

get :show
Expand All @@ -282,11 +282,11 @@ def test_action_cache_with_custom_cache_path

def test_action_cache_with_custom_cache_path_in_block
get :edit
assert_cache_exists 'test.host/edit'
assert fragment_exist?('test.host/edit')
reset!

get :edit, :id => 1
assert_cache_exists 'test.host/1;edit'
assert fragment_exist?('test.host/1;edit')
end

def test_cache_expiration
Expand Down Expand Up @@ -395,18 +395,8 @@ def reset!
@request.host = 'hostname.com'
end

def assert_cache_exists(path)
full_path = cache_path(path)
assert File.exist?(full_path), "#{full_path.inspect} does not exist."
end

def assert_cache_does_not_exist(path)
full_path = cache_path(path)
assert !File.exist?(full_path), "#{full_path.inspect} should not exist."
end

def cache_path(path)
File.join(FILE_STORE_PATH, 'views', path + '.cache')
def fragment_exist?(path)
@controller.fragment_exist?(path)
end

def read_fragment(path)
Expand Down Expand Up @@ -450,6 +440,19 @@ def test_read_fragment__with_caching_disabled
assert_nil @controller.read_fragment('name')
end

def test_fragment_exist__with_caching_enabled
@store.write('views/name', 'value')
assert @controller.fragment_exist?('name')
assert !@controller.fragment_exist?('other_name')
end

def test_fragment_exist__with_caching_disabled
ActionController::Base.perform_caching = false
@store.write('views/name', 'value')
assert !@controller.fragment_exist?('name')
assert !@controller.fragment_exist?('other_name')
end

def test_write_fragment__with_caching_enabled
assert_nil @store.read('views/name')
assert_equal 'value', @controller.write_fragment('name', 'value')
Expand Down Expand Up @@ -494,7 +497,6 @@ def test_fragment_for__with_disabled_caching
assert_equal 'generated till now -> ', buffer
end


def test_fragment_for
@store.write('views/expensive', 'fragment content')
fragment_computed = false
Expand Down
8 changes: 6 additions & 2 deletions activesupport/lib/active_support/cache.rb
Expand Up @@ -87,8 +87,12 @@ def delete(key, options = nil)

def delete_matched(matcher, options = nil)
log("delete matched", matcher.inspect, options)
end

end

def exist?(key, options = nil)
log("exist?", key, options)
end

def increment(key, amount = 1)
log("incrementing", key, amount)
if num = read(key)
Expand Down
7 changes: 6 additions & 1 deletion activesupport/lib/active_support/cache/file_store.rb
Expand Up @@ -40,13 +40,18 @@ def delete_matched(matcher, options = nil)
end
end

def exist?(name, options = nil)
super
File.exist?(real_file_path(name))
end

private
def real_file_path(name)
'%s/%s.cache' % [@cache_path, name.gsub('?', '.').gsub(':', '.')]
end

def ensure_cache_path(path)
FileUtils.makedirs(path) unless File.exists?(path)
FileUtils.makedirs(path) unless File.exist?(path)
end

def search_dir(dir, &callback)
Expand Down
8 changes: 7 additions & 1 deletion activesupport/lib/active_support/cache/mem_cache_store.rb
Expand Up @@ -48,7 +48,13 @@ def delete(key, options = nil)
rescue MemCache::MemCacheError => e
logger.error("MemCacheError (#{e}): #{e.message}")
false
end
end

def exist?(key, options = nil)
# Doesn't call super, cause exist? in memcache is in fact a read
# But who cares? Reading is very fast anyway
!read(key, options).nil?
end

def increment(key, amount = 1)
log("incrementing", key, amount)
Expand Down
7 changes: 6 additions & 1 deletion activesupport/lib/active_support/cache/memory_store.rb
Expand Up @@ -24,7 +24,12 @@ def delete_matched(matcher, options = nil)
super
@data.delete_if { |k,v| k =~ matcher }
end


def exist?(name,options = nil)
super
@data.has_key?(name)
end

def clear
@data.clear
end
Expand Down

0 comments on commit 99860b7

Please sign in to comment.