module Sequel::Plugins::SubsetStaticCache::ClassMethods

Public Instance Methods

cache_subset(meth) click to toggle source

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.

Calls superclass method
   # 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

Private Instance Methods

clear_subset_static_caches() click to toggle source

Clear the subset_static_caches. This is used if the model dataset changes, to prevent cached values from being used.

   # File lib/sequel/plugins/subset_static_cache.rb
88 def clear_subset_static_caches
89   @subset_static_caches.clear
90 end
load_subset_static_cache_rows(ds, meth) click to toggle source

Return a frozen array for all rows in the dataset.

Calls superclass method
    # File lib/sequel/plugins/subset_static_cache.rb
120 def load_subset_static_cache_rows(ds, meth)
121   ret = super if defined?(super)
122   ret || ds.all.freeze
123 end
subset_static_cache_module() click to toggle source

A module for the subset static cache methods, so that you can define a singleton method in the class with the same name, and call super to get default behavior.

    # File lib/sequel/plugins/subset_static_cache.rb
 95 def subset_static_cache_module
 96   return @subset_static_cache_module if @subset_static_cache_module
 97 
 98   # Ensure dataset_methods module is defined and class is extended with
 99   # it before calling creating this module.
100   dataset_methods_module
101 
102   Sequel.synchronize{@subset_static_cache_module ||= Sequel.set_temp_name(Module.new){"#{name}::@subset_static_cache_module"}}
103   extend(@subset_static_cache_module)
104   @subset_static_cache_module
105 end
subset_static_cache_rows(ds, meth) click to toggle source

Return the frozen array and hash used for caching the subset of the given dataset.

    # File lib/sequel/plugins/subset_static_cache.rb
109 def subset_static_cache_rows(ds, meth)
110   all = load_subset_static_cache_rows(ds, meth)
111   h = {}
112   all.each do |o|
113     o.errors.freeze
114     h[o.pk.freeze] = o.freeze
115   end
116   [all, h.freeze]
117 end