Bobochka
10/21/2015 - 7:29 AM

AR_cache_benchmarks.rb

class InMemoryCache
  def self.instance
    @instance ||= ActiveSupport::Cache.lookup_store(:memory_store)
  end

  def self.method_missing(symbol, *args, &block)
    instance.public_send(symbol, *args, &block)
  end
end

Benchmark.ips do |x|
  x.config(:time => 5, :warmup => 1)
  Division.connection.disable_query_cache!
  ActiveRecord::Base.logger = nil

  x.report('using db') do
    Division.last
  end

  x.report('using redis') do
    Rails.cache.fetch('last_division', exires_in: 15) { Division.last }
  end

  x.report('using local memory') do
    InMemoryCache.fetch('last_division', exires_in: 15) { Division.last }
  end
end


# Calculating -------------------------------------
#             using db    71.000  i/100ms
#         using redis   252.000  i/100ms
#   using local memory   911.000  i/100ms
# -------------------------------------------------
#             using db    707.840  (± 9.2%) i/s -      3.550k
#         using redis      2.615k (±14.3%) i/s -     12.852k
#   using local memory      9.730k (±15.4%) i/s -     48.283k