Class: Persistent::StorageSQLite
- Inherits:
-
Object
- Object
- Persistent::StorageSQLite
- Defined in:
- lib/persistent-cache/storage/storage_sq_lite.rb
Constant Summary collapse
- DB_TABLE =
"key_value"- DB_TIMEOUT =
30000
Instance Attribute Summary collapse
-
#storage_details ⇒ Object
Returns the value of attribute storage_details.
-
#storage_handler ⇒ Object
Returns the value of attribute storage_handler.
Instance Method Summary collapse
- #clear ⇒ Object
- #delete_entry(key) ⇒ Object
-
#initialize(storage_details) ⇒ StorageSQLite
constructor
A new instance of StorageSQLite.
- #keys ⇒ Object
- #lookup_key(key) ⇒ Object
- #save_key_value_pair(key, value, timestamp = nil) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(storage_details) ⇒ StorageSQLite
Returns a new instance of StorageSQLite.
12 13 14 15 16 17 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 12 def initialize(storage_details) raise ArgumentError.new("Storage details not provided") if storage_details.nil? or storage_details == "" @storage_details = storage_details @storage_handler = connect_to_database @storage_handler.busy_timeout = 30000 end |
Instance Attribute Details
#storage_details ⇒ Object
Returns the value of attribute storage_details.
9 10 11 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 9 def storage_details @storage_details end |
#storage_handler ⇒ Object
Returns the value of attribute storage_handler.
10 11 12 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 10 def storage_handler @storage_handler end |
Instance Method Details
#clear ⇒ Object
52 53 54 55 56 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 52 def clear EH::retry!(:args => []) do @storage_handler.execute("DELETE FROM #{DB_TABLE}") end end |
#delete_entry(key) ⇒ Object
34 35 36 37 38 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 34 def delete_entry(key) EH::retry!(:args => [Marshal.dump(key)]) do @storage_handler.execute("DELETE FROM #{DB_TABLE} WHERE key=?", Marshal.dump(key)) end end |
#keys ⇒ Object
46 47 48 49 50 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 46 def keys EH::retry!(:args => []) do @storage_handler.execute("SELECT key FROM #{DB_TABLE}").collect { |k| Marshal.load(k[0]) } end end |
#lookup_key(key) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 27 def lookup_key(key) EH::retry!(:args => [Marshal.dump(key)]) do result = @storage_handler.execute("SELECT value, timestamp FROM #{DB_TABLE} WHERE key=?", Marshal.dump(key)) !result.nil? && !result.empty? ? [Marshal.load(result[0][0]), result[0][1]] : nil end end |
#save_key_value_pair(key, value, timestamp = nil) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 19 def save_key_value_pair(key, value, = nil) delete_entry(key) time_entry = .nil? ? Time.now.to_s : .to_s EH::retry!(:args => [Marshal.dump(key), Marshal.dump(value), time_entry]) do @storage_handler.execute("INSERT INTO #{DB_TABLE} (key, value, timestamp) VALUES(?, ?, ?)",Marshal.dump(key), Marshal.dump(value), time_entry) end end |
#size ⇒ Object
40 41 42 43 44 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 40 def size EH::retry!(:args => []) do @storage_handler.execute("SELECT value FROM #{DB_TABLE}").size end end |