# File lib/sequel/plugins/static_cache.rb 249 def primary_key_lookup(pk) 250 static_cache_object(cache[pk]) 251 end
module Sequel::Plugins::StaticCache::ClassMethods
Attributes
A frozen ruby hash holding all of the model’s frozen instances, keyed by frozen primary key.
Public Instance Methods
Source
# File lib/sequel/plugins/static_cache.rb 80 def all(&block) 81 array = @static_cache_frozen ? @all.dup : to_a 82 array.each(&block) if block 83 array 84 end
An array of all of the model’s instances, without issuing a database query. If a block is given, yields each instance to the block.
Source
# File lib/sequel/plugins/static_cache.rb 147 def as_hash(key_column = nil, value_column = nil, opts = OPTS) 148 if key_column.nil? && value_column.nil? 149 if @static_cache_frozen && !opts[:hash] 150 return Hash[cache] 151 else 152 key_column = primary_key 153 end 154 end 155 156 h = opts[:hash] || {} 157 if value_column 158 if value_column.is_a?(Array) 159 if key_column.is_a?(Array) 160 @all.each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)} 161 else 162 @all.each{|r| h[r[key_column]] = r.values.values_at(*value_column)} 163 end 164 else 165 if key_column.is_a?(Array) 166 @all.each{|r| h[r.values.values_at(*key_column)] = r[value_column]} 167 else 168 @all.each{|r| h[r[key_column]] = r[value_column]} 169 end 170 end 171 elsif key_column.is_a?(Array) 172 @all.each{|r| h[r.values.values_at(*key_column)] = static_cache_object(r)} 173 else 174 @all.each{|r| h[r[key_column]] = static_cache_object(r)} 175 end 176 h 177 end
Use the cache instead of a query to get the results.
Source
# File lib/sequel/plugins/static_cache.rb 180 def as_set(column) 181 set = Set.new 182 183 if column.is_a?(Array) 184 @all.each{|r| set.add(r.values.values_at(*column))} 185 else 186 @all.each{|r| set.add(r[column])} 187 end 188 189 set 190 end
Use the cache instead of a query to get the results.
Source
# File lib/sequel/plugins/static_cache.rb 111 def cache_get_pk(pk) 112 static_cache_object(cache[pk]) 113 end
Return the frozen object with the given pk, or nil if no such object exists in the cache, without issuing a database query.
Source
# File lib/sequel/plugins/static_cache.rb 101 def count(*a, &block) 102 if a.empty? && !block 103 @all.size 104 else 105 super 106 end 107 end
Get the number of records in the cache, without issuing a database query.
Source
# File lib/sequel/plugins/static_cache.rb 117 def each(&block) 118 if @static_cache_frozen 119 @all.each(&block) 120 else 121 @all.each{|o| yield(static_cache_object(o))} 122 end 123 end
Yield each of the model’s frozen instances to the block, without issuing a database query.
Source
# File lib/sequel/plugins/static_cache.rb 92 def first(*args) 93 if defined?(yield) || args.length > 1 || (args.length == 1 && !args[0].is_a?(Integer)) 94 super 95 else 96 @all.first(*args) 97 end 98 end
If a block is given, multiple arguments are given, or a single non-Integer argument is given, performs the default behavior of issuing a database query. Otherwise, uses the cached values to return either the first cached instance (no arguments) or an array containing the number of instances specified (single integer argument).
Source
# File lib/sequel/plugins/static_cache.rb 229 def load_cache 230 @all = load_static_cache_rows 231 h = {} 232 @all.each do |o| 233 o.errors.freeze 234 h[o.pk.freeze] = o.freeze 235 end 236 @cache = h.freeze 237 end
Reload the cache for this model by retrieving all of the instances in the dataset freezing them, and populating the cached array and hash.
Source
# File lib/sequel/plugins/static_cache.rb 126 def map(column=nil, &block) 127 if column 128 raise(Error, "Cannot provide both column and block to map") if block 129 if column.is_a?(Array) 130 @all.map{|r| r.values.values_at(*column)} 131 else 132 @all.map{|r| r[column]} 133 end 134 elsif @static_cache_frozen 135 @all.map(&block) 136 elsif block 137 @all.map{|o| yield(static_cache_object(o))} 138 else 139 all.map 140 end 141 end
Use the cache instead of a query to get the results.
Source
# File lib/sequel/plugins/static_cache.rb 223 def static_cache_allow_modifications? 224 !@static_cache_frozen 225 end
Ask whether modifications to this class are allowed.
Source
# File lib/sequel/plugins/static_cache.rb 193 def to_hash(*a) 194 as_hash(*a) 195 end
Alias of as_hash for backwards compatibility.
Source
# File lib/sequel/plugins/static_cache.rb 198 def to_hash_groups(key_column, value_column = nil, opts = OPTS) 199 h = opts[:hash] || {} 200 if value_column 201 if value_column.is_a?(Array) 202 if key_column.is_a?(Array) 203 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)} 204 else 205 @all.each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)} 206 end 207 else 208 if key_column.is_a?(Array) 209 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]} 210 else 211 @all.each{|r| (h[r[key_column]] ||= []) << r[value_column]} 212 end 213 end 214 elsif key_column.is_a?(Array) 215 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << static_cache_object(r)} 216 else 217 @all.each{|r| (h[r[key_column]] ||= []) << static_cache_object(r)} 218 end 219 h 220 end
Use the cache instead of a query to get the results
Private Instance Methods
Source
# File lib/sequel/plugins/static_cache.rb 242 def load_static_cache_rows 243 ret = super if defined?(super) 244 ret || dataset.all.freeze 245 end
Load the static cache rows from the database.
Source
Return the frozen object with the given pk, or nil if no such object exists in the cache, without issuing a database query.
Source
# File lib/sequel/plugins/static_cache.rb 256 def static_cache_object(o) 257 if @static_cache_frozen 258 o 259 elsif o 260 call(Hash[o.values]) 261 end 262 end
If frozen: false is not used, just return the argument. Otherwise, create a new instance with the arguments values if the argument is not nil.