Looking for a Perl module to store a Hash structur

2020-05-28 19:50发布

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?

3条回答
▲ chillily
2楼-- · 2020-05-28 19:52

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.

查看更多
成全新的幸福
3楼-- · 2020-05-28 19:55

IPC::SharedMem might fit the bill.

查看更多
不美不萌又怎样
4楼-- · 2020-05-28 19:56

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.

查看更多
登录 后发表回答