Merb::Cache::FileStore

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.

Attributes

dir[RW]

Public Class Methods

new(config = {}) click to toggle source

Creates directory for cached files unless it exists.

    # File lib/merb-cache/stores/fundamental/file_store.rb, line 13
13:     def initialize(config = {})
14:       @dir = config[:dir] || Merb.root_path(:tmp / :cache)
15: 
16:       create_path(@dir)
17:     end

Public Instance Methods

delete(key, parameters = {}) click to toggle source

Deletes cached template by key using FileUtils#rm.

    # File lib/merb-cache/stores/fundamental/file_store.rb, line 60
60:     def delete(key, parameters = {})
61:       if File.file?(path = pathify(key, parameters))
62:         FileUtils.rm(path)
63:       end
64:     end
delete_all() click to toggle source
    # File lib/merb-cache/stores/fundamental/file_store.rb, line 66
66:     def delete_all
67:       raise NotSupportedError
68:     end
exists?(key, parameters = {}) click to toggle source

Checks if cached template with given key exists.

    # File lib/merb-cache/stores/fundamental/file_store.rb, line 55
55:     def exists?(key, parameters = {})
56:       File.file?(pathify(key, parameters))
57:     end
fetch(key, parameters = {}, conditions = {}, &blk) click to toggle source

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.

    # File lib/merb-cache/stores/fundamental/file_store.rb, line 50
50:     def fetch(key, parameters = {}, conditions = {}, &blk)
51:       read(key, parameters) || (writable?(key, parameters, conditions) && write(key, value = blk.call, parameters, conditions) && value)
52:     end
pathify(key, parameters = {}) click to toggle source
    # File lib/merb-cache/stores/fundamental/file_store.rb, line 70
70:     def pathify(key, parameters = {})
71:       if key.to_s =~ /^\//
72:         path = "#{@dir}#{key}"
73:       else
74:         path = "#{@dir}/#{key}"
75:       end
76: 
77:       path << "--#{parameters.to_sha2}" unless parameters.empty?
78:       path
79:     end
read(key, parameters = {}) click to toggle source

Reads cached template from disk if it exists.

    # File lib/merb-cache/stores/fundamental/file_store.rb, line 29
29:     def read(key, parameters = {})
30:       if exists?(key, parameters)
31:         read_file(pathify(key, parameters))
32:       end
33:     end
writable?(key, parameters = {}, conditions = {}) click to toggle source

File caching does not support expiration time.

    # File lib/merb-cache/stores/fundamental/file_store.rb, line 20
20:     def writable?(key, parameters = {}, conditions = {})
21:       case key
22:       when String, Numeric, Symbol
23:         !conditions.has_key?(:expire_in)
24:       else nil
25:       end
26:     end
write(key, data = nil, parameters = {}, conditions = {}) click to toggle source

Writes cached template to disk, creating cache directory if it does not exist.

    # File lib/merb-cache/stores/fundamental/file_store.rb, line 37
37:     def write(key, data = nil, parameters = {}, conditions = {})
38:       if writable?(key, parameters, conditions)
39:         if File.file?(path = pathify(key, parameters))
40:           write_file(path, data)
41:         else
42:           create_path(path) && write_file(path, data)
43:         end
44:       end
45:     end

Protected Instance Methods

create_path(path) click to toggle source
    # File lib/merb-cache/stores/fundamental/file_store.rb, line 83
83:     def create_path(path)
84:       FileUtils.mkdir_p(File.dirname(path))
85:     end
read_file(path) click to toggle source

Reads file content. Access to the file uses file locking.

    # File lib/merb-cache/stores/fundamental/file_store.rb, line 89
89:     def read_file(path)
90:       data = nil
91:       File.open(path, "r") do |file|
92:         file.flock(File::LOCK_EX)
93:         data = file.read
94:         file.flock(File::LOCK_UN)
95:       end
96: 
97:       data
98:     end
write_file(path, data) click to toggle source

Writes file content. Access to the file uses file locking.

     # File lib/merb-cache/stores/fundamental/file_store.rb, line 102
102:     def write_file(path, data)
103:       File.open(path, "w+") do |file|
104:         file.flock(File::LOCK_EX)
105:         file.write(data)
106:         file.flock(File::LOCK_UN)
107:       end
108: 
109:       true
110:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.