Class: Persistent::StorageDirectory

Inherits:
Object
  • Object
show all
Defined in:
lib/persistent-cache/storage/storage_directory.rb

Constant Summary collapse

CACHE_FILE =
"cache.gz"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_details) ⇒ StorageDirectory

Returns a new instance of StorageDirectory.

Raises:

  • (ArgumentError)


10
11
12
13
14
# File 'lib/persistent-cache/storage/storage_directory.rb', line 10

def initialize(storage_details)
  raise ArgumentError.new("Storage details not provided") if storage_details.nil? or storage_details == ""
  @storage_root = storage_details
  connect_to_database
end

Instance Attribute Details

#storage_rootObject

Returns the value of attribute storage_root.



8
9
10
# File 'lib/persistent-cache/storage/storage_directory.rb', line 8

def storage_root
  @storage_root
end

Instance Method Details

#clearObject



50
51
52
53
54
# File 'lib/persistent-cache/storage/storage_directory.rb', line 50

def clear
  keys.each do |key|
    delete_entry(key[0])
  end
end

#connect_to_databaseObject



16
17
18
# File 'lib/persistent-cache/storage/storage_directory.rb', line 16

def connect_to_database
  FileUtils.makedirs([@storage_root]) if not File.exists?(@storage_root)
end

#delete_entry(key) ⇒ Object



31
32
33
34
# File 'lib/persistent-cache/storage/storage_directory.rb', line 31

def delete_entry(key)
  validate_key(key)
  FileUtils.rm_rf(compile_key_path(key))
end

#get_value_path(key) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/persistent-cache/storage/storage_directory.rb', line 56

def get_value_path(key)
  validate_key(key)

  return nil if not key_cached?(key)

  compile_value_path(key)
end

#get_value_path_even_when_not_cached(key) ⇒ Object



64
65
66
# File 'lib/persistent-cache/storage/storage_directory.rb', line 64

def get_value_path_even_when_not_cached(key)
  compile_value_path(key)
end

#key_cached?(key) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/persistent-cache/storage/storage_directory.rb', line 68

def key_cached?(key)
  # don't read the value here, as it may be very large - rather look whether the key is present
  File.exists? compile_key_path(key)
end

#keysObject



45
46
47
48
# File 'lib/persistent-cache/storage/storage_directory.rb', line 45

def keys
  return [] if size == 0
  list_keys_sorted
end

#lookup_key(key) ⇒ Object



25
26
27
28
29
# File 'lib/persistent-cache/storage/storage_directory.rb', line 25

def lookup_key(key)
  validate_key(key)
  return [] if not File.exists? compile_value_path(key)
  lookup_key_value_timestamp(key)
end

#save_key_value_pair(key, value, timestamp = nil) ⇒ Object



20
21
22
23
# File 'lib/persistent-cache/storage/storage_directory.rb', line 20

def save_key_value_pair(key, value, timestamp = nil)
  prepare_to_store_key_value(key, value, timestamp)
  store_key_value(key, value, get_time(timestamp)) if not value.nil?
end

#sizeObject



36
37
38
39
40
41
42
43
# File 'lib/persistent-cache/storage/storage_directory.rb', line 36

def size
  count = Dir::glob("#{@storage_root}/**/").size
  # if the directory does not exist, count == 0, which is what we want
  return 0 if count == 0
  # if the directory does exist, but is empty, count == 1, namely the directory itself, and we want to return 0 (i.e. count - 1)
  # if the directory does exist and includes subdirectories, the directory itself is still counted as well, and we want to return count - 1
  return count - 1
end