Permalink
Browse files

introducing caching

  • Loading branch information...
1 parent ea1d6cc commit c1fec02a7ad0e4973280ab68b75e647122d21173 Nick Kallen committed Apr 17, 2009
Showing with 7 additions and 3 deletions.
  1. +3 −3 joke_server.rb
  2. +4 −0 util/in_process_lru_cache.rb
View
@@ -14,7 +14,7 @@
begin
logfile = File.join(File.dirname(__FILE__), 'log', File.basename(__FILE__, '.rb') + '.log')
- $stats = Statosaurus.new(['job_user', 'job_sys', 'job_real', 'source_transaction_id'], Logger.new(logfile))
+ $stats = Statosaurus.new(['cache_hit', 'job_user', 'job_sys', 'job_real', 'source_transaction_id'], Logger.new(logfile))
end
module JokeServer
@@ -24,11 +24,11 @@ module JokeServer
def receive_line(line)
$stats.transaction do
data, source_transaction_id = line.split(';')
- $stats.set('source_transaction_id', data)
+ $stats.set('source_transaction_id', source_transaction_id)
$stats.measure('job') do
result = JokeServer.get(data) do
100000.times { Time.now }
- sleep rand * 3
+ sleep rand
"KNOCK KNOCK: #{data}\n"
end
send_data(result)
@@ -5,11 +5,15 @@ def get(data)
data == key
end
if !cache_hit
+ $stats.set('cache_hit', 0)
+
result = yield(data)
@cache.unshift([data, result])
@cache.slice!(2..-1)
result
else
+ $stats.set('cache_hit', 1)
+
cache_hit[1]
end
end

0 comments on commit c1fec02

Please sign in to comment.