Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Caching - Is it faster to save in database or create a file?

Tags:

php

mysql

caching

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?

like image 885
Dan Avatar asked May 19 '11 03:05

Dan


People also ask

Why is cache faster than database?

A cache's primary purpose is to increase data retrieval performance by reducing the need to access the underlying slower storage layer. Trading off capacity for speed, a cache typically stores a subset of data transiently, in contrast to databases whose data is usually complete and durable.

Is database caching good?

Overview. In-memory data caching can be one of the most effective strategies to improve your overall application performance and to reduce your database costs.

When should you cache a database?

Caches are generally used to keep track of frequent responses to user requests. It can also be used in the case of storing results of long computational operations. Caching is storing data in a location different than the main data source such that it's faster to access the data.

Which database is best for caching?

Key value databases can also be used for web caches. Key value databases are a type of NoSQL database and are the least structured of the schemaless data stores. Other types of NoSQL tools include. Those who need a solution at no cost can look at free database software.

Do PHP files get cached?

A cache is a collection of duplicate data, where the original data is expensive to fetch or compute (usually in terms of access time) relative to the cache. In PHP, caching is used to minimize page generation time.


2 Answers

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?

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.

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

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).

like image 70
Denis de Bernardy Avatar answered Sep 19 '22 14:09

Denis de Bernardy


Both options utilise the file system, as (assuming you're using MySQL without MEMORY/HEAP tables) database records are still stored in files.

If you have an active database connection at the time of requesting the cached data, I'd stick with the database.

like image 42
pkavanagh Avatar answered Sep 22 '22 14:09

pkavanagh