Module: Sequel::Plugins::SubsetStaticCache::ClassMethods

Defined in:
lib/sequel/plugins/subset_static_cache.rb

Instance Method Summary collapse

Instance Method Details

#cache_subset(meth) ⇒ Object

Cache the given subset statically, so that calling the subset method on the model will return a dataset that will return cached results instead of issuing database queries (assuming the cache has the necessary information).

The model must already respond to the given method before cache_subset is called.

[View source]

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sequel/plugins/subset_static_cache.rb', line 60

def cache_subset(meth)
  ds = send(meth).with_extend(CachedDatasetMethods)
  cache = ds.instance_variable_get(:@cache)

  rows, hash = subset_static_cache_rows(ds, meth)
  cache[:subset_static_cache_all] = rows
  cache[:subset_static_cache_map] = hash

  caches = @subset_static_caches
  caches[meth] = ds
  model = self
  subset_static_cache_module.send(:define_method, meth) do
    if (model == self) && (cached_dataset = caches[meth])
      cached_dataset
    else
      super()
    end
  end
  nil
end