Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching variables in PHP

Tags:

php

caching

I've been caching the output buffer of pages lately, but now I want to cache the values of variables.

I have a PHP file that does a bunch of MySQL queries, then fills variables with various data from those queries.

Some of those variables will never change but some will change quite often. How can I cache certain variables in this manner? I'm using file-based caching, if it helps.

like image 964
AKor Avatar asked Apr 14 '11 06:04

AKor


People also ask

What is PHP caching?

PHP 'output caching' saves a chunk of data somewhere that can later be read by another script faster than it can generate it. 'Parser caching' is specific feature. PHP is a scripting language and code is not compiled or optimized to a particular computer. Every PHP file must be parsed and that takes time.

Are static variables cached?

Static data will cache exactly the same as any other data.

How does PHP store data in cache?

The easiest is to serialize() the data and store it in your database. When you need to retrieve the database, query it from the database, unserialize() it, and use it as before. As second approach is to add memcache to your PHP installation and access your data via the memcache functions.

Can PHP files be cached?

The Windows Cache Extension for PHP includes a file cache that is used to store the content of the PHP script files in shared memory, which reduces the amount of file system operations performed by PHP engine. Resolve File Path Cache - PHP scripts very often include or operate with files by using relative file paths.


4 Answers

Yup, file based caching is an option.

There are also other options like memcache and APC

You should have a look at these as well. If your application is putting a lot of load on your MySQL server, and if your DB is already optimized, caching is a step you can take.

like image 120
JohnP Avatar answered Oct 23 '22 01:10

JohnP


You can dump any variable (including an array) using serialize, and the inverse is unserialize.

Dumping to a file would be quite a useless cache solution, you can consider using memcache which can store any variable in memory, but requires some work on server side.

I find that a local mysql with MEMORY tables can be useful, too...

like image 45
gd1 Avatar answered Oct 23 '22 00:10

gd1


I dont know, how you structured your current caching stuff, so this is just a short template on how you can save any kind of variable (as long as its content is serializeable) to a file.

file_put_contents($filename, serialize($variable));
like image 44
KingCrunch Avatar answered Oct 23 '22 02:10

KingCrunch


Since you asked about file-based caching, both memcache and APC are no option, although I would certainly recommend both in cases where the stored data is not too large.

For file based caching, I would recommend you to use a caching framework. For example, you could use Zend_Cache from the Zend Framework. It allows you to store your query results in files by using a nice object oriented interface. Plus, you've got a lot of options, such as validation and serialization. There are also other caching frameworks out there.

like image 33
user228395 Avatar answered Oct 23 '22 00:10

user228395