Class: Aspera::PersistencyFolder
- Inherits:
-
Object
- Object
- Aspera::PersistencyFolder
- Defined in:
- lib/aspera/persistency_folder.rb
Overview
Persist data on file system
Instance Method Summary collapse
- #delete(object_id) ⇒ Object
- #garbage_collect(persist_category, max_age_seconds = nil) ⇒ Object
-
#get(object_id) ⇒ Object
String or nil string on existing persist, else nil.
-
#initialize(folder) ⇒ PersistencyFolder
constructor
A new instance of PersistencyFolder.
- #put(object_id, value) ⇒ Object
Constructor Details
#initialize(folder) ⇒ PersistencyFolder
Returns a new instance of PersistencyFolder.
14 15 16 17 18 |
# File 'lib/aspera/persistency_folder.rb', line 14 def initialize(folder) @cache = {} @folder = folder Log.log.debug{"persistency folder: #{@folder}"} end |
Instance Method Details
#delete(object_id) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/aspera/persistency_folder.rb', line 46 def delete(object_id) persist_filepath = id_to_filepath(object_id) Log.log.debug{"persistency 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) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/aspera/persistency_folder.rb', line 53 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{"persistency deleted expired: #{filepath}"} end return garbage_files end |
#get(object_id) ⇒ Object
Returns String or nil string on existing persist, else nil.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/aspera/persistency_folder.rb', line 21 def get(object_id) Log.log.debug{"persistency get: #{object_id}"} if @cache.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) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/aspera/persistency_folder.rb', line 36 def put(object_id, value) raise 'value: only String supported' unless value.is_a?(String) persist_filepath = id_to_filepath(object_id) Log.log.debug{"persistency saving: #{persist_filepath}"} File.delete(persist_filepath) if File.exist?(persist_filepath) File.write(persist_filepath, value) Environment.restrict_file_access(persist_filepath) @cache[object_id] = value end |