Class: ActiveSupport::Cache::FileStore

Inherits:
Store show all
Defined in:
lib/active_support/cache/file_store.rb

Overview

A cache store implementation which stores everything on the filesystem.

Instance Attribute Summary collapse

Attributes inherited from Store

#logger_off, #silence

Instance Method Summary collapse

Methods inherited from Store

#decrement, #fetch, #increment, #mute, #silence!

Constructor Details

#initialize(cache_path) ⇒ FileStore

Returns a new instance of FileStore.



7
8
9
# File 'lib/active_support/cache/file_store.rb', line 7

def initialize(cache_path)
  @cache_path = cache_path
end

Instance Attribute Details

#cache_pathObject (readonly)

Returns the value of attribute cache_path.



5
6
7
# File 'lib/active_support/cache/file_store.rb', line 5

def cache_path
  @cache_path
end

Instance Method Details

#delete(name, options = nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/active_support/cache/file_store.rb', line 25

def delete(name, options = nil)
  super
  File.delete(real_file_path(name))
rescue SystemCallError => e
  # If there's no cache, then there's nothing to complain about
end

#delete_matched(matcher, options = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/active_support/cache/file_store.rb', line 32

def delete_matched(matcher, options = nil)
  super
  search_dir(@cache_path) do |f|
    if f =~ matcher
      begin
        File.delete(f)
      rescue SystemCallError => e
        # If there's no cache, then there's nothing to complain about
      end
    end
  end
end

#exist?(name, options = nil) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/active_support/cache/file_store.rb', line 45

def exist?(name, options = nil)
  super
  File.exist?(real_file_path(name))
end

#read(name, options = nil) ⇒ Object



11
12
13
14
# File 'lib/active_support/cache/file_store.rb', line 11

def read(name, options = nil)
  super
  File.open(real_file_path(name), 'rb') { |f| Marshal.load(f) } rescue nil
end

#write(name, value, options = nil) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/active_support/cache/file_store.rb', line 16

def write(name, value, options = nil)
  super
  ensure_cache_path(File.dirname(real_file_path(name)))
  File.atomic_write(real_file_path(name), cache_path) { |f| Marshal.dump(value, f) }
  value
rescue => e
  logger.error "Couldn't create cache directory: #{name} (#{e.message})" if logger
end