class DTK::Client::DiskCacher

Class dedicated for caching data on local system as well as for cookie management

Constants

file name to hold cookies

FILE_DELIMITER

Public Class Methods

new(cache_dir = OsUtil.temp_dir) click to toggle source
# File lib/client/util/disk_cacher.rb, line 27
def initialize(cache_dir = OsUtil.temp_dir)
  @cache_dir = cache_dir
  @current_user = Configurator.client_username
end

Public Instance Methods

fetch(file_name, max_age = 0, use_mock_up = true) click to toggle source
# File lib/client/util/disk_cacher.rb, line 32
def fetch(file_name, max_age = 0, use_mock_up = true)
  file = Digest::MD5.hexdigest(file_name)
  # current user is important so that there are no clashes in writting to temp file
  # between multiple users on same machine
  file_path = File.join(@cache_dir, "#{@current_user}#{FILE_DELIMITER}#{file}")
  
  # we check if the file -- a MD5 hexdigest of the URL -- exists
  #  in the dir. If it does and the data is fresh, we just read
  #  data from the file and return
  if File.exists? file_path
    return File.new(file_path).read if ((Time.now - File.mtime(file_path)) < max_age)
  end
  
  # if the file does not exist (or if the data is not fresh), we
  #  make an get request and save it to a file
  response_string = ''
  response = Session.rest_get("metadata/get/#{file_name}")
  File.open(file_path, 'w') { |f| f << response_string = response.data } if response.ok? 
  response_string
end