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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api_cache.gemspec
Original file line number Diff line number Diff line change
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.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")
Expand Down
7 changes: 7 additions & 0 deletions lib/api_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
5 changes: 5 additions & 0 deletions lib/api_cache/abstract_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
4 changes: 4 additions & 0 deletions lib/api_cache/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def set(value)
true
end

def delete
store.delete(hash)
end

private

def hash
Expand Down
5 changes: 5 additions & 0 deletions lib/api_cache/dalli_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
4 changes: 4 additions & 0 deletions lib/api_cache/memory_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def get(key)
data
end

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

def exists?(key)
!@cache[key].nil?
end
Expand Down
5 changes: 5 additions & 0 deletions lib/api_cache/moneta_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions lib/api_cache/null_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ def set(key, value)
true
end

def delete(key)
end

def expired?(key, timeout)
true
end
Expand Down
144 changes: 89 additions & 55 deletions spec/api_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions spec/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions spec/dalli_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions spec/monteta_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "spec"
require "rspec"
require 'webmock/rspec'

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