Class: DiskCacher
- Inherits:
-
Object
- Object
- DiskCacher
- Extended by:
- DTK::Client::CommandBase
- Includes:
- DTK::Client::CommandBase
- Defined in:
- lib/config/disk_cacher.rb
Overview
Class dedicated for caching data on local system as well as for cookie management
Constant Summary collapse
- COOKIE_HOLDER_NAME =
file name to hold cookies
'tempdtkstore'
- FILE_DELIMITER =
'--'
Instance Method Summary collapse
- #fetch(file_name, max_age = 0, use_mock_up = true) ⇒ Object
-
#initialize(cache_dir = DTK::Client::OsUtil.get_temp_location) ⇒ DiskCacher
constructor
A new instance of DiskCacher.
- #load_cookie ⇒ Object
- #save_cookie(cookie_content) ⇒ Object
Methods included from DTK::Client::CommandBase
get, get_connection, handle_argument_error, post, post_file, rest_url, rotate_args
Constructor Details
#initialize(cache_dir = DTK::Client::OsUtil.get_temp_location) ⇒ DiskCacher
Returns a new instance of DiskCacher.
35 36 37 38 |
# File 'lib/config/disk_cacher.rb', line 35 def initialize(cache_dir = DTK::Client::OsUtil.get_temp_location) @cache_dir = cache_dir @current_user = ::DTK::Client::Configurator.client_username end |
Instance Method Details
#fetch(file_name, max_age = 0, use_mock_up = true) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/config/disk_cacher.rb', line 40 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 = get rest_url("metadata/get_metadata/#{file_name}") if (response["status"] == "ok") file = File.open(file_path, "w") do |data| data << response_string = response["data"] end end return response_string end |
#load_cookie ⇒ Object
74 75 76 77 78 |
# File 'lib/config/disk_cacher.rb', line 74 def file_path = File.join(@cache_dir, COOKIE_HOLDER_NAME) = File.exists?(file_path) ? File.open(file_path) {|f| Marshal.load(f)} : nil end |
#save_cookie(cookie_content) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/config/disk_cacher.rb', line 67 def () file_path = File.join(@cache_dir, COOKIE_HOLDER_NAME) File.open(file_path, "w") do |file| Marshal.dump(, file) end end |