如何正确减少 mod_perl 的冗余请求数量?

发布于 2024-08-23 20:46:27 字数 495 浏览 11 评论 0原文

在一个相当大的遗留项目中,我将几个毛茸茸的模块重构为 Moose 类。这些模块中的每一个都需要数据库访问来(惰性)获取其属性。由于这些对象的使用相当频繁,我想减少冗余请求的数量,例如针对未更改的数据。

现在,我该如何正确地做到这一点?我有几种替代方案:

  1. 通过一个角色在我的 Moose 类中实现缓存,将它们存储在 memcached 中,并在 5-10 分钟内过期(可能不是太困难,但对于惰性属性来说很棘手)更新:KiokuDB 可能会在这里提供帮助,必须阅读有关属性的信息
  2. 迁移到 DBIx::Class (无论如何都需要完成)并在此级别上实现缓存(DBIC 可能会花费大部分时间)痛苦本身就消失了)
  3. 以某种方式让我的对象在 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:

  1. 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
  2. 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)
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若相惜即相离 2024-08-30 20:46:27

对#3 的简短回答是:不要使用“my”。您可能会这样做:

 use vars qw($object);
 # OR post perl5.6:
 # our ($object); 

 # create your object if it doesn't already exist
 $object ||= create_object;

 # Maybe reload some attributes if they have expired.
 $object->check_expires;

在处理程序中像这样创建的对象将仅在每个 Apache 子进程中共享,如果您每 5-10 分钟重新加载一次数据,这很好。任何只读的模块和对象都应该加载到 PerlPostConfigRequire 脚本中,以便它们在所有子项之间共享。

The short answer to #3 is: Don't use 'my'. You might do something like:

 use vars qw($object);
 # OR post perl5.6:
 # our ($object); 

 # create your object if it doesn't already exist
 $object ||= create_object;

 # Maybe reload some attributes if they have expired.
 $object->check_expires;

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.

oО清风挽发oО 2024-08-30 20:46:27

由于无论如何您已经要进行 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文