TeAmEr
TeAmEr

Reputation: 4773

cache methods in php?

what are the available cache methods i could use in php ?

Cache HTML output
Cache some variables

it would be great to implement more than one caching method , so i need them all , all the available out there (i do caching currently with files , any other ideas ?)

Upvotes: 2

Views: 3464

Answers (3)

foo
foo

Reputation: 2111

Since every PHP run starts from scratch on page request, there is nothing that would persist between calls, making cacheing moot.

Well, that's the basic view. Of course there are ways to implement a caching, sort of - and a few packages and extensions do so (like Zend Extensions and APC). However, you should have a very close look whether it actually improves performance. Other methods like memcache (for DB results), or switching from PHP to e.g. Java will often yield better results.

You can store variables in the $_SESSION, but you shouldn't keep larger HTML there.

Please check what you are actually trying to do. "Bytecode cacheing" (that is, saving PHP parsing time) needs to be done by the PHP runtime executable. For cacheing Database (SQL) request/reply-pairs, there is memcache. Cacheing HTML output can be done, but is often not a good idea.

See also an earlier answer on a similar question.

Upvotes: 0

user336242
user336242

Reputation:

If you are using a framework, then most come with some form of caching mechanism that you can use e.g. Zend Framework's Zend_Cache. If you are not using a framework then the APC or Memcache as Pelle ten Cate mentioned can be used. The correct approach to use does depend in your situation though, do you have your website or application running on more than server and does the information in the cache need to be shared between those servers? (if yes then something like memcache is your answer, or maybe a database or distributed NoSQL solution if you are feeling brave). If you code is only running on the one server you could try something simple like serializing your variables, and writing them to disk, then on every request afterwards, see if the files exists, if it does, open it and unserialize the string into the variable you need. This though is only worth it if it would take a long time to generate the varaible normally, (e.g longer than it would to open,read,unserialize the file on disk)

For HTML caching you are generally going to get the most mileage from using a proxy like Varnish or Squid to do it for you but i realise that this may not be an option for you. If its not then you could the write to disk approach i mentioned above, and save chunks of HTML to files. look in the PHP manual for ob_start and its friends.

Upvotes: 0

Pelle
Pelle

Reputation: 6578

Most PHP build don't have a caching mechanism built in. There are extensions though that can take care of caching for you.

Have a look at APC or MemCache

Upvotes: 3

Related Questions