Skip to content

Commit

Permalink
Merge pull request #9 from TinderBox/memory-store-get
Browse files Browse the repository at this point in the history
memory_store: avoid nil array access by checking key exists. Returns nil if no key is found.
  • Loading branch information
mloughran committed Jul 16, 2014
2 parents 92394bb + ebe8b1e commit bd46b66
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/api_cache/memory_store.rb
Expand Up @@ -13,7 +13,7 @@ def set(key, value)
end

def get(key)
data = @cache[key][1]
data = exists?(key) ? @cache[key][1] : nil
APICache.logger.debug("cache: #{data.nil? ? "miss" : "hit"} (#{key})")
data
end
Expand Down
18 changes: 18 additions & 0 deletions spec/memory_store_spec.rb
@@ -0,0 +1,18 @@
require 'spec_helper'

describe APICache::MemoryStore do
before :each do
@store = APICache::MemoryStore.new
end

it "should set" do
@store.exists?('foo').should be_false
@store.set('foo', 'bar')
@store.exists?('foo').should be_true
@store.get('foo').should == 'bar'
end

it "should return nil if not found" do
@store.get('nothing').should be_nil
end
end

0 comments on commit bd46b66

Please sign in to comment.