Class: Persistent::StorageSQLite

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

Constant Summary collapse

DB_TABLE =
"key_value"
DB_TIMEOUT =
30000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_details) ⇒ StorageSQLite

Returns a new instance of StorageSQLite.

Raises:

  • (ArgumentError)


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_detailsObject

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_handlerObject

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

#clearObject



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

#keysObject



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, timestamp = nil)
  delete_entry(key)
  time_entry = timestamp.nil? ? Time.now.to_s : timestamp.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

#sizeObject



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