Class: Moneta::Adapters::Sqlite
Overview
Instance Method Summary
collapse
Methods inherited from Base
#[], #[]=, #decrement, #fetch
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Sqlite
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/moneta/adapters/sqlite.rb', line 15
def initialize(options = {})
raise ArgumentError, 'Option :file is required' unless options[:file]
table = options[:table] || 'moneta'
@db = ::SQLite3::Database.new(options[:file])
@db.execute("create table if not exists #{table} (k blob not null primary key, v blob)")
@stmts =
[@select = @db.prepare("select v from #{table} where k = ?"),
@replace = @db.prepare("replace into #{table} values (?, ?)"),
@delete = @db.prepare("delete from #{table} where k = ?"),
@clear = @db.prepare("delete from #{table}")]
end
|
Instance Method Details
#clear(options = {}) ⇒ Object
51
52
53
54
|
# File 'lib/moneta/adapters/sqlite.rb', line 51
def clear(options = {})
@clear.execute!
self
end
|
#close ⇒ Object
56
57
58
59
60
|
# File 'lib/moneta/adapters/sqlite.rb', line 56
def close
@stmts.each {|s| s.close }
@db.close
nil
end
|
#delete(key, options = {}) ⇒ Object
41
42
43
44
45
|
# File 'lib/moneta/adapters/sqlite.rb', line 41
def delete(key, options = {})
value = load(key, options)
@delete.execute!(key)
value
end
|
#increment(key, amount = 1, options = {}) ⇒ Object
47
48
49
|
# File 'lib/moneta/adapters/sqlite.rb', line 47
def increment(key, amount = 1, options = {})
@db.transaction(:exclusive) { return super }
end
|
#key?(key, options = {}) ⇒ Boolean
27
28
29
|
# File 'lib/moneta/adapters/sqlite.rb', line 27
def key?(key, options = {})
!@select.execute!(key).empty?
end
|
#load(key, options = {}) ⇒ Object
31
32
33
34
|
# File 'lib/moneta/adapters/sqlite.rb', line 31
def load(key, options = {})
rows = @select.execute!(key)
rows.empty? ? nil : rows.first.first
end
|
#store(key, value, options = {}) ⇒ Object
36
37
38
39
|
# File 'lib/moneta/adapters/sqlite.rb', line 36
def store(key, value, options = {})
@replace.execute!(key, value)
value
end
|