forked from geminabox/geminabox
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
125 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
require "fileutils" | |||
|
|||
class Geminabox::DiskCache | |||
attr_reader :root_path | |||
|
|||
def initialize(root_path) | |||
@root_path = root_path | |||
ensure_dir_exists! | |||
end | |||
|
|||
def flush | |||
FileUtils.rm_rf(root_path) | |||
ensure_dir_exists! | |||
end | |||
|
|||
def cache(key) | |||
key = Digest::MD5.hexdigest(key) | |||
read(key) || write(key, yield) | |||
end | |||
|
|||
def read(key) | |||
path = File.join(root_path, key) | |||
File.read(path) if File.exists?(path) | |||
end | |||
|
|||
def write(key, value) | |||
path = File.join(root_path, key) | |||
File.open(path, 'wb'){|f| | |||
f << value | |||
} | |||
value | |||
end | |||
|
|||
protected | |||
|
|||
def ensure_dir_exists! | |||
FileUtils.mkdir_p(root_path) | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,43 @@ | |||
require 'test_helper' | |||
|
|||
class DiskCacheTest < MiniTest::Unit::TestCase | |||
DIR = "/tmp/geminabox-disk-cache-test" | |||
def setup | |||
FileUtils.rm_rf(DIR) | |||
end | |||
|
|||
def subject | |||
@subject ||= Geminabox::DiskCache.new(DIR) | |||
end | |||
|
|||
def test_cache_some_stuff | |||
called = 0 | |||
callable = lambda{ | |||
subject.cache("foo") do | |||
called += 1 | |||
"HELLO" | |||
end | |||
} | |||
assert_equal "HELLO", callable.call | |||
assert_equal "HELLO", callable.call | |||
assert_equal 1, called | |||
end | |||
|
|||
def test_flushing_the_cache | |||
assert_equal "foo", subject.cache("foo"){ "foo" } | |||
assert_equal "foo", subject.cache("foo"){ "bar" } | |||
|
|||
subject.flush | |||
|
|||
assert_equal "bar", subject.cache("foo"){ "bar" } | |||
end | |||
|
|||
def test_multiple_keys | |||
assert_equal "foo", subject.cache("foo"){ "foo" } | |||
assert_equal "bar", subject.cache("bar"){ "bar" } | |||
end | |||
|
|||
def teardown | |||
FileUtils.rm_rf(DIR) | |||
end | |||
end |