Class: Fbe::Middleware::SqliteStore
- Inherits:
-
Object
- Object
- Fbe::Middleware::SqliteStore
- Defined in:
- lib/fbe/middleware/sqlite_store.rb
Overview
Persisted SQLite store for Faraday::HttpCache
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024-2025 Zerocracy
- License
-
MIT
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #all ⇒ Object
- #clear ⇒ Object
- #close ⇒ Object
- #delete(key) ⇒ Object
- #drop ⇒ Object
-
#initialize(path) ⇒ SqliteStore
constructor
A new instance of SqliteStore.
- #open ⇒ Object
- #prepare ⇒ Object
- #read(key) ⇒ Object
- #write(key, value) ⇒ Object
Constructor Details
#initialize(path) ⇒ SqliteStore
Returns a new instance of SqliteStore.
19 20 21 22 23 24 25 26 27 |
# File 'lib/fbe/middleware/sqlite_store.rb', line 19 def initialize(path) raise ArgumentError, 'Database path cannot be nil or empty' if path.nil? || path.empty? dir = File.dirname(path) raise ArgumentError, "Directory #{dir} does not exist" unless File.directory?(dir) @path = File.absolute_path(path) open prepare at_exit { close } end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
17 18 19 |
# File 'lib/fbe/middleware/sqlite_store.rb', line 17 def path @path end |
Instance Method Details
#all ⇒ Object
76 77 78 |
# File 'lib/fbe/middleware/sqlite_store.rb', line 76 def all perform { _1.execute('SELECT key, value FROM cache') } end |
#clear ⇒ Object
68 69 70 |
# File 'lib/fbe/middleware/sqlite_store.rb', line 68 def clear perform { _1.execute 'DELETE FROM cache;' } end |
#close ⇒ Object
62 63 64 65 66 |
# File 'lib/fbe/middleware/sqlite_store.rb', line 62 def close return if !@db || @db.closed? @db.close @db = nil end |
#delete(key) ⇒ Object
34 35 36 37 |
# File 'lib/fbe/middleware/sqlite_store.rb', line 34 def delete(key) perform { _1.execute('DELETE FROM cache WHERE key = ?', [key]) } nil end |
#drop ⇒ Object
72 73 74 |
# File 'lib/fbe/middleware/sqlite_store.rb', line 72 def drop perform { _1.execute 'DROP TABLE IF EXISTS cache;' } end |
#open ⇒ Object
50 51 52 53 |
# File 'lib/fbe/middleware/sqlite_store.rb', line 50 def open return if @db @db = SQLite3::Database.new(@path) end |
#prepare ⇒ Object
55 56 57 58 59 60 |
# File 'lib/fbe/middleware/sqlite_store.rb', line 55 def prepare perform do |tdb| tdb.execute 'CREATE TABLE IF NOT EXISTS cache(key TEXT UNIQUE NOT NULL, value TEXT);' tdb.execute 'CREATE INDEX IF NOT EXISTS key_idx ON cache(key);' end end |
#read(key) ⇒ Object
29 30 31 32 |
# File 'lib/fbe/middleware/sqlite_store.rb', line 29 def read(key) value = perform { _1.execute('SELECT value FROM cache WHERE key = ? LIMIT 1', [key]) }.dig(0, 0) JSON.parse(value) if value end |
#write(key, value) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/fbe/middleware/sqlite_store.rb', line 39 def write(key, value) value = JSON.dump(value) perform do |tdb| tdb.execute(<<~SQL, [key, value]) INSERT INTO cache(key, value) VALUES(?1, ?2) ON CONFLICT(key) DO UPDATE SET value = ?2 SQL end nil end |