Skip to content

Commit

Permalink
Invalidate the key_versions_cache_store when a new cache store is used.
Browse files Browse the repository at this point in the history
This is because were failing if you ran them with the cachy_spec first. This 
is because the versions cache was not wiped be and the and the memory cache was not 
cleared and was calling a cached call to read_versions. This code fixes that and adds a 
test for it also.
  • Loading branch information
philly-mac committed Mar 29, 2012
1 parent b0b1402 commit 6176cfb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion lib/cachy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.cache(*args)
cache_store.write key, result, options
result
end

def self.cache_if(cond, *args, &block)
if cond
cache(*args, &block)
Expand Down Expand Up @@ -118,6 +118,8 @@ class << self
def self.cache_store=(cache)
@cache_store = wrap_cache(cache)
@cache_store.write HEALTH_CHECK_KEY, 'yes'
@key_versions_cache_store = nil
memory_store.clear
end

def self.cache_store
Expand Down
14 changes: 11 additions & 3 deletions spec/cachy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@
Cachy.cache(:x){ true }.should == true
end
end

describe :cache_if do
it "should not call the cache command if condition is wrong" do
Cachy.should_not_receive(:cache)
Cachy.cache_if(false, :x) do
"asd"
end
end

it "should call cache command if condition is true" do
Cachy.should_receive(:cache)
Cachy.cache_if(true, :x) do
"asd"
end
end

it "should pass params correctly" do
Cachy.should_receive(:cache).with(:x, {:y => 1}, :expires_in => 3)
Cachy.cache_if(true, :x, {:y => 1}, :expires_in => 3) do
Expand Down Expand Up @@ -357,6 +357,14 @@ def @mock.[](x)
Cachy.increment_key :x
Cachy.key_versions.should == {:x => 2}
end

it "invalidates the cached key_versions_cache_store when a new cache store is used" do
key_cache.write(Cachy::KEY_VERSIONS_KEY, :x => 1)
Cachy.key_versions_cache_store = key_cache
Cachy.key_versions.should == {:x => 1}
Cachy.cache_store = @cache
Cachy.key_versions.should == {}
end
end

describe :delete_key do
Expand Down

0 comments on commit 6176cfb

Please sign in to comment.