2

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

5
  • 2
    What are you looking to cache? Commented Jan 20, 2011 at 12:16
  • By "cache" do you mean simply caching served content, or the use of systems such as memcached, etc.? (Please update your question to add clarity.) Commented Jan 20, 2011 at 12:19
  • HTML output , and some variables sometimes Commented Jan 20, 2011 at 12:19
  • 2
    I'd ask WHY do you want to cache Commented Jan 20, 2011 at 12:50
  • So you’re looking for server side caching methods? Commented Jan 20, 2011 at 12:53

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Why not? You can easily store the computed HTML into the APC cache for later use. Isn't that exactly what you need?
0

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.

2 Comments

thanks ! one question , my data that i want to store is fetched from database , so i want to reduce the queries thats why i want to cache the results (where results is the generated HTML page containing data of the database aka a forum thread or something similar) , so the ONLY way to do it is saving the HTML output to a file rather than using the database over and over ? or there's another way than saving the output to a file ? memcache would be slow i guess , i use one server .
if you only use one server, then there is no real point in using memcache. Whats making your database queries slow? is it that you have to do lots of them per page, or is that they are complicated and slow to run? Either way, you could still save the cached output to DB, as the query to access the cached item would very simple and quick... SELECT content FROM cache_table WHERE id = 2;
0

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.

6 Comments

Well, strictly speaking the $_SESSION global is something that can be shared between multiple requests from the same client, but memcache or APC are still the ways to go if you don't want to rely on a database or the filesystem.
Yes, I know, and that's why I wrote "basic view". Of course there are some ways around this, and you could e.g. serialize() stuff to a file and read it back, storing the filename in a $_SESSION key and so on. However, this approach may be slower rather than faster, depending on what you do. Specialized extensions can do a good job on their respective areas, but before picking any of them at random, it's important to check what the actual requirement / goal is.
i want to give the site admin option to select cache method he likes , so i just need all available cache methods in php thats all
General available cacheing methods in PHP? There are none. - You can implement specific cacheing, but need to write specific code for that. There is no simple switch to "turn on cacheing in PHP".
well i know that @foo , am asking of how can i cache html information to implement the technique into code and use it , another technique into another code and use it , then allow the admin to choose between the 2 techniques !
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.