Class: Merb::Cache::FileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/merb-cache/cache-store/file.rb

Defined Under Namespace

Classes: NotAccessible

Instance Method Summary collapse

Constructor Details

#initializeFileStore

Provides the file cache store for merb-cache



5
6
7
8
9
10
# File 'lib/merb-cache/cache-store/file.rb', line 5

def initialize
  @config = Merb::Controller._cache.config
  @config[:cache_directory] ||= Merb.root_path("tmp/cache")
#    @config[:cache_action_directory] ||= Merb.dir_for(:public) / "cache"
  prepare
end

Instance Method Details

#cache(_controller, key, from_now = nil, &block) ⇒ Object

Capture or restore the data in cache. If the cache entry expired or does not exist, the data are taken from the execution of the block, marshalled and stored in cache. Otherwise, the data are loaded from the cache and returned unmarshalled

Parameters

_controller<Merb::Controller>

The instance of the current controller

key<String>

The key identifying the cache entry

from_now<~minutes>

The number of minutes (from now) the cache should persist

&block

The template to be used or not

Additional information

When fetching data (the cache entry exists and has not expired) The data are loaded from the cache and returned unmarshalled. Otherwise: The controller is used to capture the rendered template (from the block). It uses the capture_#engine and concat_#engine methods to do so. The captured data are then marshalled and stored.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/merb-cache/cache-store/file.rb', line 66

def cache(_controller, key, from_now = nil, &block)
  cache_file = @config[:cache_directory] / "#{key}.cache"
  _cache_hit = _data = _expire = nil
 
  if File.file?(cache_file)
    _data, _expire = Marshal.load(cache_read(cache_file))
    _cache_hit = true if _expire.nil? || Time.now < _expire
  end
  unless _cache_hit
    cache_directory = File.dirname(cache_file)
    FileUtils.mkdir_p(cache_directory)
    _expire = from_now ? from_now.minutes.from_now : nil
    _data = _controller.send(:capture, &block)
    cache_write(cache_file, Marshal.dump([_data, _expire]))
  end
  _controller.send(:concat, _data, block.binding)
  true
end

#cache_get(key) ⇒ Object

Fetch data from the file using the specified key The entry is deleted if it has expired

Parameter

key<Sting>

The key identifying the cache entry

Returns

data<String, NilClass>

nil is returned whether the entry expired or was not found



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/merb-cache/cache-store/file.rb', line 111

def cache_get(key)
  cache_file = @config[:cache_directory] / "#{key}.cache"
  if File.file?(cache_file)
    _data, _expire = Marshal.load(cache_read(cache_file))
    if _expire.nil? || Time.now < _expire
      Merb.logger.info("cache: hit (#{key})")
      return _data
    end
    FileUtils.rm_f(cache_file)
  end
  Merb.logger.info("cache: miss (#{key})")
  nil
end

#cache_set(key, data, from_now = nil) ⇒ Object

Store data to the file using the specified key

Parameters

key<Sting>

The key identifying the cache entry

data<String>

The data to be put in cache

from_now<~minutes>

The number of minutes (from now) the cache should persist



92
93
94
95
96
97
98
99
100
# File 'lib/merb-cache/cache-store/file.rb', line 92

def cache_set(key, data, from_now = nil)
  cache_file = @config[:cache_directory] / "#{key}.cache"
  cache_directory = File.dirname(cache_file)
  FileUtils.mkdir_p(cache_directory)
  _expire = from_now ? from_now.minutes.from_now : nil
  cache_write(cache_file, Marshal.dump([data, _expire]))
  Merb.logger.info("cache: set (#{key})")
  true
end

#cache_store_typeObject

Gives info on the current cache store

Returns

The type of the current cache store


158
159
160
# File 'lib/merb-cache/cache-store/file.rb', line 158

def cache_store_type
  "file"
end

#cached?(key) ⇒ Boolean

Checks whether a cache entry exists

Parameter

key<String>

The key identifying the cache entry

Returns

true if the cache entry exists, false otherwise

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
# File 'lib/merb-cache/cache-store/file.rb', line 36

def cached?(key)
  cache_file = @config[:cache_directory] / "#{key}.cache"
  _data = _expire = nil
  if File.file?(cache_file)
    _data, _expire = Marshal.load(cache_read(cache_file))
    return true if _expire.nil? || Time.now < _expire
    FileUtils.rm_f(cache_file)
  end
  false
end

#expire(key) ⇒ Object

Expire the cache entry identified by the given key

Parameter

key<Sting>

The key identifying the cache entry



129
130
131
132
133
# File 'lib/merb-cache/cache-store/file.rb', line 129

def expire(key)
  FileUtils.rm_f(@config[:cache_directory] / "#{key}.cache")
  Merb.logger.info("cache: expired (#{key})")
  true
end

#expire_allObject

Expire all the cache entries



148
149
150
151
152
# File 'lib/merb-cache/cache-store/file.rb', line 148

def expire_all
  FileUtils.rm_rf(Dir.glob("#{@config[:cache_directory]}/*"))
  Merb.logger.info("cache: expired all")
  true
end

#expire_match(key) ⇒ Object

Expire the cache entries matching the given key

Parameter

key<Sting>

The key matching the cache entries



139
140
141
142
143
144
145
# File 'lib/merb-cache/cache-store/file.rb', line 139

def expire_match(key)
  #files = Dir.glob(@config[:cache_directory] / "#{key}*.cache")
  files = Dir.glob(@config[:cache_directory] / "#{key}*")
  FileUtils.rm_rf(files)
  Merb.logger.info("cache: expired matching (#{key})")
  true
end

#prepareObject

This method is there to ensure minimal requirements are met (directories are accessible, table exists, connected to server, …)



20
21
22
23
24
25
26
27
# File 'lib/merb-cache/cache-store/file.rb', line 20

def prepare
  FileUtils.mkdir_p @config[:cache_directory]
  unless File.readable?(@config[:cache_directory]) &&
    File.writable?(@config[:cache_directory])
    raise NotAccessible, @config[:cache_directory]
  end
  true
end