diff --git a/Gemfile.lock b/Gemfile.lock index f8a2757..ac5261e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,34 +1,35 @@ GEM remote: https://rubygems.org/ specs: - diff-lcs (1.2.5) - docile (1.1.5) - json (2.0.2) + diff-lcs (1.5.1) + docile (1.4.1) memcached (1.8.0) - rake (11.2.2) - redcarpet (3.3.4) - rspec (3.5.0) - rspec-core (~> 3.5.0) - rspec-expectations (~> 3.5.0) - rspec-mocks (~> 3.5.0) - rspec-core (3.5.3) - rspec-support (~> 3.5.0) - rspec-expectations (3.5.0) + rake (13.2.1) + redcarpet (3.6.0) + rspec (3.13.0) + rspec-core (~> 3.13.0) + rspec-expectations (~> 3.13.0) + rspec-mocks (~> 3.13.0) + rspec-core (3.13.2) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.3) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.5.0) - rspec-mocks (3.5.0) + rspec-support (~> 3.13.0) + rspec-mocks (3.13.2) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.5.0) - rspec-support (3.5.0) - simplecov (0.12.0) - docile (~> 1.1.0) - json (>= 1.8, < 3) - simplecov-html (~> 0.10.0) - simplecov-html (0.10.0) - yard (0.9.5) + rspec-support (~> 3.13.0) + rspec-support (3.13.1) + simplecov (0.22.0) + docile (~> 1.1) + simplecov-html (~> 0.11) + simplecov_json_formatter (~> 0.1) + simplecov-html (0.13.1) + simplecov_json_formatter (0.1.4) + yard (0.9.37) PLATFORMS ruby + x86_64-linux DEPENDENCIES memcached @@ -39,4 +40,4 @@ DEPENDENCIES yard BUNDLED WITH - 1.11.2 + 2.5.23 diff --git a/lib/diskcached.rb b/lib/diskcached.rb index 9cd2867..f4f388e 100644 --- a/lib/diskcached.rb +++ b/lib/diskcached.rb @@ -1,7 +1,7 @@ # @author Joshua P. Mervine class Diskcached # version for gem - VERSION = '1.1.3' + VERSION = '1.1.4' # disk location for cache store attr_reader :store @@ -45,16 +45,15 @@ def timeout=(t) @gc_time = t end - # return true if cache with 'key' is expired + # return true if cache with 'key' is expired or there is no cache entry def expired? key - return false if timeout.nil? mtime = read_cache_mtime(key) - return (mtime.nil? || mtime+timeout <= Time.now) + return mtime.nil? || (timeout && mtime+timeout <= Time.now) end # expire cache with 'key' def delete key - File.delete( cache_file(key) ) if File.exists?( cache_file(key) ) + File.delete( cache_file(key) ) if File.exist?( cache_file(key) ) end # expire (delete) all caches in #store directory @@ -120,7 +119,7 @@ def set key, value # or raises Diskcache::NotFound # - if 'key' is an Array returns only keys # which exist and aren't expired, it raises - # Diskcache::NotFound if none are available + # Diskcached::NotFound if none are available def get key if key.is_a? Array ret = {} @@ -149,7 +148,7 @@ def cache_file key private # creates the actual cache file def write_cache_file key, content - f = File.open( cache_file(key), "w+" ) + f = File.open( cache_file(key), "wb+" ) f.flock(File::LOCK_EX) f.write( content ) f.close @@ -158,7 +157,7 @@ def write_cache_file key, content # reads the actual cache file def read_cache_file key - f = File.open( cache_file(key), "r" ) + f = File.open( cache_file(key), "rb" ) f.flock(File::LOCK_SH) out = f.read f.close @@ -168,7 +167,7 @@ def read_cache_file key # returns mtime of cache file or nil if # file doesn't exist def read_cache_mtime key - return nil unless File.exists?(cache_file(key)) + return nil unless File.exist?(cache_file(key)) File.mtime( cache_file(key) ) end @@ -177,7 +176,7 @@ def ensure_store_directory FileUtils.mkpath( store ) unless File.directory?( store ) end - class NotFound < Exception + class NotFound < StandardError end end diff --git a/spec/diskcached_spec.rb b/spec/diskcached_spec.rb index 95a2546..0661e0d 100644 --- a/spec/diskcached_spec.rb +++ b/spec/diskcached_spec.rb @@ -38,7 +38,7 @@ expect(@cache.set('test1', "test string")).to be_truthy end it "should create a file on disk" do - expect(File.exists?(File.join($cachedir, "test1.cache"))).to be_truthy + expect(File.exist?(File.join($cachedir, "test1.cache"))).to be_truthy end end @@ -81,10 +81,10 @@ expect(@cache.cache('test1') do "test string" end).to eq "test string" - expect(File.exists?(File.join($cachedir, "test1.cache"))).to be_truthy + expect(File.exist?(File.join($cachedir, "test1.cache"))).to be_truthy end it "should create a file on disk" do - expect(File.exists?(File.join($cachedir, "test1.cache"))).to be_truthy + expect(File.exist?(File.join($cachedir, "test1.cache"))).to be_truthy end it "should read cache before expiration" do expect(@cache.cache('test1')).to eq "test string" @@ -116,6 +116,19 @@ expect(@cache.expired?('test2')).to be_truthy end end + + describe "#expired?_timeout" do + before(:all) do + @cache = Diskcached.new($cachedir, nil) + end + it "should be true" do + expect(@cache.expired?('test2')).to be_falsey + end + it "should be false" do + @cache.cache('test2') { "cache test2" } + expect(@cache.expired?('test2')).to be_falsey + end + end describe "#delete" do before(:all) do @@ -128,7 +141,7 @@ expect(@cache.expired?('test3')).to be_truthy end it "should remove cache file" do - expect(File.exists?(File.join($cachedir, "test3.cache"))).to be_falsey + expect(File.exist?(File.join($cachedir, "test3.cache"))).to be_falsey end end @@ -168,13 +181,13 @@ expect(@cache.expired?('flush1')).to be_truthy @cache.instance_variable_set(:@gc_last, Time.now) expect { @cache.flush_expired }.to_not raise_error - expect(File.exists?(File.join($cachedir, "flush1.cache"))).to be_truthy + expect(File.exist?(File.join($cachedir, "flush1.cache"))).to be_truthy end it "should flush caches are are expired" do sleep 2 expect { @cache.flush_expired }.to_not raise_error expect(@cache.expired?('flush1')).to be_truthy - expect(File.exists?(File.join($cachedir, "flush1.cache"))).not_to be_truthy + expect(File.exist?(File.join($cachedir, "flush1.cache"))).not_to be_truthy end end @@ -193,7 +206,7 @@ expect(@cache.expired?('flush1')).to be_truthy @cache.instance_variable_set(:@gc_last, Time.now) expect { @cache.flush_expired! }.to_not raise_error - expect(File.exists?(File.join($cachedir, "flush1.cache"))).to_not be_truthy + expect(File.exist?(File.join($cachedir, "flush1.cache"))).to_not be_truthy end end @@ -217,9 +230,9 @@ sleep 2 expect { @cache.cache('test10') { "cache test10" } }.to_not raise_error sleep 1 - expect(File.exists?(@cache.cache_file('test8'))).to be_falsey - expect(File.exists?(@cache.cache_file('test9'))).to be_falsey - expect(File.exists?(@cache.cache_file('test10'))).to be_truthy + expect(File.exist?(@cache.cache_file('test8'))).to be_falsey + expect(File.exist?(@cache.cache_file('test9'))).to be_falsey + expect(File.exist?(@cache.cache_file('test10'))).to be_truthy end end @@ -234,9 +247,9 @@ sleep 2 expect { @cache.cache('test10') { "cache test10" } }.to_not raise_error sleep 1 - expect(File.exists?(@cache.cache_file('test8'))).to be_truthy - expect(File.exists?(@cache.cache_file('test9'))).to be_truthy - expect(File.exists?(@cache.cache_file('test10'))).to be_truthy + expect(File.exist?(@cache.cache_file('test8'))).to be_truthy + expect(File.exist?(@cache.cache_file('test9'))).to be_truthy + expect(File.exist?(@cache.cache_file('test10'))).to be_truthy end end