CacheFileStore back to the previous state

This commit is contained in:
erwanlr
2013-02-21 12:12:07 +01:00
parent 88e33c5b4e
commit 4f3225c56a
2 changed files with 20 additions and 23 deletions

View File

@@ -51,7 +51,7 @@ class CacheFileStore
end end
end end
def get(key) def read_entry(key)
entry_file_path = get_entry_file_path(key) entry_file_path = get_entry_file_path(key)
if File.exists?(entry_file_path) if File.exists?(entry_file_path)
@@ -59,7 +59,7 @@ class CacheFileStore
end end
end end
def set(key, data_to_store, cache_timeout) def write_entry(key, data_to_store, cache_timeout)
if cache_timeout > 0 if cache_timeout > 0
File.open(get_entry_file_path(key), 'w') do |f| File.open(get_entry_file_path(key), 'w') do |f|
f.write(@serializer.dump(data_to_store)) f.write(@serializer.dump(data_to_store))

View File

@@ -20,15 +20,12 @@
require 'spec_helper' require 'spec_helper'
describe CacheFileStore do describe CacheFileStore do
let(:cache_dir) { SPEC_CACHE_DIR + '/cache_file_store' }
before :all do
@cache_dir = SPEC_CACHE_DIR + '/cache_file_store'
end
before :each do before :each do
Dir.delete(@cache_dir) rescue nil Dir.delete(cache_dir) rescue nil
@cache = CacheFileStore.new(@cache_dir) @cache = CacheFileStore.new(cache_dir)
end end
after :each do after :each do
@@ -37,7 +34,7 @@ describe CacheFileStore do
describe '#storage_path' do describe '#storage_path' do
it 'returns the storage path given in the #new' do it 'returns the storage path given in the #new' do
@cache.storage_path.should == @cache_dir @cache.storage_path.should == cache_dir
end end
end end
@@ -52,39 +49,39 @@ describe CacheFileStore do
it "should remove all files from the cache dir (#{@cache_dir}" do it "should remove all files from the cache dir (#{@cache_dir}" do
# let's create some files into the directory first # let's create some files into the directory first
(0..5).each do |i| (0..5).each do |i|
File.new(@cache_dir + "/file_#{i}.txt", File::CREAT) File.new(cache_dir + "/file_#{i}.txt", File::CREAT)
end end
count_files_in_dir(@cache_dir, 'file_*.txt').should == 6 count_files_in_dir(cache_dir, 'file_*.txt').should == 6
@cache.clean @cache.clean
count_files_in_dir(@cache_dir).should == 0 count_files_in_dir(cache_dir).should == 0
end end
end end
describe '#get (nonexistent entry)' do describe '#read_entry (nonexistent entry)' do
it 'should return nil' do it 'should return nil' do
@cache.get(Digest::SHA1.hexdigest('hello world')).should be_nil @cache.read_entry(Digest::SHA1.hexdigest('hello world')).should be_nil
end end
end end
describe '#set, #get' do describe '#write_entry, #read_entry' do
after :each do after :each do
@cache.set(@key, @data, @timeout) @cache.write_entry(@key, @data, @timeout)
@cache.get(@key).should === @expected @cache.read_entry(@key).should === @expected
end end
it 'should get the correct entry (string)' do it 'should get the correct entry (string)' do
@timeout = 10 @timeout = 10
@key = 'some_key' @key = 'some_key'
@data = 'Hello World !' @data = 'Hello World !'
@expected = @data @expected = @data
end end
it 'should not write the entry' do it 'should not write the entry' do
@timeout = 0 @timeout = 0
@key = 'another_key' @key = 'another_key'
@data = 'Another Hello World !' @data = 'Another Hello World !'
@expected = nil @expected = nil
end end