Module: Sequel::Plugins::SubsetStaticCache
- Defined in:
- lib/sequel/plugins/subset_static_cache.rb
Overview
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.
Defined Under Namespace
Modules: CachedDatasetMethods, ClassMethods
Class Method Summary collapse
Class Method Details
.configure(model) ⇒ Object
46 47 48 49 50 |
# File 'lib/sequel/plugins/subset_static_cache.rb', line 46 def self.configure(model) model.class_exec do @subset_static_caches ||= ({}.compare_by_identity) end end |