开发者

Looking for a Perl module to store a Hash structure in shared RAM

开发者 https://www.devze.com 2023-01-03 20:25 出处:网络
I\'d like to store a data structure persistently in RAM and have it accessible from pre-forked web server processes in Perl.

I'd like to store a data structure persistently in RAM and have it accessible from pre-forked web server processes in Perl.

Ideally I would like it to behave like memcached but开发者_如何学运维 without the need for a separate daemon. Any ideas?


Use Cache::FastMmap and all you need is a file. It uses mmap to provide a shared in-memory cache for IPC, which means it is quite fast. See the documentation for possible issues and caveats.


IPC::SharedMem might fit the bill.


Mod_perl shares RAM on systems with properly implemented copy-on-write forking. Load your Perl hash in a BEGIN block of your mod_perl program, and all forked instances of the mod_perl program will share the memory, as long as there are no writes to the pages storing your hash. This doesn't work perfectly (some pages will get written to) but on my servers and data it decreases memory usage by 70-80%.

Mod_perl also speeds up your server by eliminating the compile-time for Perl on subsequent web requests. The downside of mod_perl that you have to program carefully, and avoid programs that modify global variables, since these variables, like your hash, are shared by all the mod_perl instances. It is worthwhile to learn enough Perl so that you don't need to change globals, anyway!

The performance gains from mod_perl are fantastic, but mod_perl is not available in many shared hosts. It is easy to screw up, and hard to debug while you are learning it. I only use it when the performance improvements are appreciated enough by my customers to justify my development pain.

0

精彩评论

暂无评论...
验证码 换一张
取 消