Class: Moneta::Adapters::Sqlite
- Inherits:
-
Object
- Object
- Moneta::Adapters::Sqlite
- Includes:
- Defaults, IncrementSupport
- Defined in:
- lib/moneta/adapters/sqlite.rb
Overview
Sqlite3 backend
Instance Attribute Summary collapse
- #backend ⇒ Object readonly
Instance Method Summary collapse
-
#clear(options = {}) ⇒ void
Clear all keys in this store.
-
#close ⇒ Object
Explicitly close the store.
- #create(key, value, options = {}) ⇒ Object
-
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value.
-
#increment(key, amount = 1, options = {}) ⇒ Object
Atomically increment integer value with key.
-
#initialize(options = {}) ⇒ Sqlite
constructor
A new instance of Sqlite.
-
#key?(key, options = {}) ⇒ Boolean
Exists the value with key.
-
#load(key, options = {}) ⇒ Object
Fetch value with key.
-
#store(key, value, options = {}) ⇒ Object
Store value with key.
Methods included from IncrementSupport
Methods included from Defaults
#[], #[]=, #decrement, #each_key, #features, #fetch, #fetch_values, included, #merge!, #slice, #supports?, #values_at
Methods included from OptionSupport
#expires, #prefix, #raw, #with
Constructor Details
#initialize(options = {}) ⇒ Sqlite
Returns a new instance of Sqlite
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/moneta/adapters/sqlite.rb', line 20 def initialize( = {}) table = [:table] || 'moneta' @backend = [:backend] || begin raise ArgumentError, 'Option :file is required' unless [:file] ::SQLite3::Database.new([:file]) end @backend.busy_timeout([:busy_timeout] || 1000) @backend.execute("create table if not exists #{table} (k blob not null primary key, v blob)") if journal_mode = [:journal_mode] @backend.journal_mode = journal_mode.to_s end @stmts = [@exists = @backend.prepare("select exists(select 1 from #{table} where k = ?)"), @select = @backend.prepare("select v from #{table} where k = ?"), @replace = @backend.prepare("replace into #{table} values (?, ?)"), @delete = @backend.prepare("delete from #{table} where k = ?"), @clear = @backend.prepare("delete from #{table}"), @create = @backend.prepare("insert into #{table} values (?, ?)")] end |
Instance Attribute Details
#backend ⇒ Object (readonly)
12 13 14 |
# File 'lib/moneta/adapters/sqlite.rb', line 12 def backend @backend end |
Instance Method Details
#clear(options = {}) ⇒ void
This method returns an undefined value.
Clear all keys in this store
71 72 73 74 |
# File 'lib/moneta/adapters/sqlite.rb', line 71 def clear( = {}) @clear.execute! self end |
#close ⇒ Object
Explicitly close the store
88 89 90 91 92 |
# File 'lib/moneta/adapters/sqlite.rb', line 88 def close @stmts.each {|s| s.close } @backend.close nil end |
#create(key, value, options = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/moneta/adapters/sqlite.rb', line 77 def create(key, value, = {}) @create.execute!(key,value) true rescue SQLite3::ConstraintException # If you know a better way to detect whether an insert-ignore # suceeded, please tell me. @create.reset! false end |
#delete(key, options = {}) ⇒ Object
Delete the key from the store and return the current value
59 60 61 62 63 |
# File 'lib/moneta/adapters/sqlite.rb', line 59 def delete(key, = {}) value = load(key, ) @delete.execute!(key) value end |
#increment(key, amount = 1, options = {}) ⇒ Object
Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.
Atomically increment integer value with key
This method also accepts negative amounts.
66 67 68 |
# File 'lib/moneta/adapters/sqlite.rb', line 66 def increment(key, amount = 1, = {}) @backend.transaction(:exclusive) { return super } end |
#key?(key, options = {}) ⇒ Boolean
Exists the value with key
42 43 44 |
# File 'lib/moneta/adapters/sqlite.rb', line 42 def key?(key, = {}) @exists.execute!(key).first.first.to_i == 1 end |
#load(key, options = {}) ⇒ Object
Fetch value with key. Return nil if the key doesn't exist
47 48 49 50 |
# File 'lib/moneta/adapters/sqlite.rb', line 47 def load(key, = {}) rows = @select.execute!(key) rows.empty? ? nil : rows.first.first end |
#store(key, value, options = {}) ⇒ Object
Store value with key
53 54 55 56 |
# File 'lib/moneta/adapters/sqlite.rb', line 53 def store(key, value, = {}) @replace.execute!(key, value) value end |