Class: ActiveRecord::MemoryCache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/cachear.rb

Constant Summary collapse

@@max_size =

Maximal cache size for one model is 10MB by default

10 * 1024 * 1024
@@log =
false

Instance Method Summary collapse

Constructor Details

#initializeMemoryCache

Returns a new instance of MemoryCache.



162
163
164
165
166
# File 'lib/cachear.rb', line 162

def initialize
  @tables = Hash.new do |hsh, table|
    hsh[table] = Cache.new
  end
end

Instance Method Details

#cache_query(klass, sql, &blk) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cachear.rb', line 168

def cache_query(klass, sql, &blk)
  sql = ActiveRecord::CacheSQLQuery.create(sql)
  tbl = klass.table_name()
  if @tables[tbl].cached?(sql) and !@tables[tbl][sql].expired?
    log_hit(sql) 
    inst = @tables[tbl][sql].instance
    inst
  else      
    log_miss(sql)      
    inst = store_result( sql, blk.call(sql.to_s) ).instance      
    inst      
  end    
end

#expire(klass) ⇒ Object



182
183
184
185
186
187
188
189
# File 'lib/cachear.rb', line 182

def expire(klass)
  if(cache = @tables[ klass.table_name() ])
    cache.each do |key, inst|
      inst.expire!
      cache.delete(key)
    end
  end
end