Class: Persistent::StorageSQLite
- Inherits:
-
Persistent::Storage::API
- Object
- Persistent::Storage::API
- Persistent::StorageSQLite
- Defined in:
- lib/persistent-cache/storage_sqlite.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.
15 16 17 18 19 20 |
# File 'lib/persistent-cache/storage_sqlite.rb', line 15 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.
12 13 14 |
# File 'lib/persistent-cache/storage_sqlite.rb', line 12 def storage_details @storage_details end |
#storage_handler ⇒ Object
Returns the value of attribute storage_handler.
13 14 15 |
# File 'lib/persistent-cache/storage_sqlite.rb', line 13 def storage_handler @storage_handler end |
Instance Method Details
#clear ⇒ Object
55 56 57 58 59 |
# File 'lib/persistent-cache/storage_sqlite.rb', line 55 def clear EH::retry!(:args => []) do @storage_handler.execute("DELETE FROM #{DB_TABLE}") end end |
#delete_entry(key) ⇒ Object
37 38 39 40 41 |
# File 'lib/persistent-cache/storage_sqlite.rb', line 37 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
49 50 51 52 53 |
# File 'lib/persistent-cache/storage_sqlite.rb', line 49 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
30 31 32 33 34 35 |
# File 'lib/persistent-cache/storage_sqlite.rb', line 30 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
22 23 24 25 26 27 28 |
# File 'lib/persistent-cache/storage_sqlite.rb', line 22 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
43 44 45 46 47 |
# File 'lib/persistent-cache/storage_sqlite.rb', line 43 def size EH::retry!(:args => []) do @storage_handler.execute("SELECT value FROM #{DB_TABLE}").size end end |