Module | Sequel::Plugins::StaticCache::ClassMethods |
In: |
lib/sequel/plugins/static_cache.rb
|
cache | [R] | A frozen ruby hash holding all of the model‘s frozen instances, keyed by frozen primary key. |
Use the cache instead of a query to get the results.
# File lib/sequel/plugins/static_cache.rb, line 130 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, line 94 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, line 84 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, line 100 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, line 109 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, line 193 193: def static_cache_allow_modifications? 194: !@static_cache_frozen 195: end
Use the cache instead of a query to get the results
# File lib/sequel/plugins/static_cache.rb, line 168 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