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

Defined in:
lib/sequel-cacheable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_optionsObject (readonly)

Returns the value of attribute cache_options.



30
31
32
# File 'lib/sequel-cacheable.rb', line 30

def cache_options
  @cache_options
end

#cache_storeObject (readonly)

Returns the value of attribute cache_store.



28
29
30
# File 'lib/sequel-cacheable.rb', line 28

def cache_store
  @cache_store
end

#cache_store_typeObject (readonly)

Returns the value of attribute cache_store_type.



29
30
31
# File 'lib/sequel-cacheable.rb', line 29

def cache_store_type
  @cache_store_type
end

Instance Method Details

#cache_del(key) ⇒ Object



91
92
93
# File 'lib/sequel-cacheable.rb', line 91

def cache_del(key)
  cache_store.send(cache_store_type.delete_method, key)
end

#cache_get(key) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sequel-cacheable.rb', line 59

def cache_get(key)
  if cache_options.ignore_exceptions?
    obj = cache_store.get(key) rescue nil
  else
    obj = cache_store.get(key)
  end

  if obj && cache_options.pack_lib?
    obj = restore_cache(cache_options.pack_lib.unpack(obj))
  end

  obj
end

#cache_mget(*keys) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sequel-cacheable.rb', line 73

def cache_mget(*keys)
  if cache_options.ignore_exceptions?
    objs = cache_store.mget(*keys) rescue nil
  else
    objs = cache_store.mget(*keys)
  end

  if objs && cache_options.pack_lib?
    objs.map!{|obj| 
      key = keys.shift
      (obj && restore_cache(cache_options.pack_lib.unpack(obj))) ||
        model[key.sub(/^#{model}::/, '')]
    }
  end

  objs || []
end

#cache_set(key, obj, ttl = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sequel-cacheable.rb', line 42

def cache_set(key, obj, ttl = nil)
  return obj if obj.nil?

  ttl = ttl || cache_options.ttl
  if cache_options.pack_lib?
    obj = obj.map{|o| o.id } if obj.kind_of?(Array)
    obj = cache_options.pack_lib.pack(obj)
  end

  args = [key, obj]
  args << ttl if cache_store_type.set_with_ttl?
  cache_store.set(*args)
  unless cache_store_type.set_with_ttl?
    cache_store.expire(key, ttl)
  end
end

#cache_set_get(key, ttl = nil) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/sequel-cacheable.rb', line 95

def cache_set_get(key, ttl = nil)
  if (val = cache_get(key)).nil?
    val = yield
    cache_set(key, val, ttl)
  end
  val
end

#clear_query_cacheObject



128
129
130
131
132
133
# File 'lib/sequel-cacheable.rb', line 128

def clear_query_cache
  return unless cache_options.query_cache?
  cache_store.keys("#{model.name}::Query::*").each do | key |
    cache_del(key)
  end
end

#inherited(subclass) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/sequel-cacheable.rb', line 32

def inherited(subclass)
  super
  cache = [@cache_store, @cache_store_type, @cache_options]
  subclass.instance_eval do
    @cache_store = cache[0]
    @cache_store_type = cache[1]
    @cache_options = cache[2]
  end
end

#primary_key_lookup(key) ⇒ Object



135
136
137
138
139
# File 'lib/sequel-cacheable.rb', line 135

def primary_key_lookup(key)
  cache_set_get("#{model}::#{key}") do
    super(key)
  end
end

#restore_cache(object) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sequel-cacheable.rb', line 103

def restore_cache(object)
  return object if object.nil?

  return cache_mget(*object.map{|id| "#{model}::#{id}" }) if object.kind_of?(Array)

  object.keys.each do | key |
    value = object.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
    object[key] = value
  end
  new(object, true)
end