Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete key #5

Merged
merged 4 commits into from Jan 13, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion api_cache.gemspec
Expand Up @@ -16,7 +16,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"] 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('webmock')
s.add_development_dependency('rake') s.add_development_dependency('rake')
s.add_development_dependency('moneta', "~> 0.6.0") s.add_development_dependency('moneta', "~> 0.6.0")
Expand Down
7 changes: 7 additions & 0 deletions lib/api_cache.rb
Expand Up @@ -128,6 +128,13 @@ def self.get(key, options = {}, &block)
end end
end end
end end

# Manually delete data from the cache.
#
def self.delete(key)
APICache::Cache.new(key, {}).delete
end

end end


require 'api_cache/cache' require 'api_cache/cache'
Expand Down
5 changes: 5 additions & 0 deletions lib/api_cache/abstract_store.rb
Expand Up @@ -14,6 +14,11 @@ def get(key)
raise "Method not implemented. Called abstract class." raise "Method not implemented. Called abstract class."
end end


# Delete value.
def delete(key)
raise "Method not implemented. Called abstract class."
end

# Does a given key exist in the cache? # Does a given key exist in the cache?
def exists?(key) def exists?(key)
raise "Method not implemented. Called abstract class." raise "Method not implemented. Called abstract class."
Expand Down
4 changes: 4 additions & 0 deletions lib/api_cache/cache.rb
Expand Up @@ -47,6 +47,10 @@ def set(value)
true true
end end


def delete
store.delete(hash)
end

private private


def hash def hash
Expand Down
5 changes: 5 additions & 0 deletions lib/api_cache/dalli_store.rb
Expand Up @@ -16,6 +16,11 @@ def get(key)
@dalli.get(key) @dalli.get(key)
end end


# Delete value.
def delete(key)
@dalli.delete(key)
end

# Does a given key exist in the cache? # Does a given key exist in the cache?
def exists?(key) def exists?(key)
!get(key).nil? !get(key).nil?
Expand Down
4 changes: 4 additions & 0 deletions lib/api_cache/memory_store.rb
Expand Up @@ -18,6 +18,10 @@ def get(key)
data data
end end


def delete(key)
@cache.delete(key)
end

def exists?(key) def exists?(key)
!@cache[key].nil? !@cache[key].nil?
end end
Expand Down
5 changes: 5 additions & 0 deletions lib/api_cache/moneta_store.rb
Expand Up @@ -16,6 +16,11 @@ def get(key)
@moneta[key] @moneta[key]
end end


# Delete value.
def delete(key)
@moneta.delete(key)
end

# Does a given key exist in the cache? # Does a given key exist in the cache?
def exists?(key) def exists?(key)
@moneta.key?(key) @moneta.key?(key)
Expand Down
3 changes: 3 additions & 0 deletions lib/api_cache/null_store.rb
Expand Up @@ -13,6 +13,9 @@ def set(key, value)
true true
end end


def delete(key)
end

def expired?(key, timeout) def expired?(key, timeout)
true true
end end
Expand Down
144 changes: 89 additions & 55 deletions spec/api_cache_spec.rb
Expand Up @@ -40,62 +40,96 @@
end end


describe "get method" do describe "get method" do
before :each do
@api = mock(APICache::API, :get => @api_data) context "when cache is mocked" do
@cache = mock(APICache::Cache, :get => @cache_data, :set => true) before :each do

@api = mock(APICache::API, :get => @api_data)
APICache::API.stub!(:new).and_return(@api) @cache = mock(APICache::Cache, :get => @cache_data, :set => true)
APICache::Cache.stub!(:new).and_return(@cache)
end APICache::API.stub!(:new).and_return(@api)

APICache::Cache.stub!(:new).and_return(@cache)
it "should fetch data from the cache if the state is :current" do end
@cache.stub!(:state).and_return(:current)

it "should fetch data from the cache if the state is :current" do
APICache.get(@key).should == @cache_data @cache.stub!(:state).and_return(:current)
end

APICache.get(@key).should == @cache_data
it "should make new request to API if the state is :refetch and store result in cache" do end
@cache.stub!(:state).and_return(:refetch)
@cache.should_receive(:set).with(@api_data) it "should make new request to API if the state is :refetch and store result in cache" do

@cache.stub!(:state).and_return(:refetch)
APICache.get(@key).should == @api_data @cache.should_receive(:set).with(@api_data)
end

APICache.get(@key).should == @api_data
it "should return the cached value if the state is :refetch but the api is not accessible" do end
@cache.stub!(:state).and_return(:refetch)
@api.should_receive(:get).with.and_raise(APICache::CannotFetch) it "should return the cached value if the state is :refetch but the api is not accessible" do

@cache.stub!(:state).and_return(:refetch)
APICache.get(@key).should == @cache_data @api.should_receive(:get).with.and_raise(APICache::CannotFetch)
end

APICache.get(@key).should == @cache_data
it "should make new request to API if the state is :invalid" do end
@cache.stub!(:state).and_return(:invalid)

it "should make new request to API if the state is :invalid" do
APICache.get(@key).should == @api_data @cache.stub!(:state).and_return(:invalid)
end

APICache.get(@key).should == @api_data
it "should raise CannotFetch if the api cannot fetch data and the cache state is :invalid" do end
@cache.stub!(:state).and_return(:invalid)
@api.should_receive(:get).with.and_raise(APICache::CannotFetch) it "should raise CannotFetch if the api cannot fetch data and the cache state is :invalid" do

@cache.stub!(:state).and_return(:invalid)
lambda { @api.should_receive(:get).with.and_raise(APICache::CannotFetch)
APICache.get(@key).should
}.should raise_error(APICache::CannotFetch) lambda {
end APICache.get(@key).should

}.should raise_error(APICache::CannotFetch)
it "should make new request to API if the state is :missing" do end
@cache.stub!(:state).and_return(:missing)

it "should make new request to API if the state is :missing" do
APICache.get(@key).should == @api_data @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 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) context "when cache is not mocked" do
@api.should_receive(:get).with.and_raise(APICache::CannotFetch)

before :each do
lambda { APICache.store = APICache::MemoryStore.new
APICache.get(@key).should @api = mock(APICache::API, :get => @api_data)
}.should raise_error(APICache::CannotFetch) 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 end

end end
6 changes: 6 additions & 0 deletions spec/cache_spec.rb
Expand Up @@ -33,4 +33,10 @@
sleep 1 sleep 1
cache.state.should == :invalid cache.state.should == :invalid
end end

it "should initially have invalid state" do
cache = APICache::Cache.new('foo', @options)
cache.state.should == :invalid
end

end end
11 changes: 11 additions & 0 deletions spec/dalli_store_spec.rb
Expand Up @@ -27,4 +27,15 @@
sleep 1 sleep 1
@store.expired?('foo', 1).should be_true @store.expired?('foo', 1).should be_true
end 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 end
11 changes: 11 additions & 0 deletions spec/monteta_store_spec.rb
Expand Up @@ -28,4 +28,15 @@
sleep 1 sleep 1
@store.expired?('foo', 1).should be_true @store.expired?('foo', 1).should be_true
end 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 end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
@@ -1,4 +1,4 @@
require "spec" require "rspec"
require 'webmock/rspec' require 'webmock/rspec'


$:.push File.join(File.dirname(__FILE__), '..', 'lib') $:.push File.join(File.dirname(__FILE__), '..', 'lib')
Expand Down