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
def initialize(config = {})
  @dir = config[:dir] || Merb.root_path(:tmp / :cache)

  create_path(@dir)
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
def delete(key, parameters = {})
  if File.file?(path = pathify(key, parameters))
    FileUtils.rm(path)
  end
end
delete_all() click to toggle source
# File lib/merb-cache/stores/fundamental/file_store.rb, line 66
def delete_all
  raise NotSupportedError
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
def exists?(key, parameters = {})
  File.file?(pathify(key, parameters))
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
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 = {}) click to toggle source
# 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 = {}) click to toggle source

Reads cached template from disk if it exists.

# 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 = {}) click to toggle source

File caching does not support expiration time.

# 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 = {}) 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
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

Protected Instance Methods

create_path(path) click to toggle source
# File lib/merb-cache/stores/fundamental/file_store.rb, line 83
def create_path(path)
  FileUtils.mkdir_p(File.dirname(path))
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
def read_file(path)
  data = nil
  File.open(path, "r") do |file|
    file.flock(File::LOCK_EX)
    data = file.read
    file.flock(File::LOCK_UN)
  end

  data
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
def write_file(path, data)
  File.open(path, "w+") do |file|
    file.flock(File::LOCK_EX)
    file.write(data)
    file.flock(File::LOCK_UN)
  end

  true
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.