Methods
Public Instance
Attributes
cache | [R] |
A frozen ruby hash holding all of the model’s frozen instances, keyed by frozen primary key. |
Public Instance methods
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.
# File lib/sequel/plugins/static_cache.rb 77 def all(&block) 78 array = @static_cache_frozen ? @all.dup : to_a 79 array.each(&block) if block 80 array 81 end
Use the cache instead of a query to get the results.
# File lib/sequel/plugins/static_cache.rb 144 def as_hash(key_column = nil, value_column = nil, opts = OPTS) 145 if key_column.nil? && value_column.nil? 146 if @static_cache_frozen && !opts[:hash] 147 return Hash[cache] 148 else 149 key_column = primary_key 150 end 151 end 152 153 h = opts[:hash] || {} 154 if value_column 155 if value_column.is_a?(Array) 156 if key_column.is_a?(Array) 157 @all.each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)} 158 else 159 @all.each{|r| h[r[key_column]] = r.values.values_at(*value_column)} 160 end 161 else 162 if key_column.is_a?(Array) 163 @all.each{|r| h[r.values.values_at(*key_column)] = r[value_column]} 164 else 165 @all.each{|r| h[r[key_column]] = r[value_column]} 166 end 167 end 168 elsif key_column.is_a?(Array) 169 @all.each{|r| h[r.values.values_at(*key_column)] = static_cache_object(r)} 170 else 171 @all.each{|r| h[r[key_column]] = static_cache_object(r)} 172 end 173 h 174 end
Return the frozen object with the given pk, or nil if no such object exists in the cache, without issuing a database query.
# File lib/sequel/plugins/static_cache.rb 108 def cache_get_pk(pk) 109 static_cache_object(cache[pk]) 110 end
Get the number of records in the cache, without issuing a database query.
# File lib/sequel/plugins/static_cache.rb 98 def count(*a, &block) 99 if a.empty? && !block 100 @all.size 101 else 102 super 103 end 104 end
Yield each of the model’s frozen instances to the block, without issuing a database query.
# File lib/sequel/plugins/static_cache.rb 114 def each(&block) 115 if @static_cache_frozen 116 @all.each(&block) 117 else 118 @all.each{|o| yield(static_cache_object(o))} 119 end 120 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).
# File lib/sequel/plugins/static_cache.rb 89 def first(*args) 90 if defined?(yield) || args.length > 1 || (args.length == 1 && !args[0].is_a?(Integer)) 91 super 92 else 93 @all.first(*args) 94 end 95 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.
# File lib/sequel/plugins/static_cache.rb 213 def load_cache 214 @all = load_static_cache_rows 215 h = {} 216 @all.each do |o| 217 o.errors.freeze 218 h[o.pk.freeze] = o.freeze 219 end 220 @cache = h.freeze 221 end
Use the cache instead of a query to get the results.
# File lib/sequel/plugins/static_cache.rb 123 def map(column=nil, &block) 124 if column 125 raise(Error, "Cannot provide both column and block to map") if block 126 if column.is_a?(Array) 127 @all.map{|r| r.values.values_at(*column)} 128 else 129 @all.map{|r| r[column]} 130 end 131 elsif @static_cache_frozen 132 @all.map(&block) 133 elsif block 134 @all.map{|o| yield(static_cache_object(o))} 135 else 136 all.map 137 end 138 end
Ask whether modifications to this class are allowed.
# File lib/sequel/plugins/static_cache.rb 207 def static_cache_allow_modifications? 208 !@static_cache_frozen 209 end
Alias of as_hash
for backwards compatibility.
# File lib/sequel/plugins/static_cache.rb 177 def to_hash(*a) 178 as_hash(*a) 179 end
Use the cache instead of a query to get the results
# File lib/sequel/plugins/static_cache.rb 182 def to_hash_groups(key_column, value_column = nil, opts = OPTS) 183 h = opts[:hash] || {} 184 if value_column 185 if value_column.is_a?(Array) 186 if key_column.is_a?(Array) 187 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)} 188 else 189 @all.each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)} 190 end 191 else 192 if key_column.is_a?(Array) 193 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]} 194 else 195 @all.each{|r| (h[r[key_column]] ||= []) << r[value_column]} 196 end 197 end 198 elsif key_column.is_a?(Array) 199 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << static_cache_object(r)} 200 else 201 @all.each{|r| (h[r[key_column]] ||= []) << static_cache_object(r)} 202 end 203 h 204 end