Class: MemCache
- Inherits:
-
Object
- Object
- MemCache
- Defined in:
- lib/adapters/memcache-client.rb
Overview
Add this line to your ApplicationController (app/controllers/application.rb) to enable logging for memcache-client: custom_benchmark {|runtime| MemCache.cache_runtime(runtime) }
Constant Summary collapse
- @@record_size =
false- @@logger =
nil- @@error_logger =
nil- @@cache_latency =
0.0
- @@cache_gets =
0- @@cache_sets =
0- @@cache_deletes =
0- @@cache_hits =
0- @@cache_misses =
0- @@get_data_size =
0- @@set_data_size =
0
Class Method Summary collapse
Instance Method Summary collapse
- #add_with_benchmark(key, val, expiry = 0, raw = false) ⇒ Object (also: #add)
- #delete_with_benchmark(key, expiry = 0) ⇒ Object (also: #delete)
- #get_multi_with_benchmark(*keys) ⇒ Object (also: #get_multi)
- #get_with_benchmark(key, raw = false) ⇒ Object (also: #get, #[])
- #rescue_no_connection ⇒ Object
- #set_with_benchmark(key, val, expiry = 0, raw = false) ⇒ Object (also: #set, #[]=)
Class Method Details
.cache_runtime(runtime) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/adapters/memcache-client.rb', line 35 def self.cache_runtime(runtime) latency,gets,gsize,sets,ssize,deletes,hits,misses = self.get_benchmarks # Since we're using memcache store, the reset_benchmarks method call must # appear at the beginning of the the request. This is necessary before # Rails saves the session to the store after the entry is written to # production.log. If you don't clear the MemCache stats at the beginning # of the request, then the session save from other requests pollutes the # cache stats for the subsequent requests. Use a before_filter in # application.rb: # before_filter { MemCache.reset_benchmarks } # If you aren't using memcache session store then you uncomment this line: # self.reset_benchmarks " | memcache: #{sprintf("%.5f,%d,%d,%d,%d,%d,%d,%d",latency,gets,gsize,sets,ssize,deletes,hits,misses)} (#{sprintf("%d", (latency * 100) / runtime)}%)" end |
.get_benchmarks ⇒ Object
31 32 33 |
# File 'lib/adapters/memcache-client.rb', line 31 def self.get_benchmarks [@@cache_latency, @@cache_gets, @@get_data_size, @@cache_sets, @@set_data_size, @@cache_deletes, @@cache_hits, @@cache_misses] end |
.reset_benchmarks ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/adapters/memcache-client.rb', line 20 def self.reset_benchmarks @@cache_latency = 0.0 @@cache_gets = 0 @@cache_sets = 0 @@cache_deletes = 0 @@cache_hits = 0 @@cache_misses = 0 @@get_data_size = 0 @@set_data_size = 0 end |
Instance Method Details
#add_with_benchmark(key, val, expiry = 0, raw = false) ⇒ Object Also known as: add
112 113 114 115 116 117 118 119 120 |
# File 'lib/adapters/memcache-client.rb', line 112 def add_with_benchmark(key, val, expiry=0, raw=false) t1 = Time.now rescue_no_connection { add_without_benchmark(key, val, expiry, raw) } @@cache_latency += Time.now - t1 @@cache_sets += 1 size = @@record_size ? (Marshal.dump(val).length rescue 0) : 0 @@set_data_size += size if @@record_size @@logger.info("MEMCACHE ADD #{key} SIZE #{size} TIME #{Time.now - t1}") if @@logger end |
#delete_with_benchmark(key, expiry = 0) ⇒ Object Also known as: delete
124 125 126 127 128 129 130 |
# File 'lib/adapters/memcache-client.rb', line 124 def delete_with_benchmark(key, expiry=0) t1 = Time.now rescue_no_connection { delete_without_benchmark(key, expiry) } @@cache_latency += Time.now - t1 @@cache_deletes += 1 @@logger.info("MEMCACHE DELETE #{key} TIME #{Time.now - t1}") if @@logger end |
#get_multi_with_benchmark(*keys) ⇒ Object Also known as: get_multi
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/adapters/memcache-client.rb', line 80 def get_multi_with_benchmark(*keys) val = nil t1 = Time.now vals = rescue_no_connection { get_multi_without_benchmark(keys) } @@cache_latency += Time.now - t1 @@cache_gets += 1 if @@logger or @@record_size request_id = t1.to_f.to_s.last(4) for key in keys size = @@record_size ? (Marshal.dump(vals[key]).length rescue 0) : 0 @@get_data_size += size if @@record_size @@logger.info("MEMCACHE GETMULTI ID #{request_id} KEY #{key} SIZE #{size} TIME #{Time.now - t1}") if @@logger end end vals end |
#get_with_benchmark(key, raw = false) ⇒ Object Also known as: get, []
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/adapters/memcache-client.rb', line 64 def get_with_benchmark(key, raw=false) val = nil t1 = Time.now val = rescue_no_connection { get_without_benchmark(key, raw) } val.nil? ? (@@cache_misses += 1) : (@@cache_hits += 1) @@cache_latency += Time.now - t1 @@cache_gets += 1 size = @@record_size ? (Marshal.dump(val).length rescue 0) : 0 @@get_data_size += size if @@record_size @@logger.info("MEMCACHE GET #{key} SIZE #{size} TIME #{Time.now - t1}") if @@logger val end |
#rescue_no_connection ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/adapters/memcache-client.rb', line 52 def rescue_no_connection begin yield rescue MemCache::MemCacheError => err @@error_logger.info([Time.now.to_s, err., err.backtrace].compact.join("\n")) if @@error_logger if err. != "No connection to server" and err. !~ /^lost connection/i and ENV['RAILS_ENV'] != 'development' SystemNotifier.deliver_non_controller_exception_notification(err) end return nil end end |
#set_with_benchmark(key, val, expiry = 0, raw = false) ⇒ Object Also known as: set, []=
99 100 101 102 103 104 105 106 107 |
# File 'lib/adapters/memcache-client.rb', line 99 def set_with_benchmark(key, val, expiry=0, raw=false) t1 = Time.now rescue_no_connection { set_without_benchmark(key, val, expiry, raw) } @@cache_latency += Time.now - t1 @@cache_sets += 1 size = @@record_size ? (Marshal.dump(val).length rescue 0) : 0 @@set_data_size += size if @@record_size @@logger.info("MEMCACHE SET #{key} SIZE #{size} TIME #{Time.now - t1}") if @@logger end |