class Aspera::PersistencyFolder
Persist data on file system
Constants
- FILE_SUFFIX
- ID_SEPARATOR
- PROTECTED_CHAR_REPLACE
- WINDOWS_PROTECTED_CHAR
Public Class Methods
new(folder)
click to toggle source
# File lib/aspera/persistency_folder.rb, line 14 def initialize(folder) @cache={} set_folder(folder) end
Public Instance Methods
delete(object_id)
click to toggle source
# File lib/aspera/persistency_folder.rb, line 50 def delete(object_id) object_id=marshalled_id(object_id) persist_filepath=id_to_filepath(object_id) Log.log.debug("empty data, deleting: #{persist_filepath}") File.delete(persist_filepath) if File.exist?(persist_filepath) @cache.delete(object_id) end
garbage_collect(persist_category,max_age_seconds=nil)
click to toggle source
# File lib/aspera/persistency_folder.rb, line 58 def garbage_collect(persist_category,max_age_seconds=nil) garbage_files=Dir[File.join(@folder,persist_category+'*'+FILE_SUFFIX)] if !max_age_seconds.nil? current_time = Time.now garbage_files.select! { |filepath| (current_time - File.stat(filepath).mtime).to_i > max_age_seconds} end garbage_files.each do |filepath| File.delete(filepath) Log.log.debug("Deleted expired: #{filepath}") end return garbage_files end
get(object_id)
click to toggle source
@return String
or nil string on existing persist, else nil
# File lib/aspera/persistency_folder.rb, line 25 def get(object_id) object_id=marshalled_id(object_id) Log.log.debug("persistency get: #{object_id}") if @cache.has_key?(object_id) Log.log.debug("got from memory cache") else persist_filepath=id_to_filepath(object_id) Log.log.debug("persistency = #{persist_filepath}") if File.exist?(persist_filepath) Log.log.debug("got from file cache") @cache[object_id]=File.read(persist_filepath) end end return @cache[object_id] end
put(object_id,value)
click to toggle source
# File lib/aspera/persistency_folder.rb, line 41 def put(object_id,value) raise "only String supported" unless value.is_a?(String) object_id=marshalled_id(object_id) persist_filepath=id_to_filepath(object_id) Log.log.debug("saving: #{persist_filepath}") File.write(persist_filepath,value) @cache[object_id]=value end
set_folder(folder)
click to toggle source
# File lib/aspera/persistency_folder.rb, line 19 def set_folder(folder) @folder=folder Log.log.debug("persistency folder: #{@folder}") end
Private Instance Methods
id_to_filepath(object_id)
click to toggle source
@param object_id String
or Array
# File lib/aspera/persistency_folder.rb, line 74 def id_to_filepath(object_id) FileUtils.mkdir_p(@folder) return File.join(@folder,"#{object_id}#{FILE_SUFFIX}") #.gsub(/[^a-z]+/,FILE_FIELD_SEPARATOR) end
marshalled_id(object_id)
click to toggle source
# File lib/aspera/persistency_folder.rb, line 80 def marshalled_id(object_id) if object_id.is_a?(Array) # special case, url in second position: TODO: check any position if object_id[1].is_a?(String) and object_id[1] =~ URI::ABS_URI object_id=object_id.clone object_id[1]=URI.parse(object_id[1]).host end object_id=object_id.join(ID_SEPARATOR) end raise "id must be a String" unless object_id.is_a?(String) return object_id. gsub(WINDOWS_PROTECTED_CHAR,PROTECTED_CHAR_REPLACE). # remove windows forbidden chars gsub('.',PROTECTED_CHAR_REPLACE). # keep dot for extension only (nicer) downcase end