mperham/dalli — pull request #142 by PaulSD (2011-11-17 02:40:47+00:00)
Title: Documentation typo and Passenger handling proposal

For the passenger commit ... I've been using something like the following in config/preinitializer.rb for several years now (of course, updated slightly when I switched from memcache-client to Dalli):
unless defined? DALLI_PASSENGER_RECONNECT
DALLI_PASSENGER_RECONNECT = true
require 'dalli'
module Dalli
class Client
alias_method :orig_initialize, :initialize
def initialize(*args)
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
close if forked
end
end
orig_initialize(*args)
end
end
end
end

The advantage to this over the example currently listed in the Dalli readme is that you don't have to worry about finding a clean way to get at Rails's session store, and you also don't have to worry about remembering to update environment.rb again if you end up using Dalli under Passenger outside of Rails's object cache and session store ... just set and forget.

I was thinking of simply proposing this as a better example to put in the README file, but then I thought ... is there any good reason not to just put this in Dalli itself? The code doesn't do anything if it's not run under Passenger, so why not simply add it to Dalli directly and relieve developers of this burden entirely?