Class: Persistent::StorageRAM

Inherits:
Persistent::Storage::API
  • Object
show all
Defined in:
lib/persistent-cache/storage_ram.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_details = nil) ⇒ StorageRAM

Returns a new instance of StorageRAM.



10
11
12
# File 'lib/persistent-cache/storage_ram.rb', line 10

def initialize(storage_details = nil)
  @storage = {}
end

Instance Attribute Details

#storageObject

Returns the value of attribute storage.



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

def storage
  @storage
end

Instance Method Details

#clearObject



48
49
50
51
52
# File 'lib/persistent-cache/storage_ram.rb', line 48

def clear
  EH.retry!(:args => []) do
    @storage.clear
  end
end

#delete_entry(serialized_key) ⇒ Object



29
30
31
32
33
# File 'lib/persistent-cache/storage_ram.rb', line 29

def delete_entry(serialized_key)
  EH.retry!(:args => [serialized_key]) do
    @storage.delete(serialized_key)
  end
end

#keysObject



41
42
43
44
45
46
# File 'lib/persistent-cache/storage_ram.rb', line 41

def keys
  EH.retry!(:args => []) do
    return [] if @storage.keys == []
    [@storage.keys]
  end
end

#lookup_key(serialized_key) ⇒ Object



22
23
24
25
26
27
# File 'lib/persistent-cache/storage_ram.rb', line 22

def lookup_key(serialized_key)
  EH.retry!(:args => [serialized_key]) do
    return [] if @storage[serialized_key].nil?
    [@storage[serialized_key][:value], @storage[serialized_key][:timestamp]]
  end
end

#save_key_value_pair(serialized_key, serialized_value, timestamp = nil) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/persistent-cache/storage_ram.rb', line 14

def save_key_value_pair(serialized_key, serialized_value, timestamp = nil)
  delete_entry(serialized_key)
  time_entry = timestamp.nil? ? Time.now.to_s : timestamp.to_s
  EH.retry!(:args => [serialized_key, serialized_value, time_entry]) do
    @storage[serialized_key] = {:value => serialized_value, :timestamp => time_entry}
  end
end

#sizeObject



35
36
37
38
39
# File 'lib/persistent-cache/storage_ram.rb', line 35

def size
  EH.retry!(:args => []) do
    @storage.size
  end
end