module Lightly::CacheOperations

Attributes

dir[W]
hash[W]
permissions[RW]

Public Class Methods

new(dir: 'cache', life: '1h', hash: true, enabled: true, permissions: nil) click to toggle source
# File lib/lightly/cache_operations.rb, line 9
def initialize(dir: 'cache', life: '1h', hash: true, enabled: true, permissions: nil)
  @dir = dir
  @life = life_to_seconds life
  @hash = hash
  @enabled = enabled
  @permissions = permissions
end

Public Instance Methods

cached?(key) click to toggle source
# File lib/lightly/cache_operations.rb, line 62
def cached?(key)
  path = get_path key
  File.exist?(path) and File.size(path).positive? and !expired?(path)
end
clear(key) click to toggle source
# File lib/lightly/cache_operations.rb, line 45
def clear(key)
  path = get_path key
  FileUtils.rm path if File.exist? path
end
dir() click to toggle source
# File lib/lightly/cache_operations.rb, line 33
def dir
  @dir ||= 'cache'
end
disable() click to toggle source
# File lib/lightly/cache_operations.rb, line 71
def disable
  @enabled = false
end
enable() click to toggle source
# File lib/lightly/cache_operations.rb, line 67
def enable
  @enabled = true
end
enabled?() click to toggle source
# File lib/lightly/cache_operations.rb, line 41
def enabled?
  @enabled ||= (@enabled.nil? ? true : @enabled)
end
flush() click to toggle source
# File lib/lightly/cache_operations.rb, line 50
def flush
  return false if dir == '/' || dir.empty?

  FileUtils.rm_rf dir
end
get(key) { || ... } click to toggle source
# File lib/lightly/cache_operations.rb, line 17
def get(key)
  return load key if cached?(key) && enabled?

  content = yield
  save key, content if content && enabled?
  content
end
get_path(key) click to toggle source
# File lib/lightly/cache_operations.rb, line 75
def get_path(key)
  key = Digest::MD5.hexdigest(key) if hash?
  File.join dir, key
end
hash?() click to toggle source
# File lib/lightly/cache_operations.rb, line 37
def hash?
  @hash ||= (@hash.nil? ? true : @hash)
end
life() click to toggle source
# File lib/lightly/cache_operations.rb, line 25
def life
  @life ||= 3600
end
life=(new_life) click to toggle source
# File lib/lightly/cache_operations.rb, line 29
def life=(new_life)
  @life = life_to_seconds new_life
end
prune() click to toggle source
# File lib/lightly/cache_operations.rb, line 56
def prune
  return false if dir == '/' || dir.empty?

  Dir["#{dir}/*"].each { |file| expired? file }
end
save(key, content) click to toggle source
# File lib/lightly/cache_operations.rb, line 80
def save(key, content)
  FileUtils.mkdir_p dir
  path = get_path key
  File.open path, 'wb', permissions do |file|
    file.write Marshal.dump(content)
  end
end

Private Instance Methods

expired?(path) click to toggle source
# File lib/lightly/cache_operations.rb, line 94
def expired?(path)
  expired = life >= 0 && File.exist?(path) && Time.now - File.mtime(path) >= life
  FileUtils.rm path if expired
  expired
end
life_to_seconds(arg) click to toggle source
# File lib/lightly/cache_operations.rb, line 100
def life_to_seconds(arg)
  arg = arg.to_s

  case arg[-1]
  when 's' then arg[0..].to_i
  when 'm' then arg[0..].to_i * 60
  when 'h' then arg[0..].to_i * 60 * 60
  when 'd' then arg[0..].to_i * 60 * 60 * 24
  else
    arg.to_i
  end
end
load(key) click to toggle source
# File lib/lightly/cache_operations.rb, line 90
def load(key)
  Marshal.load File.binread(get_path key)
end