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 80 def all(&block) 81 array = @static_cache_frozen ? @all.dup : to_a 82 array.each(&block) if block 83 array 84 end
Use the cache instead of a query to get the results.
# 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.
# 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
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 111 def cache_get_pk(pk) 112 static_cache_object(cache[pk]) 113 end
Get the number of records in the cache, without issuing a database query.
# 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
Yield each of the model’s frozen instances to the block, without issuing a database query.
# 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
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 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
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 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
Use the cache instead of a query to get the results.
# 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
Ask whether modifications to this class are allowed.
# File lib/sequel/plugins/static_cache.rb 223 def static_cache_allow_modifications? 224 !@static_cache_frozen 225 end
Alias of as_hash for backwards compatibility.
# File lib/sequel/plugins/static_cache.rb 193 def to_hash(*a) 194 as_hash(*a) 195 end
Use the cache instead of a query to get the results
# 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