Class: Merb::Cache::FileStore

Inherits:
AbstractStore show all
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)

Instance Method Summary (collapse)

Methods inherited from AbstractStore

#delete_all!, #write_all

Constructor Details

- (FileStore) initialize(config = {})

Creates directory for cached files unless it exists.

Parameters:

  • config (Hash) (defaults to: {})

    Configuration options

Options Hash (config):

  • :dir (String) — default: Merb.root_path(:tmp / :cache) Storage path.

    Merb.root_path(:tmp / :cache) Storage path.



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

Raises:



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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