CakePHP 模型加载需要一段时间

发布于 2024-12-22 21:19:44 字数 520 浏览 1 评论 0原文

为什么 App::import('*', '*'); 以及模型加载时间长达约 3-5ms?

当一个页面有大约 100 个所需模型时,这是相当烦人的;仅加载核心和模型就需要大约 300 毫秒。

此外,我发现当启用缓存系统时,加载时间并没有减少。

您知道为什么它这么长,而对于需要大量模型的应用程序来说,减少这个时间又是另一回事吗?

提前致谢!

编辑:当我使用文件缓存系统(将调试模式设置为0)时,我看到由缓存始终与我上次生成页面时相同。这意味着缓存每次都会重新生成模型文件(尽管调试模式设置为 0)。这正常吗?

EDIT2:当我使用 Notepad++ 打开模型缓存文件时,开头有尾随的“nul”标志,这是其他缓存文件中没有的(例如持久) 。

EDIT3长话短说:为什么如果缓存系统打开,模型的加载时间与没有缓存系统时一样长?

Why is App::import('*', '*');, as well as model loading takes as long as ~3-5ms?

This is quite annoying when you have around 100 required models for a page; it'll take more or less 300ms just to load the core and the models.

Furthermore, I saw that this load time is not decreased when a cache system is enabled.

Have you an idea why it's so long and another to decrease this time for an application that need much models?

Thanks in advance!

EDIT : When I use the file cache system (whit debug mode to 0) I see that the creation date property of the file generated by the cache is always the same as the last time I generate a page. This means that the cache is regenerating the model files every time (despite of the debug mode set to 0). Is that normal?

EDIT2 : When I open the model cache files with Notepad++, there is trailing "nul" flags at the begining that I don't have in other cache files (like persistent).

EDIT3 : Long story short : Why is the loading time of a Model as long if a cache system is on as it would be without this cache system?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

弥繁 2024-12-29 21:19:44

确实,我看到了标签、用户、问题、评论、广告以及每个的一些自定义查找查询。 (本页)。在这种情况下,不需要将喜欢的、共享的或那些属性作为模型。如果你做到了这一点,那么你就必须期望减轻加载问题。

如果您需要在一个页面中使用 30 个模型,那么您正在做一些不合逻辑的事情,或者您的用例远远超出了规范。

Really, I see Tag, User, Question, Comment, Advertisement and a few custom find queries for each. (This page). There is no need for a liked, or shared, or those attributes to be models in this case. If you get that granular then you have to expect to mitigate loading issues.

If you need to use 30 models in one page then you are doing something illogical or your use case is far outside the norm.

古镇旧梦 2024-12-29 21:19:44

有几件事需要注意:

App:import('', '') 正在加载所有内容 - 因此仅此一项就会占用资源。如果您的页面包含 30 多个模型查询 - 首先通过它们的关系获取数据。不要使用 $this->loadModel() 或 $uses 数组;这不是 CakePHP 的设计用途。检查我的回复,了解更好的做法:

cakephp:如何访问另一个没有关联的模型

然后您可以进一步限制获取的数据(这会提高速度):

如何限制使用 find 方法关联模型

Re: Cache - 在 CakePHP 书中阅读它,因为它列出了一些很好的示例。如果您遇到困难,可能值得切换到 MemCache 或其他专用系统,而不是 CakePHP 平面文件。

Couple of things to note:

App:import('', '') is loading everything - so that alone will hog resources. If you have a page with 30 plus model queries - firstly get the data via their relationships. Don't use $this->loadModel() or the $uses array; that isn't how CakePHP was designed to be used. Check my response here on a better practice:

cakephp: how to access another model, which has no association

Then you can further limit the data fetched (which increases speed):

How to limit the fields of the associated models using find method

Re: Cache - read up on it in the CakePHP book as it sets out some good examples. If you are struggling, it may be worth switching over to MemCache or another dedicated system instead of the CakePHP flat files.

一片旧的回忆 2024-12-29 21:19:44

优化是一项相对任务。您的时间和资源最好投入到优化应用程序中消耗最多资源的部分。如果你的瓶颈只有 3 毫秒,那么我很难理解为什么你需要进行任何优化。

话虽这么说,如果您只是一个真正好奇的人,想要找到问题的答案,我会鼓励您坐下来,自己对内部代码进行一些分析,以分解影响执行时间的因素。 CakePHP 邮件列表可能是了解 Cake 内部工作原理的另一个有用资源。

Optimization is a relative task. Your time and resources are best invested in optimizing the part of the application measured to be consuming the most resources. If your bottleneck is only 3ms, then I have a hard time seeing why you need to be doing any optimization at all.

That being said, if you're just a really curious person intent on finding an answer to your question I would encourage you to sit down and do some profiling of the internal code yourself to break down what is contributing to the execution time. The CakePHP mailing list might be another helpful resource in understanding how the Cake internals work.

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