# File lib/sequel/plugins/static_cache.rb 201 def primary_key_lookup(pk) 202 static_cache_object(cache[pk]) 203 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
An array of all of the model's frozen instances, without issuing a database query.
# File lib/sequel/plugins/static_cache.rb 75 def all 76 if @static_cache_frozen 77 @all.dup 78 else 79 map{|o| o} 80 end 81 end
Use the cache instead of a query to get the results.
# File lib/sequel/plugins/static_cache.rb 130 def as_hash(key_column = nil, value_column = nil, opts = OPTS) 131 if key_column.nil? && value_column.nil? 132 if @static_cache_frozen && !opts[:hash] 133 return Hash[cache] 134 else 135 key_column = primary_key 136 end 137 end 138 139 h = opts[:hash] || {} 140 if value_column 141 if value_column.is_a?(Array) 142 if key_column.is_a?(Array) 143 @all.each{|r| h[r.values.values_at(*key_column)] = r.values.values_at(*value_column)} 144 else 145 @all.each{|r| h[r[key_column]] = r.values.values_at(*value_column)} 146 end 147 else 148 if key_column.is_a?(Array) 149 @all.each{|r| h[r.values.values_at(*key_column)] = r[value_column]} 150 else 151 @all.each{|r| h[r[key_column]] = r[value_column]} 152 end 153 end 154 elsif key_column.is_a?(Array) 155 @all.each{|r| h[r.values.values_at(*key_column)] = static_cache_object(r)} 156 else 157 @all.each{|r| h[r[key_column]] = static_cache_object(r)} 158 end 159 h 160 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 94 def cache_get_pk(pk) 95 static_cache_object(cache[pk]) 96 end
Get the number of records in the cache, without issuing a database query.
# File lib/sequel/plugins/static_cache.rb 84 def count(*a, &block) 85 if a.empty? && !block 86 @all.size 87 else 88 super 89 end 90 end
Yield each of the model's frozen instances to the block, without issuing a database query.
# File lib/sequel/plugins/static_cache.rb 100 def each(&block) 101 if @static_cache_frozen 102 @all.each(&block) 103 else 104 @all.each{|o| yield(static_cache_object(o))} 105 end 106 end
Use the cache instead of a query to get the results.
# File lib/sequel/plugins/static_cache.rb 109 def map(column=nil, &block) 110 if column 111 raise(Error, "Cannot provide both column and block to map") if block 112 if column.is_a?(Array) 113 @all.map{|r| r.values.values_at(*column)} 114 else 115 @all.map{|r| r[column]} 116 end 117 elsif @static_cache_frozen 118 @all.map(&block) 119 elsif block 120 @all.map{|o| yield(static_cache_object(o))} 121 else 122 all.map 123 end 124 end
Ask whether modifications to this class are allowed.
# File lib/sequel/plugins/static_cache.rb 193 def static_cache_allow_modifications? 194 !@static_cache_frozen 195 end
Alias of as_hash
for backwards compatibility.
# File lib/sequel/plugins/static_cache.rb 163 def to_hash(*a) 164 as_hash(*a) 165 end
Use the cache instead of a query to get the results
# File lib/sequel/plugins/static_cache.rb 168 def to_hash_groups(key_column, value_column = nil, opts = OPTS) 169 h = opts[:hash] || {} 170 if value_column 171 if value_column.is_a?(Array) 172 if key_column.is_a?(Array) 173 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r.values.values_at(*value_column)} 174 else 175 @all.each{|r| (h[r[key_column]] ||= []) << r.values.values_at(*value_column)} 176 end 177 else 178 if key_column.is_a?(Array) 179 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << r[value_column]} 180 else 181 @all.each{|r| (h[r[key_column]] ||= []) << r[value_column]} 182 end 183 end 184 elsif key_column.is_a?(Array) 185 @all.each{|r| (h[r.values.values_at(*key_column)] ||= []) << static_cache_object(r)} 186 else 187 @all.each{|r| (h[r[key_column]] ||= []) << static_cache_object(r)} 188 end 189 h 190 end
Private Instance Methods
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 207 def load_cache 208 a = dataset.all 209 h = {} 210 a.each do |o| 211 o.errors.freeze 212 h[o.pk.freeze] = o.freeze 213 end 214 @all = a.freeze 215 @cache = h.freeze 216 end
Return the frozen object with the given pk, or nil if no such object exists in the cache, without issuing a database query.
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.
# File lib/sequel/plugins/static_cache.rb 221 def static_cache_object(o) 222 if @static_cache_frozen 223 o 224 elsif o 225 call(Hash[o.values]) 226 end 227 end