Class: Aspera::PersistencyFolder

Inherits:
Object
  • Object
show all
Defined in:
lib/aspera/persistency_folder.rb

Overview

Persist data on file system

Instance Method Summary collapse

Constructor Details

#initialize(folder) ⇒ PersistencyFolder



14
15
16
17
# File 'lib/aspera/persistency_folder.rb', line 14

def initialize(folder)
  @cache={}
  set_folder(folder)
end

Instance Method Details

#delete(object_id) ⇒ Object



50
51
52
53
54
55
56
# 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

#flush_by_prefix(persist_category) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/aspera/persistency_folder.rb', line 58

def flush_by_prefix(persist_category)
  persist_files=Dir[File.join(@folder,persist_category+'*'+FILE_SUFFIX)]
  persist_files.each do |filepath|
    File.delete(filepath)
  end
  return persist_files
end

#get(object_id) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Object



41
42
43
44
45
46
47
48
# 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) ⇒ Object



19
20
21
22
# File 'lib/aspera/persistency_folder.rb', line 19

def set_folder(folder)
  @folder=folder
  Log.log.debug("persistency folder: #{@folder}")
end