PHP 缓存动态构建的文件

发布于 2024-12-11 06:25:59 字数 417 浏览 0 评论 0原文

我目前正在开发一个基于 MVC 模式的小型个人 Web 应用程序框架。在此框架中,一些页面和模板是从多个文件动态构建的。但是,尤其是这些模板不会经常更改,因此我想创建某种形式的缓存。理想情况下,我只想在原始源文件更改时更新缓存文件,并且我想在其中一个源文件更改后的第一个请求时更新缓存文件。但由于 1 个缓存文件是由多个源文件构建的,因此我需要某种方法来知道哪些源文件属于缓存文件,这样我就知道需要检查哪些文件是否有更新。

我想出了使用数据库中的表或保存该信息的单独文件的想法。然而,这需要在每个页面请求上产生一些额外的开销,因为我必须再读取一个文件,或者必须执行一些数据库 IO。我可以使用其他技术来尽可能减少开销吗?

附:缓存并不是真正需要的,因为我只会将它用于服务器上的一些小网站,而服务器对于这些网站来说太强大了。然而,我认为这对我来说是一个很好的习惯,可以让我更加习惯缓存技术。

I am currently working on a small personal framework for web applications, based on the MVC pattern. In this framework some pages and templates are dynamically constructed from multiple files. However, especially these templates won't change often, so I want to create some form of caching. Ideally I only want to update the cached files when the original source files are changed, and I want to update the cached files on the first request after one of the source files is changed. But since 1 cache file is constructed from multiple source files, I need some way to know which source files belongs to a cache file, so I know which files I need to check for updates.

I came up with the idea to either use a table in a database or a separate file which saves that information. However, this requires some extra overhead on each page request, since I'll have to read one more file or I'll have to do some database IO. Are there any other techniques I can use to have as little overhead as possible?

ps. Caching isn't really needed since I'll only use it for a few small websites on a server which is far too powerful for these websites. However, I think it's a good practice for me to become more used to caching techniques.

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

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

发布评论

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

评论(1

甜扑 2024-12-18 06:25:59

也许看看 PHP APC,您可以为模板创建 SHA1 哈希值并将它们存储在具有较长过期时间的缓存中。当情况发生变化时,您可以在缓存中创建标志,并从缓存和文件系统中检索文档。

Maybe look into PHP APC, you can create SHA1 hashes for your templates and store them in the cache with long expiration times. You can create flags in the cache when things change, and retrieve the documents from the cache vs the file system.

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