Module: Sequel::Plugins::Cacheable::ClassMethods
- Defined in:
- lib/sequel-cacheable/class_methods.rb
Instance Attribute Summary collapse
-
#cache_driver ⇒ Object
readonly
Returns the value of attribute cache_driver.
-
#cache_options ⇒ Object
readonly
Returns the value of attribute cache_options.
-
#caches ⇒ Object
readonly
Returns the value of attribute caches.
Instance Method Summary collapse
- #cache_clear(type) ⇒ Object
- #cache_del(key) ⇒ Object
- #cache_fetch(key, ttl = , &block) ⇒ Object
- #cache_get(key) ⇒ Object
- #cache_key(key) ⇒ Object
- #cache_set(key, value, ttl = ) ⇒ Object
- #inherited(subclass) ⇒ Object
- #primary_key_lookup(id) ⇒ Object
- #restore_by_cache(hash) ⇒ Object
Instance Attribute Details
#cache_driver ⇒ Object (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_options ⇒ Object (readonly)
Returns the value of attribute cache_options.
6 7 8 |
# File 'lib/sequel-cacheable/class_methods.rb', line 6 def end |
#caches ⇒ Object (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 = [: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 = [: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 |