class NicInfo::Cache

Public Class Methods

new(config) click to toggle source
# File lib/nicinfo/cache.rb, line 24
def initialize config
  @config = config
end

Public Instance Methods

clean() click to toggle source
# File lib/nicinfo/cache.rb, line 67
def clean
  cache_files = Dir::entries(@config.rdap_cache_dir)
  eviction = Time.now - @config.config[ NicInfo::CACHE ][ NicInfo::CACHE_EVICTION ]
  eviction_count = 0
  cache_files.each do |file|
    full_file_name = File.join(@config.rdap_cache_dir, file)
    if !file.start_with?(".") && (File.mtime(full_file_name) < eviction)
      @config.logger.trace("Evicting " + full_file_name)
      File::unlink(full_file_name)
      eviction_count += 1
    end
  end
  @config.logger.trace("Evicted " + eviction_count.to_s + " files from the cache")
  return eviction_count
end
count() click to toggle source
# File lib/nicinfo/cache.rb, line 98
def count
  count = 0
  cache_files = Dir::entries(@config.rdap_cache_dir)
  cache_files.each do |file|
    if !file.start_with?(".")
      count += 1
    end
  end
  return count
end
create(url, data) click to toggle source

creates an object in the cache. if the object already exists in the cache, this does nothing.

# File lib/nicinfo/cache.rb, line 40
def create url, data
  safe = NicInfo::make_safe(url)
  file_name = File.join(@config.rdap_cache_dir, safe)
  expiry = Time.now - @config.config[ NicInfo::CACHE ][ NicInfo::CACHE_EXPIRY ]
  return if (File.exist?(file_name) && File.mtime(file_name) > expiry)
  create_or_update(url, data)
end
create_or_update(url, data) click to toggle source

creates or updates an object in the cache

# File lib/nicinfo/cache.rb, line 29
def create_or_update url, data
  return nil if @config.config[ NicInfo::CACHE ][ NicInfo::USE_CACHE ] == false
  safe = NicInfo::make_safe(url)
  @config.logger.trace("Persisting " + url + " as " + safe)
  f = File.open(File.join(@config.rdap_cache_dir, safe), "w")
  f.puts data
  f.close
end
empty() click to toggle source
# File lib/nicinfo/cache.rb, line 83
def empty
  cache_files = Dir::entries(@config.rdap_cache_dir)
  eviction_count = 0
  cache_files.each do |file|
    full_file_name = File.join(@config.rdap_cache_dir, file)
    if !file.start_with?(".")
      @config.logger.trace("Evicting " + full_file_name)
      File::unlink(full_file_name)
      eviction_count += 1
    end
  end
  @config.logger.trace("Evicted " + eviction_count.to_s + " files from the cache")
  return eviction_count
end
get(url) click to toggle source
# File lib/nicinfo/cache.rb, line 48
def get url
  return nil if @config.config[ NicInfo::CACHE ][ NicInfo::USE_CACHE ] == false
  safe = NicInfo::make_safe(url)
  file_name = File.join(@config.rdap_cache_dir, safe)
  expiry = Time.now - @config.config[ NicInfo::CACHE ][ NicInfo::CACHE_EXPIRY ]
  if (File.exist?(file_name) && File.mtime(file_name) > expiry)
    @config.logger.trace("Getting " + url + " from cache.")
    f = File.open(file_name, "r")
    data = ''
    f.each_line do |line|
      data += line
    end
    f.close
    return data
  end
  #else
  return nil
end