Class: Merb::Cache::FileStore
- Inherits:
-
AbstractStore
- Object
- AbstractStore
- Merb::Cache::FileStore
- Defined in:
- merb-cache/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)
-
- (Object) dir
Returns the value of attribute dir.
Instance Method Summary (collapse)
-
- (Object) delete(key, parameters = {})
Deletes cached template by key using FileUtils#rm.
- - (Object) delete_all
-
- (Boolean) exists?(key, parameters = {})
Checks if cached template with given key exists.
-
- (Object) fetch(key, parameters = {}, conditions = {}, &blk)
Fetches cached data by key if it exists.
-
- (FileStore) initialize(config = {})
constructor
Creates directory for cached files unless it exists.
- - (Object) pathify(key, parameters = {})
-
- (Object) read(key, parameters = {})
Reads cached template from disk if it exists.
-
- (Boolean) writable?(key, parameters = {}, conditions = {})
File caching does not support expiration time.
-
- (Object) write(key, data = nil, parameters = {}, conditions = {})
Writes cached template to disk, creating cache directory if it does not exist.
Methods inherited from AbstractStore
Constructor Details
- (FileStore) initialize(config = {})
Creates directory for cached files unless it exists.
17 18 19 20 21 |
# File 'merb-cache/lib/merb-cache/stores/fundamental/file_store.rb', line 17 def initialize(config = {}) @dir = config[:dir] || Merb.root(:tmp / :cache) create_path(@dir) end |
Instance Attribute Details
- (Object) dir
Returns the value of attribute dir
10 11 12 |
# File 'merb-cache/lib/merb-cache/stores/fundamental/file_store.rb', line 10 def dir @dir end |
Instance Method Details
- (Object) delete(key, parameters = {})
Deletes cached template by key using FileUtils#rm.
64 65 66 67 68 |
# File 'merb-cache/lib/merb-cache/stores/fundamental/file_store.rb', line 64 def delete(key, parameters = {}) if File.file?(path = pathify(key, parameters)) FileUtils.rm(path) end end |
- (Object) delete_all
70 71 72 |
# File 'merb-cache/lib/merb-cache/stores/fundamental/file_store.rb', line 70 def delete_all raise NotSupportedError end |
- (Boolean) exists?(key, parameters = {})
Checks if cached template with given key exists.
59 60 61 |
# File 'merb-cache/lib/merb-cache/stores/fundamental/file_store.rb', line 59 def exists?(key, parameters = {}) File.file?(pathify(key, parameters)) end |
- (Object) fetch(key, parameters = {}, conditions = {}, &blk)
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.
54 55 56 |
# File 'merb-cache/lib/merb-cache/stores/fundamental/file_store.rb', line 54 def fetch(key, parameters = {}, conditions = {}, &blk) read(key, parameters) || (writable?(key, parameters, conditions) && write(key, value = blk.call, parameters, conditions) && value) end |
- (Object) pathify(key, parameters = {})
74 75 76 77 78 79 80 81 82 83 |
# File 'merb-cache/lib/merb-cache/stores/fundamental/file_store.rb', line 74 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 |
- (Object) read(key, parameters = {})
Reads cached template from disk if it exists.
33 34 35 36 37 |
# File 'merb-cache/lib/merb-cache/stores/fundamental/file_store.rb', line 33 def read(key, parameters = {}) if exists?(key, parameters) read_file(pathify(key, parameters)) end end |
- (Boolean) writable?(key, parameters = {}, conditions = {})
File caching does not support expiration time.
24 25 26 27 28 29 30 |
# File 'merb-cache/lib/merb-cache/stores/fundamental/file_store.rb', line 24 def writable?(key, parameters = {}, conditions = {}) case key when String, Numeric, Symbol !conditions.has_key?(:expire_in) else nil end end |
- (Object) write(key, data = nil, parameters = {}, conditions = {})
Writes cached template to disk, creating cache directory if it does not exist.
41 42 43 44 45 46 47 48 49 |
# File 'merb-cache/lib/merb-cache/stores/fundamental/file_store.rb', line 41 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 |