Class: SimpleCache::SqliteStore
- Inherits:
-
SqliteDatabase
- Object
- SqliteDatabase
- SimpleCache::SqliteStore
- Defined in:
- lib/simple_cache/sqlite_store.rb
Constant Summary collapse
- Marshal =
::SimpleCache::Marshal
- TABLE_NAME =
"simple_cache"
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ Object
- #fetch(key, &block) ⇒ Object
-
#initialize(name) ⇒ SqliteStore
constructor
A new instance of SqliteStore.
- #store(key, value, ttl = nil) ⇒ Object
Methods inherited from SqliteDatabase
Constructor Details
#initialize(name) ⇒ SqliteStore
Returns a new instance of SqliteStore.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/simple_cache/sqlite_store.rb', line 38 def initialize(name) @path = "#{SimpleCache::SqliteStore.base_dir}/#{name}/simple_cache.sqlite3" super @path begin ask("SELECT 1 FROM #{TABLE_NAME} LIMIT 1") rescue SQLite3::SQLException ask("CREATE TABLE #{TABLE_NAME}(uid INTEGER PRIMARY KEY, value TEXT, ttl INTEGER NOT NULL)") end end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
36 37 38 |
# File 'lib/simple_cache/sqlite_store.rb', line 36 def path @path end |
Class Method Details
.base_dir ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/simple_cache/sqlite_store.rb', line 28 def self.base_dir if RUBY_PLATFORM.downcase.include?("darwin") "#{Dir.home}/Library/Cache" else "#{Dir.home}/cache" end end |
Instance Method Details
#clear ⇒ Object
69 70 71 |
# File 'lib/simple_cache/sqlite_store.rb', line 69 def clear ask "DELETE FROM #{TABLE_NAME}" end |
#fetch(key, &block) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/simple_cache/sqlite_store.rb', line 49 def fetch(key, &block) value, ttl = ask("SELECT value, ttl FROM #{TABLE_NAME} WHERE uid=?", Marshal.uid(key)) if ttl && (ttl == 0 || ttl > Time.now.to_i) Marshal.unmarshal(value) elsif block_given? yield self, key end end |
#store(key, value, ttl = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/simple_cache/sqlite_store.rb', line 58 def store(key, value, ttl = nil) unless value.nil? ask("REPLACE INTO #{TABLE_NAME}(uid, value, ttl) VALUES(?,?,?)", Marshal.uid(key), Marshal.marshal(value), ttl ? ttl + Time.now.to_i : 0) else ask("DELETE FROM #{TABLE_NAME} WHERE uid=?", Marshal.uid(key)) end value end |