Class: Merb::Cache::FileStore
- Inherits:
-
AbstractStore
- Object
- AbstractStore
- Merb::Cache::FileStore
- Defined in:
- lib/merb-cache/stores/fundamental/file_store.rb
Overview
Cache store that uses files. Usually this is good for fragment and page caching but not object caching.
By default cached files are stored in tmp/cache under Merb.root directory. To use other location pass :dir option to constructor.
File caching does not support expiration time.
Instance Attribute Summary collapse
-
#dir ⇒ Object
Returns the value of attribute dir.
Instance Method Summary collapse
-
#delete(key, parameters = {}) ⇒ Object
Deletes cached template by key using FileUtils#rm.
- #delete_all ⇒ Object
-
#exists?(key, parameters = {}) ⇒ Boolean
Checks if cached template with given key exists.
-
#fetch(key, parameters = {}, conditions = {}, &blk) ⇒ Object
Fetches cached data by key if it exists.
-
#initialize(config = {}) ⇒ FileStore
constructor
Creates directory for cached files unless it exists.
- #pathify(key, parameters = {}) ⇒ Object
-
#read(key, parameters = {}) ⇒ Object
Reads cached template from disk if it exists.
-
#writable?(key, parameters = {}, conditions = {}) ⇒ Boolean
File caching does not support expiration time.
-
#write(key, data = nil, parameters = {}, conditions = {}) ⇒ Object
Writes cached template to disk, creating cache directory if it does not exist.
Methods inherited from AbstractStore
Constructor Details
Instance Attribute Details
#dir ⇒ Object
Returns the value of attribute dir.
10 11 12 |
# File 'lib/merb-cache/stores/fundamental/file_store.rb', line 10 def dir @dir end |
Instance Method Details
#delete(key, parameters = {}) ⇒ Object
Deletes cached template by key using FileUtils#rm.
60 61 62 63 64 |
# File 'lib/merb-cache/stores/fundamental/file_store.rb', line 60 def delete(key, parameters = {}) if File.file?(path = pathify(key, parameters)) FileUtils.rm(path) end end |
#delete_all ⇒ Object
66 67 68 |
# File 'lib/merb-cache/stores/fundamental/file_store.rb', line 66 def delete_all raise NotSupportedError end |
#exists?(key, parameters = {}) ⇒ Boolean
Checks if cached template with given key exists.
55 56 57 |
# File 'lib/merb-cache/stores/fundamental/file_store.rb', line 55 def exists?(key, parameters = {}) File.file?(pathify(key, parameters)) end |
#fetch(key, parameters = {}, conditions = {}, &blk) ⇒ Object
Fetches cached data by key if it exists. If it does not, uses passed block to get new cached value and writes it using given key.
50 51 52 |
# File 'lib/merb-cache/stores/fundamental/file_store.rb', line 50 def fetch(key, parameters = {}, conditions = {}, &blk) read(key, parameters) || (writable?(key, parameters, conditions) && write(key, value = blk.call, parameters, conditions) && value) end |
#pathify(key, parameters = {}) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/merb-cache/stores/fundamental/file_store.rb', line 70 def pathify(key, parameters = {}) if key.to_s =~ /^\// path = "#{@dir}#{key}" else path = "#{@dir}/#{key}" end path << "--#{parameters.to_sha2}" unless parameters.empty? path end |
#read(key, parameters = {}) ⇒ Object
Reads cached template from disk if it exists.
29 30 31 32 33 |
# File 'lib/merb-cache/stores/fundamental/file_store.rb', line 29 def read(key, parameters = {}) if exists?(key, parameters) read_file(pathify(key, parameters)) end end |
#writable?(key, parameters = {}, conditions = {}) ⇒ Boolean
File caching does not support expiration time.
20 21 22 23 24 25 26 |
# File 'lib/merb-cache/stores/fundamental/file_store.rb', line 20 def writable?(key, parameters = {}, conditions = {}) case key when String, Numeric, Symbol !conditions.has_key?(:expire_in) else nil end end |
#write(key, data = nil, parameters = {}, conditions = {}) ⇒ Object
Writes cached template to disk, creating cache directory if it does not exist.
37 38 39 40 41 42 43 44 45 |
# File 'lib/merb-cache/stores/fundamental/file_store.rb', line 37 def write(key, data = nil, parameters = {}, conditions = {}) if writable?(key, parameters, conditions) if File.file?(path = pathify(key, parameters)) write_file(path, data) else create_path(path) && write_file(path, data) end end end |