如何正确减少 mod_perl 的冗余请求数量?
在一个相当大的遗留项目中,我将几个毛茸茸的模块重构为 Moose 类。这些模块中的每一个都需要数据库访问来(惰性)获取其属性。由于这些对象的使用相当频繁,我想减少冗余请求的数量,例如针对未更改的数据。
现在,我该如何正确地做到这一点?我有几种替代方案:
- 通过一个角色在我的 Moose 类中实现缓存,将它们存储在
memcached
中,并在 5-10 分钟内过期(可能不是太困难,但对于惰性属性来说很棘手)更新:KiokuDB 可能会在这里提供帮助,必须阅读有关属性的信息 - 迁移到
DBIx::Class
(无论如何都需要完成)并在此级别上实现缓存(DBIC 可能会花费大部分时间)痛苦本身就消失了) - 以某种方式让我的对象在 mod_perl 进程中持久存在(不知道如何做到这一点:()
你会如何做到这一点以及你认为一个明智的方法是什么?在对象或 ORM 上缓存数据是首选等级?
In a fairly big legacy project, I've refactored several hairy modules into Moose classes. Each of these modules requires database access to (lazy) fetch its attributes. Since those objects are used pretty heavily, I want to reduce the number of redundant requests, for example for unchanged data.
Now, how do I do that properly? I've got several alternatives:
- Implement caching in my Moose classes via a role to store them in
memcached
with expiration of 5-10 minutes (probably not too difficult, but tricky with lazy attributes) update: KiokuDB could probably help here, have to read up about attributes - Migrate to
DBIx::Class
(needs to be done anyway) and implement caching on this level (DBIC will probably take most of the pain away just by itself) - Somehow make my objects persist inside the mod_perl process (no clue how to do this :()
How would you do this and what do you consider a sane way? Is caching data preferred on object or the ORM level?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对#3 的简短回答是:不要使用“my”。您可能会这样做:
在处理程序中像这样创建的对象将仅在每个 Apache 子进程中共享,如果您每 5-10 分钟重新加载一次数据,这很好。任何只读的模块和对象都应该加载到 PerlPostConfigRequire 脚本中,以便它们在所有子项之间共享。
The short answer to #3 is: Don't use 'my'. You might do something like:
Objects created like this inside your handler will only be shared inside each Apache child, which is fine if you are reloading the data every 5-10 minutes. Any modules and objects that are read-only should be loaded in a PerlPostConfigRequire script so that they will be shared across all children.
由于无论如何您已经要进行 DBIC,因此让此更改来解决它是有意义的。自行推出然后实施 DBIC 的意义不大,当维护人员发现您使用 DBIC 但使用自行开发的缓存时,他们会犹豫不决……“出于某种原因”。
不这样做的唯一原因是 (1) 如果您现在确实需要这种性能,并且您没有时间等待 DBIC 更改,因为我认为这将是相当广泛的。或者 (2),如果您不确定是否真的要转到 DBIC。如果您没有对其进行调查并且正在执行大量自定义 SQL 而不是基本 CRUD,那么最终的投资回报可能会非常小。
Since you're already going to be doing DBIC anyway, it would make sense to let this change take care of it. It would make less sense to roll your own and then implement DBIC, giving your maintainers pause when they discover that you are using DBIC but with home-grown caching ..."for some reason."
The only reasons not to do this would be (1) if you really need that performance now and you don't have time to wait for the DBIC changes, since I imagine that'll be fairly extensive. Or (2), if you're not certain about whether you're really going to move to DBIC. If you haven't investigated it and you're doing a lot of custom SQL instead of basic CRUD, it might end up being a very small return on investment.