class Tml::CacheAdapters::File

Public Class Methods

cache() click to toggle source
# File lib/tml/cache_adapters/file.rb, line 35
def self.cache
  @cache ||= {}
end
cache_path() click to toggle source
# File lib/tml/cache_adapters/file.rb, line 39
def self.cache_path
  "#{Tml.config.cache[:path]}/#{Tml.config.cache[:version]}"
end
file_path(key) click to toggle source
# File lib/tml/cache_adapters/file.rb, line 43
def self.file_path(key)
  File.join(cache_path, "#{key}.json")
end

Public Instance Methods

cache_name() click to toggle source
# File lib/tml/cache_adapters/file.rb, line 47
def cache_name
  'file'
end
clear(opts = {}) click to toggle source
# File lib/tml/cache_adapters/file.rb, line 84
def clear(opts = {})
  warn('This is a readonly cache')
end
delete(key, opts = {}) click to toggle source
# File lib/tml/cache_adapters/file.rb, line 76
def delete(key, opts = {})
  warn('This is a readonly cache')
end
exist?(key, opts = {}) click to toggle source
# File lib/tml/cache_adapters/file.rb, line 80
def exist?(key, opts = {})
  File.exists?(self.class.file_path(key))
end
fetch(key, opts = {}) { || ... } click to toggle source
# File lib/tml/cache_adapters/file.rb, line 51
def fetch(key, opts = {})
  if self.class.cache[key]
    info("Memory hit: #{key}")
    return self.class.cache[key]
  end

  path = self.class.file_path(key)

  if File.exists?(path)
    info("Cache hit: #{key}")
    self.class.cache[key] = JSON.parse(File.read(path))
    return self.class.cache[key]
  end

  info("Cache miss: #{key}")

  return nil unless block_given?

  yield
end
read_only?() click to toggle source
# File lib/tml/cache_adapters/file.rb, line 88
def read_only?
  true
end
store(key, data, opts = {}) click to toggle source
# File lib/tml/cache_adapters/file.rb, line 72
def store(key, data, opts = {})
  warn('This is a readonly cache')
end