module Sequel::Plugins::SubsetStaticCache::ClassMethods

  1. lib/sequel/plugins/subset_static_cache.rb

Methods

Public Instance

  1. cache_subset

Public Instance methods

cache_subset(meth)

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.

[show source]
   # File lib/sequel/plugins/subset_static_cache.rb
60 def cache_subset(meth)
61   ds = send(meth).with_extend(CachedDatasetMethods)
62   cache = ds.instance_variable_get(:@cache)
63 
64   rows, hash = subset_static_cache_rows(ds, meth)
65   cache[:subset_static_cache_all] = rows
66   cache[:subset_static_cache_map] = hash
67 
68   caches = @subset_static_caches
69   caches[meth] = ds
70   model = self
71   subset_static_cache_module.send(:define_method, meth) do
72     if (model == self) && (cached_dataset = caches[meth])
73       cached_dataset
74     else
75       super()
76     end
77   end
78   nil
79 end