Module: Sequel::Plugins::Cacheable::ClassMethods

Defined in:
lib/sequel-cacheable/class_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_driverObject (readonly)

Returns the value of attribute cache_driver.



6
7
8
# File 'lib/sequel-cacheable/class_methods.rb', line 6

def cache_driver
  @cache_driver
end

#cache_optionsObject (readonly)

Returns the value of attribute cache_options.



6
7
8
# File 'lib/sequel-cacheable/class_methods.rb', line 6

def cache_options
  @cache_options
end

#cachesObject (readonly)

Returns the value of attribute caches.



6
7
8
# File 'lib/sequel-cacheable/class_methods.rb', line 6

def caches
  @caches
end

Instance Method Details

#cache_clear(type) ⇒ Object



69
70
71
# File 'lib/sequel-cacheable/class_methods.rb', line 69

def cache_clear(type)
  @caches[type].dup.each {|key| cache_del(key) }
end

#cache_del(key) ⇒ Object



64
65
66
67
# File 'lib/sequel-cacheable/class_methods.rb', line 64

def cache_del(key)
  @caches[key.match(/\AQuery:/) ? :query : :instance].delete(key)
  cache_driver.del(cache_key(key))
end

#cache_fetch(key, ttl = , &block) ⇒ Object



59
60
61
62
# File 'lib/sequel-cacheable/class_methods.rb', line 59

def cache_fetch(key, ttl = @cache_options[:ttl], &block)
  @caches[key.match(/\AQuery:/) ? :query : :instance] << key
  cache_driver.fetch(cache_key(key), ttl, &block)
end

#cache_get(key) ⇒ Object



55
56
57
# File 'lib/sequel-cacheable/class_methods.rb', line 55

def cache_get(key)
  restore_by_cache(cache_driver.get(cache_key(key)))
end

#cache_key(key) ⇒ Object



46
47
48
# File 'lib/sequel-cacheable/class_methods.rb', line 46

def cache_key(key)
  "#{self.name}:#{key}"
end

#cache_set(key, value, ttl = ) ⇒ Object



50
51
52
53
# File 'lib/sequel-cacheable/class_methods.rb', line 50

def cache_set(key, value, ttl = @cache_options[:ttl])
  @caches[key.match(/\AQuery:/) ? :query : :instance] << key
  cache_driver.set(cache_key(key), value, ttl)
end

#inherited(subclass) ⇒ Object



8
9
10
# File 'lib/sequel-cacheable/class_methods.rb', line 8

def inherited(subclass)
  super
end

#primary_key_lookup(id) ⇒ Object



12
13
14
15
16
# File 'lib/sequel-cacheable/class_methods.rb', line 12

def primary_key_lookup(id)
  cache_fetch(id.to_s) do
    super
  end
end

#restore_by_cache(hash) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sequel-cacheable/class_methods.rb', line 18

def restore_by_cache(hash)
  return nil if hash.nil?

  return hash.map{|hs| restore_by_cache(hs) } if hash.is_a?(Array)

  return hash if hash.kind_of?(Sequel::Model)

  hash.keys.each do | key |
    value = hash.delete(key)
    key = key.to_sym rescue key
    case db_schema[key][:type]
    when :date
      value = Date.new(*value)
    when :time
      value = Sequel::SQLTime.at(value[0], value[1])
    when :datetime
      value = Time.at(value[0], value[1])
    when :decimal
      value = BigDecimal.new(value)
    when :integer
      value = value.to_i
    end
    hash[key] = value
  end

  return new(hash, true)
end