Skip to content

Instantly share code, notes, and snippets.

@arthurnn
Created February 19, 2014 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurnn/9096869 to your computer and use it in GitHub Desktop.
Save arthurnn/9096869 to your computer and use it in GitHub Desktop.
require 'active_record'
require 'identity_cache'
require 'logger'
logger = Logger.new nil
ActiveRecord::Base.establish_connection(adapter: 'mysql2', database: 'test', username: 'root')
IdentityCache.cache_backend = ActiveSupport::Cache::MemCacheStore.new
IdentityCache.logger = logger
class Image < ActiveRecord::Base
connection.create_table table_name, force: true do |t|
t.integer :product_id
end
include IdentityCache
belongs_to :product
end
class Product < ActiveRecord::Base
connection.create_table table_name, force: true do |t|
end
include IdentityCache
has_many :images
cache_has_many :images, :embed => true
# has_many :pictures, class_name: 'Image'
# cache_has_many :pictures, :embed => true
end
product = Product.create!
3.times { Image.create!(product: product) }
id = product.id
Benchmark.bm do |x|
x.report do
100_000.times do
Product.fetch(id)
end
end
end
@arthurnn
Copy link
Author

results:

before:

       user     system      total        real
  36.310000   2.480000  38.790000 ( 39.942972)

after:

       user     system      total        real
  21.880000   2.200000  24.080000 ( 24.851130)

rails 3:

       user     system      total        real
  20.220000   2.040000  22.260000 ( 23.073374)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment