问题
I know there is a module like Apache::DBI
which allows to use persistent connections to database under mod_perl.
Is there an easy way to make something like this? Could you recommend any memcached pooling package?
回答1:
If you look at Apache::DBI's source code, you'll see that it just puts the database handle in a lexical scoped at the top of the package. It's easy enough to do this for Memcached. Make a new package, with a my $memcached;
at the top, then have an init()
method make the connection and save it in that lexical. Then have a getConnection()
method to fetch the connection in your code. That method needs to check if the connection is still alive and reconnect if needed.
Lastly, setup a PerlChildInitHandler
to call init()
(see Apache::DBI connect_on_init()
).
来源:https://stackoverflow.com/questions/8344162/what-is-the-best-way-to-create-persistent-memcached-connections-under-mod-perl