寻找一个 Perl 模块来在共享 RAM 中存储哈希结构
我想将数据结构持久存储在 RAM 中,并可以从预分叉中访问它 Perl 中的 Web 服务器进程。
理想情况下,我希望它的行为类似于 memcached,但不需要单独的守护进程。有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Cache::FastMmap ,您所需要的只是一个文件。它使用 mmap 为 IPC 提供共享内存缓存,这意味着它非常快。请参阅文档以了解可能的问题和注意事项。
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 可能符合要求。
IPC::SharedMem might fit the bill.
Mod_perl 在具有正确实现的写时复制分叉的系统上共享 RAM。将 Perl 散列加载到 mod_perl 程序的 BEGIN 块中,只要没有对存储散列的页面进行写入,mod_perl 程序的所有分叉实例都将共享内存。这并不完美(某些页面会被写入),但在我的服务器和数据上,它减少了 70-80% 的内存使用量。
Mod_perl 还可以消除后续 Web 请求上的 Perl 编译时间,从而加快服务器速度。 mod_perl 的缺点是您必须仔细编程,并避免修改全局变量的程序,因为这些变量(如哈希值)由所有 mod_perl 实例共享。无论如何,学习足够的 Perl 是值得的,这样您就不需要更改全局变量了!
mod_perl 的性能提升非常惊人,但 mod_perl 在许多共享主机中不可用。当你学习它的时候,很容易搞砸,而且很难调试。我只在客户对性能改进足够赞赏以证明我的开发痛苦是合理的时候才使用它。
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.