The subset_static_cache plugin is designed for model subsets that are not modified at all in production use cases, or at least where modifications to them would usually coincide with an application restart. When caching a model subset, it retrieves all rows in the database and statically caches a ruby array and hash keyed on primary key containing all of the model instances. All of these cached instances are frozen so they won’t be modified unexpectedly.
With the following code:
class StatusType < Sequel::Model dataset_module do where :available, hidden: false end cache_subset :available end
The following methods will use the cache and not issue a database query:
-
StatusType.available.with_pk
-
StatusType.available.all
-
StatusType.available.each
-
StatusType.available.first (without block, only supporting no arguments or single integer argument)
-
StatusType.available.count (without an argument or block)
-
StatusType.available.map
-
StatusType.available.as_hash
-
StatusType.available.to_hash
-
StatusType.available.to_hash_groups
The cache is not used if you chain methods before or after calling the cached method, as doing so would not be safe:
StatusType.where{number > 1}.available.all StatusType.available.where{number > 1}.all
The cache is also not used if you change the class’s dataset after caching the subset, or in subclasses of the model.
You should not modify any row that is statically cached when using this plugin, as otherwise you will get different results for cached and uncached method calls.
Classes and Modules
Public Class methods
# File lib/sequel/plugins/subset_static_cache.rb 46 def self.configure(model) 47 model.class_exec do 48 @subset_static_caches ||= ({}.compare_by_identity) 49 end 50 end