Class: Persistent::StorageDirectory

Inherits:
Persistent::Storage::API
  • Object
show all
Defined in:
lib/persistent-cache/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_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_directory.rb', line 8

def storage_root
  @storage_root
end

Instance Method Details

#clearObject



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

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

#connect_to_databaseObject



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

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

#delete_entry(key) ⇒ Object



31
32
33
34
# File 'lib/persistent-cache/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



51
52
53
54
55
56
57
# File 'lib/persistent-cache/storage_directory.rb', line 51

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



59
60
61
# File 'lib/persistent-cache/storage_directory.rb', line 59

def get_value_path_even_when_not_cached(key)
  compile_value_path(key)
end

#key_cached?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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



40
41
42
43
# File 'lib/persistent-cache/storage_directory.rb', line 40

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

#lookup_key(key) ⇒ Object



25
26
27
28
29
# File 'lib/persistent-cache/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_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
# File 'lib/persistent-cache/storage_directory.rb', line 36

def size
  count = Dir::glob("#{@storage_root}/**/#{CACHE_FILE}").size
end