Skip to content

Commit

Permalink
unit test for cache_meta_info
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiningRay committed Apr 13, 2013
1 parent 06143af commit c2dde7e
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 33 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -4,3 +4,5 @@ rvm:
- ree
- 1.9.2
- 1.9.3
services:
- memcached
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -5,4 +5,5 @@ source 'http://rubygems.org'
gemspec
gem 'rake'
gem 'rails', '~> 3.1'
gem 'memcache-client'
#gem 'mocha', :require => false
5 changes: 3 additions & 2 deletions lib/super_cache.rb
@@ -1,13 +1,14 @@
require "super_cache/version"
require 'uri'
require 'fileutils'
# for static-caching the generated html pages
require 'super_cache/mem_cache_store_patch'

# for static-caching the generated html pages
module SuperCache
autoload :Lock, 'super_cache/lock'
autoload :DogPileFilter, 'super_cache/dog_pile_filter'
autoload :SimpleFilter, 'super_cache/simple_filter'

autoload :CacheMetaInfo, 'super_cache/cache_meta_info'
def self.included(base)
base.class_attribute :cache_filter
base.extend(ClassMethods)
Expand Down
6 changes: 3 additions & 3 deletions lib/super_cache/cache_meta_info.rb
Expand Up @@ -2,15 +2,15 @@
# and open the template in the editor.
module SuperCache
module CacheMetaInfo
autoload RedisStore
autoload MemCacheStore
autoload :RedisStore, 'super_cache/cache_meta_info/redis_store'
autoload :MemCacheStore, 'super_cache/cache_meta_info/mem_cache_store'

def key_for_cached_keys
"#{self.class.name}:#{self.id}:cached_keys"
end

def clear_related_caches
Rails.cache.debug{cached_keys.to_a.join(' ')}
Rails.logger.debug{cached_keys.to_a.join(' ')}
cached_keys.each do |key|
Rails.cache.delete key
end
Expand Down
20 changes: 11 additions & 9 deletions lib/super_cache/cache_meta_info/mem_cache_store.rb
@@ -1,12 +1,14 @@
module CacheMetaInfo::MemCacheStore
def append_cached_key(key)
cached_keys
@need_append ||= !Rails.cache.write(key_for_cached_keys, key,
:raw => true,
:unless_exist => true )
if @need_append and not cached_keys.include?(key)
cached_keys << key
Rails.cache.append(key_for_cached_keys, ",#{key}")
module SuperCache
module CacheMetaInfo
module MemCacheStore
def append_cached_key(key)
cached_keys
@need_append ||= !Rails.cache.write(key_for_cached_keys, key,
:raw => true,
:unless_exist => true )
Rails.cache.append(key_for_cached_keys, ",#{key}") if @need_append and not cached_keys.include?(key)
cached_keys << key
end
end
end
end
40 changes: 22 additions & 18 deletions lib/super_cache/cache_meta_info/redis_store.rb
@@ -1,22 +1,26 @@
module CacheMetaInfo::RedisStore
def append_cached_key(key)
cached_keys << key
end
def clear_related_caches
cached_keys.each do |key|
Rails.cache.delete key
cached_keys.delete key
end
end

def remove_cached_key(key)
cached_keys.delete key
end
module SuperCache
module CacheMetaInfo
module RedisStore
def append_cached_key(key)
cached_keys << key
end
def clear_related_caches
cached_keys.each do |key|
Rails.cache.delete key
cached_keys.delete key
end
end

def remove_cached_key(key)
cached_keys.delete key
end

def self.included(base)
base.class_eval do
include ::Redis::Objects unless included_modules.include?(::Redis::Objects)
set :cached_keys
def self.included(base)
base.class_eval do
include ::Redis::Objects unless included_modules.include?(::Redis::Objects)
set :cached_keys
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/super_cache/simple_filter.rb
Expand Up @@ -65,7 +65,7 @@ def append_cache_key_to_subject(*keys)
subjects = Array.wrap(controller.instance_exec(controller, &options[:subject])).flatten.select{|s|s.respond_to?(:append_cached_key)}
subjects.each do |s|
keys.each do |k|
s.append_cache_key k
s.append_cached_key k
end
end
end
Expand Down
32 changes: 32 additions & 0 deletions test/cache_meta_info_test.rb
@@ -0,0 +1,32 @@
require File.expand_path('test_helper', File.dirname(__FILE__))
require File.expand_path('my_controller', File.dirname(__FILE__))
Rails.cache = ActiveSupport::Cache::MemCacheStore.new
class Model
include SuperCache::CacheMetaInfo
include SuperCache::CacheMetaInfo::MemCacheStore
attr_accessor :id
def initialize(id)
@id = id
end
end
class MyController
super_caches_page :index, :subject => Proc.new { @model }
def index
$model = @model = Model.new(10)
render :text => @model.id
end
end
class CacheMetaIntoTest < ActionController::TestCase
tests MyController
test "should get index" do
get :index
assert_response :success
assert_equal '10', @response.body.strip
assert_equal '10', Rails.cache.read('test.host/my', :raw => true)
assert_equal $model.key_for_cached_keys, 'Model:10:cached_keys'
assert_equal $model.cached_keys.size, 1
assert_equal $model.cached_keys[0], 'test.host/my'
$model.clear_related_caches
assert_nil Rails.cache.read('test.host/my', :raw => true)
end
end

0 comments on commit c2dde7e

Please sign in to comment.