3 # A cache store implementation which stores everything on the filesystem.
4 class FileStore
< Store
5 attr_reader
:cache_path
7 def initialize(cache_path
)
8 @cache_path = cache_path
11 def read(name
, options
= nil)
13 File
.open(real_file_path(name
), 'rb') { |f
| Marshal
.load(f
) } rescue nil
16 def write(name
, value
, options
= nil)
18 ensure_cache_path(File
.dirname(real_file_path(name
)))
19 File
.atomic_write(real_file_path(name
), cache_path
) { |f
| Marshal
.dump(value
, f
) }
22 logger
.error
"Couldn't create cache directory: #{name} (#{e.message})" if logger
25 def delete(name
, options
= nil)
27 File
.delete(real_file_path(name
))
28 rescue SystemCallError
=> e
29 # If there's no cache, then there's nothing to complain about
32 def delete_matched(matcher
, options
= nil)
34 search_dir(@cache_path) do |f
|
38 rescue SystemCallError
=> e
39 # If there's no cache, then there's nothing to complain about
45 def exist
?(name
, options
= nil)
47 File
.exist
?(real_file_path(name
))
51 def real_file_path(name
)
52 '%s/%s.cache' % [@cache_path, name
.gsub('?', '.').gsub(':', '.')]
55 def ensure_cache_path(path
)
56 FileUtils
.makedirs(path
) unless File
.exist
?(path
)
59 def search_dir(dir
, &callback
)
60 Dir
.foreach(dir
) do |d
|
61 next if d
== "." || d
== ".."
62 name
= File
.join(dir
, d
)
63 if File
.directory
?(name
)
64 search_dir(name
, &callback
)