module Sequel::Plugins::SubsetStaticCache::ClassMethods
Public Instance Methods
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
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
Private Instance Methods
Source
# File lib/sequel/plugins/subset_static_cache.rb 88 def clear_subset_static_caches 89 @subset_static_caches.clear 90 end
Clear the subset_static_caches. This is used if the model dataset changes, to prevent cached values from being used.
Source
# File lib/sequel/plugins/subset_static_cache.rb 121 def load_subset_static_cache_rows(ds, meth) 122 ret = super if defined?(super) 123 ret || ds.all.freeze 124 end
Return a frozen array for all rows in the dataset.
Calls superclass method
Source
# 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 mod_name = "#{name}::@subset_static_cache_module" 103 Sequel.synchronize{@subset_static_cache_module ||= Sequel.set_temp_name(Module.new){mod_name}} 104 extend(@subset_static_cache_module) 105 @subset_static_cache_module 106 end
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.
Source
# File lib/sequel/plugins/subset_static_cache.rb 110 def subset_static_cache_rows(ds, meth) 111 all = load_subset_static_cache_rows(ds, meth) 112 h = {} 113 all.each do |o| 114 o.errors.freeze 115 h[o.pk.freeze] = o.freeze 116 end 117 [all, h.freeze] 118 end
Return the frozen array and hash used for caching the subset of the given dataset.