Class: RKD::Query::FileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/r_k_d/query/file_cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ FileCache

Returns a new instance of FileCache.



9
10
11
# File 'lib/r_k_d/query/file_cache.rb', line 9

def initialize(key)
  @key = key
end

Class Method Details

.cache(key) ⇒ Object



5
6
7
# File 'lib/r_k_d/query/file_cache.rb', line 5

def cache(key)
  new(key).cache { yield }
end

Instance Method Details

#cacheObject



13
14
15
16
17
18
19
20
21
# File 'lib/r_k_d/query/file_cache.rb', line 13

def cache
  if present?
    File.read(file_path)
  else
    response = yield
    File.write(file_path, response)
    response
  end
end

#flushObject



27
28
29
# File 'lib/r_k_d/query/file_cache.rb', line 27

def flush
  File.delete(file_path)
end

#present?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/r_k_d/query/file_cache.rb', line 23

def present?
  @present ||= RKD::Config.file_cache_enabled? && File.exist?(file_path) && (File.ctime(file_path) > (Time.now - 86400))
end