Class: Entry
- Inherits:
-
Object
show all
- Extended by:
- Db
- Defined in:
- lib/cached.rb
Class Method Summary
collapse
Methods included from Db
ask, database, database=, db, log, row, sql
Class Method Details
.cleanup ⇒ Object
20
21
22
|
# File 'lib/cached.rb', line 20
def self.cleanup
ask "DELETE FROM cached WHERE valid_until < ?", now
end
|
.get(key) ⇒ Object
28
29
30
31
|
# File 'lib/cached.rb', line 28
def self.get(key)
value, valid_until = *row("SELECT value, valid_until FROM cached WHERE key=?", key)
return value if value && valid_until.to_i >= now
end
|
.init ⇒ Object
9
10
11
12
13
14
15
16
|
# File 'lib/cached.rb', line 9
def self.init
unless ask "SELECT name FROM sqlite_master WHERE type='table' AND name='cached'"
ask "CREATE TABLE cached(key, value, valid_until)"
end
ask "CREATE UNIQUE INDEX IF NOT EXISTS cached_key ON cached(key)"
ask "CREATE INDEX IF NOT EXISTS cached_valid_until ON cached(valid_until)"
end
|
.now ⇒ Object
24
25
26
|
# File 'lib/cached.rb', line 24
def self.now
Time.now.to_i
end
|
.set(key, value, options) ⇒ Object
33
34
35
|
# File 'lib/cached.rb', line 33
def self.set(key, value, options)
ask "INSERT OR REPLACE INTO cached (key, value, valid_until) VALUES(?, ?, ?)", key, value, now + options[:ttl]
end
|