Class: ReevooMark::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/reevoomark/cache.rb

Defined Under Namespace

Classes: Entry

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir) ⇒ Cache

Create a new cache repository, storing it’s cache in the given dir.



3
4
5
6
# File 'lib/reevoomark/cache.rb', line 3

def initialize(cache_dir)
  FileUtils.mkdir_p(cache_dir) unless File.exist?(cache_dir)
  @cache_dir = cache_dir
end

Instance Method Details

#fetch(remote_url, &fetcher) ⇒ Object

Fetch an unexpired cached document, or store the result of the block.



18
19
20
21
22
23
24
25
# File 'lib/reevoomark/cache.rb', line 18

def fetch(remote_url, &fetcher)
  entry = entry_for(remote_url)
  if entry.valid?
    entry.document
  else
    entry.document = fetcher.call
  end
end

#fetch_expired(remote_url, options = {}) ⇒ Object

Fetch the cache entry, don’t worry if it’s expired.



9
10
11
12
13
14
15
# File 'lib/reevoomark/cache.rb', line 9

def fetch_expired(remote_url, options = {})
  entry = entry_for(remote_url)
  if entry.exists?
    entry.revalidate_for(options[:revalidate_for])
    entry.document
  end
end