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
-
#current_files(persist_category) ⇒ Array<String>
List all persisted files matching the given category prefix.
- #current_items(persist_category) ⇒ Object
-
#delete(object_id) ⇒ Object
Delete persisted item.
-
#garbage_collect(persist_category, max_age_seconds = nil) ⇒ Array<String>
Delete persisted items matching the given category, optionally older than a given age.
-
#get(object_id) ⇒ String?
Get value of persisted item.
-
#initialize(folder) ⇒ PersistencyFolder
constructor
A new instance of PersistencyFolder.
-
#put(object_id, value) ⇒ nil
Set value of persisted item.
Constructor Details
#initialize(folder) ⇒ PersistencyFolder
Returns a new instance of PersistencyFolder.
15 16 17 18 19 |
# File 'lib/aspera/persistency_folder.rb', line 15 def initialize(folder) @cache = {} @folder = folder Log.log.debug { "persistency folder: #{@folder}" } end |
Instance Method Details
#current_files(persist_category) ⇒ Array<String>
List all persisted files matching the given category prefix
84 85 86 |
# File 'lib/aspera/persistency_folder.rb', line 84 def current_files(persist_category) Dir[File.join(@folder, "#{persist_category}*#{FILE_SUFFIX}")] end |
#current_items(persist_category) ⇒ Object
88 89 90 |
# File 'lib/aspera/persistency_folder.rb', line 88 def current_items(persist_category) current_files(persist_category).to_h { |i| [File.basename(i, FILE_SUFFIX), File.read(i)] } end |
#delete(object_id) ⇒ Object
Delete persisted item
56 57 58 59 60 61 |
# File 'lib/aspera/persistency_folder.rb', line 56 def delete(object_id) persist_filepath = id_to_filepath(object_id) Log.log.debug { "persistency deleting: #{persist_filepath}" } FileUtils.rm_f(persist_filepath) @cache.delete(object_id) end |
#garbage_collect(persist_category, max_age_seconds = nil) ⇒ Array<String>
Delete persisted items matching the given category, optionally older than a given age
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/aspera/persistency_folder.rb', line 67 def garbage_collect(persist_category, max_age_seconds = nil) garbage_files = current_files(persist_category) 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 @cache.clear return garbage_files end |
#get(object_id) ⇒ String?
Get value of persisted item
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/aspera/persistency_folder.rb', line 24 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) ⇒ nil
Set value of persisted item
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/aspera/persistency_folder.rb', line 43 def put(object_id, value) Aspera.assert_type(value, String) persist_filepath = id_to_filepath(object_id) Log.log.debug { "persistency saving: #{persist_filepath}" } FileUtils.rm_f(persist_filepath) File.write(persist_filepath, value) Environment.restrict_file_access(persist_filepath) @cache[object_id] = value nil end |