PHP缓存-保存到数据库或创建文件是否更快? [英] PHP Caching - Is it faster to save in database or create a file?

查看:94
本文介绍了PHP缓存-保存到数据库或创建文件是否更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在通过将动态生成的PHP页面保存到具有到期时间字段的数据库中来缓存它们.如果再次请求该页面,则程序将检查该页面的未过期缓存版本是否可以提供,并且仅在找不到该页面时才重新生成该页面.

I am currently caching dynamically generated PHP pages by saving them to a database with an expiry time field. If the page is requested again, the program checks for a unexpired cached version of the page to serve up, and only regenerates the page if it can't find one.

这很好用-但这会减轻服务器的负担,将缓存的页面另存为文件而不是保存到数据库吗?我可以在文件中使用命名约定来处理到期时间.

This works well - but would it put less load on the server to save the cached pages as files instead saving to the database? I could use a naming convention in the files to handle the expiry time.

如果从文件而不是数据库中进行读取/写入速度更快且服务器负担更少,那么我将切换到该位置.有谁知道哪个更快/最佳做法?

If it is faster and less server taxing to read/write from a file instead of a database, I'll switch to that. Does anyone know which is faster / best practice?

推荐答案

如果从文件而不是数据库中进行读取/写入速度更快且服务器负担更少,那么我将切换到该位置.有谁知道哪个更快/最佳做法?

If it is faster and less server taxing to read/write from a file instead of a database, I'll switch to that. Does anyone know which is faster / best practice?

最快的方法是使用静态文件,因为这样就可以在不启动PHP的情况下发出缓存(使用RewriteRules).但是,如果您有多个前端,它将无法正确缩放.

The fastest is to use static files, because you can then issue the cache without even starting PHP (using RewriteRules). It won't scale properly if you've multiple front ends, however.

下一个最好的方法是使用 Memcache 例如.

The next best thing is to store it in memory, using Memcache for instance.

最不推荐的是使用SQL.如果坚持下去,至少可以使用内存存储引擎或类似的东西(例如,如果您使用的是PostgreSQL,则位于RAM磁盘上存储的表空间中的未记录表)来帮助您的硬盘.

The least preferable is to use SQL. If you stick with it, at least do your hard-drive a favor by using the Memory storage engine or equivalent (e.g. an unlogged table that lives in a tablespace stored on a RAM disk, if you're using PostgreSQL).

这篇关于PHP缓存-保存到数据库或创建文件是否更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆