Class: JekyllGetAPI::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll-get-api/cache.rb

Class Method Summary collapse

Class Method Details

.cache_valid?(cache_file, cache_expiry) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/jekyll-get-api/cache.rb', line 27

def self.cache_valid?(cache_file, cache_expiry)
  File.exist?(cache_file) && (Time.now - File.mtime(cache_file)) < cache_expiry
end

.get(resource) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/jekyll-get-api/cache.rb', line 7

def self.get(resource)
  type = resource['type']
  name = resource['name']
  cache_expiry = resource['cache_expiry'] || 3600
  cache_file = File.join(@@root_path, '_cache', "jekyll-get-api-#{type}-#{name}.json")

  return nil unless cache_valid?(cache_file, cache_expiry)

  warn "Reading from cache for #{name}".green
  File.read(cache_file)
end

.root_path=(path) ⇒ Object



3
4
5
# File 'lib/jekyll-get-api/cache.rb', line 3

def self.root_path=(path)
  @@root_path = path
end

.write(resource, content) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/jekyll-get-api/cache.rb', line 19

def self.write(resource, content)
  type = resource['type']
  name = resource['name']
  cache_file = File.join(@@root_path, '_cache', "jekyll-get-api-#{type}-#{name}.json")
  FileUtils.mkdir_p(File.dirname(cache_file))
  File.write(cache_file, content)
end