Skip to content

Delete key #5

Merged
merged 4 commits into from Jan 13, 2012
View
2 api_cache.gemspec
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
- s.add_development_dependency('rspec', "~> 1.0")
+ s.add_development_dependency('rspec', "~> 2.7")
s.add_development_dependency('webmock')
s.add_development_dependency('rake')
s.add_development_dependency('moneta', "~> 0.6.0")
View
7 lib/api_cache.rb
@@ -128,6 +128,13 @@ def self.get(key, options = {}, &block)
end
end
end
+
+ # Manually delete data from the cache.
+ #
+ def self.delete(key)
+ APICache::Cache.new(key, {}).delete
+ end
+
end
require 'api_cache/cache'
View
5 lib/api_cache/abstract_store.rb
@@ -14,6 +14,11 @@ def get(key)
raise "Method not implemented. Called abstract class."
end
+ # Delete value.
+ def delete(key)
+ raise "Method not implemented. Called abstract class."
+ end
+
# Does a given key exist in the cache?
def exists?(key)
raise "Method not implemented. Called abstract class."
View
4 lib/api_cache/cache.rb
@@ -47,6 +47,10 @@ def set(value)
true
end
+ def delete
+ store.delete(hash)
+ end
+
private
def hash
View
5 lib/api_cache/dalli_store.rb
@@ -16,6 +16,11 @@ def get(key)
@dalli.get(key)
end
+ # Delete value.
+ def delete(key)
+ @dalli.delete(key)
+ end
+
# Does a given key exist in the cache?
def exists?(key)
!get(key).nil?
View
4 lib/api_cache/memory_store.rb
@@ -18,6 +18,10 @@ def get(key)
data
end
+ def delete(key)
+ @cache.delete(key)
+ end
+
def exists?(key)
!@cache[key].nil?
end
View
5 lib/api_cache/moneta_store.rb
@@ -16,6 +16,11 @@ def get(key)
@moneta[key]
end
+ # Delete value.
+ def delete(key)
+ @moneta.delete(key)
+ end
+
# Does a given key exist in the cache?
def exists?(key)
@moneta.key?(key)
View
3 lib/api_cache/null_store.rb
@@ -13,6 +13,9 @@ def set(key, value)
true
end
+ def delete(key)
+ end
+
def expired?(key, timeout)
true
end
View
144 spec/api_cache_spec.rb
@@ -40,62 +40,96 @@
end
describe "get method" do
- before :each do
- @api = mock(APICache::API, :get => @api_data)
- @cache = mock(APICache::Cache, :get => @cache_data, :set => true)
-
- APICache::API.stub!(:new).and_return(@api)
- APICache::Cache.stub!(:new).and_return(@cache)
- end
-
- it "should fetch data from the cache if the state is :current" do
- @cache.stub!(:state).and_return(:current)
-
- APICache.get(@key).should == @cache_data
- end
-
- it "should make new request to API if the state is :refetch and store result in cache" do
- @cache.stub!(:state).and_return(:refetch)
- @cache.should_receive(:set).with(@api_data)
-
- APICache.get(@key).should == @api_data
- end
-
- it "should return the cached value if the state is :refetch but the api is not accessible" do
- @cache.stub!(:state).and_return(:refetch)
- @api.should_receive(:get).with.and_raise(APICache::CannotFetch)
-
- APICache.get(@key).should == @cache_data
- end
-
- it "should make new request to API if the state is :invalid" do
- @cache.stub!(:state).and_return(:invalid)
-
- APICache.get(@key).should == @api_data
- end
-
- it "should raise CannotFetch if the api cannot fetch data and the cache state is :invalid" do
- @cache.stub!(:state).and_return(:invalid)
- @api.should_receive(:get).with.and_raise(APICache::CannotFetch)
-
- lambda {
- APICache.get(@key).should
- }.should raise_error(APICache::CannotFetch)
- end
-
- it "should make new request to API if the state is :missing" do
- @cache.stub!(:state).and_return(:missing)
-
- APICache.get(@key).should == @api_data
+
+ context "when cache is mocked" do
+ before :each do
+ @api = mock(APICache::API, :get => @api_data)
+ @cache = mock(APICache::Cache, :get => @cache_data, :set => true)
+
+ APICache::API.stub!(:new).and_return(@api)
+ APICache::Cache.stub!(:new).and_return(@cache)
+ end
+
+ it "should fetch data from the cache if the state is :current" do
+ @cache.stub!(:state).and_return(:current)
+
+ APICache.get(@key).should == @cache_data
+ end
+
+ it "should make new request to API if the state is :refetch and store result in cache" do
+ @cache.stub!(:state).and_return(:refetch)
+ @cache.should_receive(:set).with(@api_data)
+
+ APICache.get(@key).should == @api_data
+ end
+
+ it "should return the cached value if the state is :refetch but the api is not accessible" do
+ @cache.stub!(:state).and_return(:refetch)
+ @api.should_receive(:get).with.and_raise(APICache::CannotFetch)
+
+ APICache.get(@key).should == @cache_data
+ end
+
+ it "should make new request to API if the state is :invalid" do
+ @cache.stub!(:state).and_return(:invalid)
+
+ APICache.get(@key).should == @api_data
+ end
+
+ it "should raise CannotFetch if the api cannot fetch data and the cache state is :invalid" do
+ @cache.stub!(:state).and_return(:invalid)
+ @api.should_receive(:get).with.and_raise(APICache::CannotFetch)
+
+ lambda {
+ APICache.get(@key).should
+ }.should raise_error(APICache::CannotFetch)
+ end
+
+ it "should make new request to API if the state is :missing" do
+ @cache.stub!(:state).and_return(:missing)
+
+ APICache.get(@key).should == @api_data
+ end
+
+ it "should raise an exception if the api cannot fetch data and the cache state is :missing" do
+ @cache.stub!(:state).and_return(:missing)
+ @api.should_receive(:get).with.and_raise(APICache::CannotFetch)
+
+ lambda {
+ APICache.get(@key).should
+ }.should raise_error(APICache::CannotFetch)
+ end
end
-
- it "should raise an exception if the api cannot fetch data and the cache state is :missing" do
- @cache.stub!(:state).and_return(:missing)
- @api.should_receive(:get).with.and_raise(APICache::CannotFetch)
-
- lambda {
- APICache.get(@key).should
- }.should raise_error(APICache::CannotFetch)
+
+
+ context "when cache is not mocked" do
+
+ before :each do
+ APICache.store = APICache::MemoryStore.new
+ @api = mock(APICache::API, :get => @api_data)
+ APICache::API.stub!(:new).and_return(@api)
+ end
+
+ #it "should initially fetch" do
+ # @api.should_receive(:get)
+ # APICache.get(@key)
+ #end
+
+ it "should only fetch once" do
+ @api.should_receive(:get).once
+ APICache.get(@key)
+ APICache.get(@key)
+ end
+
+ it "should refetch if deleted" do
+ @api.should_receive(:get).twice
+ APICache.get(@key)
+ APICache.delete(@key)
+ APICache.get(@key)
+ end
+
end
+
end
+
end
View
6 spec/cache_spec.rb
@@ -33,4 +33,10 @@
sleep 1
cache.state.should == :invalid
end
+
+ it "should initially have invalid state" do
+ cache = APICache::Cache.new('foo', @options)
+ cache.state.should == :invalid
+ end
+
end
View
11 spec/dalli_store_spec.rb
@@ -27,4 +27,15 @@
sleep 1
@store.expired?('foo', 1).should be_true
end
+
+ context "after delete" do
+
+ it "should no longer exist" do
+ @store.set("key", "value")
+ @store.delete("key")
+ @store.exists?("key").should be_false
+ end
+
+ end
+
end
View
11 spec/monteta_store_spec.rb
@@ -28,4 +28,15 @@
sleep 1
@store.expired?('foo', 1).should be_true
end
+
+ context "after delete" do
+
+ it "should no longer exist" do
+ @store.set("key", "value")
+ @store.delete("key")
+ @store.exists?("key").should be_false
+ end
+
+ end
+
end
View
2 spec/spec_helper.rb
@@ -1,4 +1,4 @@
-require "spec"
+require "rspec"
require 'webmock/rspec'
$:.push File.join(File.dirname(__FILE__), '..', 'lib')
Something went wrong with that request. Please try again.