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.
13 14 15 16 17 18 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 13 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.
10 11 12 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 10 def storage_details @storage_details end |
#storage_handler ⇒ Object
Returns the value of attribute storage_handler.
11 12 13 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 11 def storage_handler @storage_handler end |
Instance Method Details
#clear ⇒ Object
53 54 55 56 57 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 53 def clear EH::retry!(:args => []) do @storage_handler.execute("DELETE FROM #{DB_TABLE}") end end |
#delete_entry(key) ⇒ Object
35 36 37 38 39 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 35 def delete_entry(key) EH::retry!(:args => [serialize(key)]) do @storage_handler.execute("DELETE FROM #{DB_TABLE} WHERE key=?", serialize(key)) end end |
#keys ⇒ Object
47 48 49 50 51 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 47 def keys EH::retry!(:args => []) do @storage_handler.execute("SELECT key FROM #{DB_TABLE}").collect { |k| deserialize(k[0]) } end end |
#lookup_key(key) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 28 def lookup_key(key) EH::retry!(:args => [serialize(key)]) do result = @storage_handler.execute("SELECT value, timestamp FROM #{DB_TABLE} WHERE key=?", serialize(key)) !result.nil? && !result.empty? ? [deserialize(result[0][0]), result[0][1]] : nil end end |
#save_key_value_pair(key, value, timestamp = nil) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 20 def save_key_value_pair(key, value, = nil) delete_entry(key) time_entry = .nil? ? Time.now.to_s : .to_s EH::retry!(:args => [serialize(key), serialize(value), time_entry]) do @storage_handler.execute("INSERT INTO #{DB_TABLE} (key, value, timestamp) VALUES(?, ?, ?)",serialize(key), serialize(value), time_entry) end end |
#size ⇒ Object
41 42 43 44 45 |
# File 'lib/persistent-cache/storage/storage_sq_lite.rb', line 41 def size EH::retry!(:args => []) do @storage_handler.execute("SELECT value FROM #{DB_TABLE}").size end end |