Class: Ramaze::Cache::Sequel
- Includes:
- Cache::API
- Defined in:
- lib/ramaze/cache/sequel.rb
Overview
Cache based on a Sequel model using relational databases.
Defined Under Namespace
Classes: Table
Instance Method Summary collapse
-
#cache_clear ⇒ Object
Wipe out all data in the table, use with care.
-
#cache_delete(*keys) ⇒ Object
Delete records for given
keys
. - #cache_fetch(key, default = nil) ⇒ Object
-
#cache_setup(host, user, app, name) ⇒ Object
Setup the table, not suitable for multiple apps yet.
- #cache_store(key, value, options = {}) ⇒ Object
- #namespaced(key) ⇒ Object
Instance Method Details
#cache_clear ⇒ Object
Wipe out all data in the table, use with care.
34 35 36 |
# File 'lib/ramaze/cache/sequel.rb', line 34 def cache_clear Table.delete_all end |
#cache_delete(*keys) ⇒ Object
Delete records for given keys
39 40 41 42 43 44 |
# File 'lib/ramaze/cache/sequel.rb', line 39 def cache_delete(*keys) super do |key| record = @store[:key => namespaced(key)] record.delete if record end end |
#cache_fetch(key, default = nil) ⇒ Object
46 47 48 |
# File 'lib/ramaze/cache/sequel.rb', line 46 def cache_fetch(key, default = nil) super{|key| @store[:key => namespaced(key)] } end |
#cache_setup(host, user, app, name) ⇒ Object
Setup the table, not suitable for multiple apps yet.
27 28 29 30 31 |
# File 'lib/ramaze/cache/sequel.rb', line 27 def cache_setup(host, user, app, name) @namespace = [host, user, app, name].compact.join(':') Table.create_table unless Table.table_exists? @store = Table end |
#cache_store(key, value, options = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ramaze/cache/sequel.rb', line 50 def cache_store(key, value, = {}) key = namespaced(key) ttl = [:ttl] expires = Time.now + ttl if ttl record = @store[:key => key].update(:value => value, :expires => expires) record.value rescue record = @store.create(:key => key, :value => value, :expires => expires) record.value end |
#namespaced(key) ⇒ Object
62 63 64 |
# File 'lib/ramaze/cache/sequel.rb', line 62 def namespaced(key) [@namespace, key].join(':') end |