Class: Ramaze::Cache::Sequel

Inherits:
Object
  • Object
show all
Includes:
Cache::API
Defined in:
lib/ramaze/cache/sequel.rb

Overview

Cache based on a Sequel model using relational databases.

Please note that this cache might not work perfectly with Sequel 3.0.0, as the #[] and #[]= methods do not respect serialization. I’ll lobby for a change in that direction.

Defined Under Namespace

Classes: Table

Instance Method Summary collapse

Instance Method Details

#cache_clearObject

Wipe out all data in the table, use with care.



49
50
51
# File 'lib/ramaze/cache/sequel.rb', line 49

def cache_clear
  Table.delete
end

#cache_delete(*keys) ⇒ Object

Delete records for given keys



54
55
56
57
58
59
# File 'lib/ramaze/cache/sequel.rb', line 54

def cache_delete(*keys)
  super do |key|
    record = @store[:key => namespaced(key)]
    record.delete if record
  end
end

#cache_fetch(key, default = nil) ⇒ Object



61
62
63
# File 'lib/ramaze/cache/sequel.rb', line 61

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.



42
43
44
45
46
# File 'lib/ramaze/cache/sequel.rb', line 42

def cache_setup(host, user, app, name)
  @namespace = [host, user, app, name].compact.join(':')
  Table.create_table?
  @store = Table
end

#cache_store(key, value, options = {}) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ramaze/cache/sequel.rb', line 65

def cache_store(key, value, options = {})
  key = namespaced(key)
  ttl = options[: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



77
78
79
# File 'lib/ramaze/cache/sequel.rb', line 77

def namespaced(key)
  [@namespace, key].join(':')
end