This commit is contained in:
Christian Mehlmauer
2014-12-15 12:37:46 +01:00
parent 88808db9a5
commit 6654f446a4

View File

@@ -9,6 +9,7 @@
# #
require 'yaml' require 'yaml'
require 'fileutils'
class CacheFileStore class CacheFileStore
attr_reader :storage_path, :serializer attr_reader :storage_path, :serializer
@@ -18,6 +19,7 @@ class CacheFileStore
# YAML is Human Readable, contrary to Marshal which store in a binary format # YAML is Human Readable, contrary to Marshal which store in a binary format
# Marshal does not need any "require" # Marshal does not need any "require"
def initialize(storage_path, serializer = Marshal) def initialize(storage_path, serializer = Marshal)
@storage_dir = File.expand_path(storage_path)
@storage_path = File.expand_path(File.join(storage_path, storage_dir)) @storage_path = File.expand_path(File.join(storage_path, storage_dir))
@serializer = serializer @serializer = serializer
@@ -29,15 +31,23 @@ class CacheFileStore
end end
def clean def clean
Dir[File.join(@storage_path, '*')].each do |f| # clean old directories
File.delete(f) unless File.symlink?(f) Dir[File.join(@storage_dir, '*')].each do |f|
if File.directory?(f)
# delete directory if create time is older than 4 hours
FileUtils.rm_rf(f) if File.mtime(f) < (Time.now - (60*240))
else
File.delete(f) unless File.symlink?(f)
end
end end
end end
def read_entry(key) def read_entry(key)
@serializer.load(File.read(get_entry_file_path(key))) begin
rescue @serializer.load(File.read(get_entry_file_path(key)))
nil rescue
nil
end
end end
def write_entry(key, data_to_store, cache_ttl) def write_entry(key, data_to_store, cache_ttl)