Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php caching techniques

Hi this is more of an information request really.

I'm currently working on a pretty large event listing website and have started thinking about some caching for the data sets being used.

I have been messing with APC this week and have seen some real improvements during testing however what I'm struggling to get my head around is best practices and techniques required when trying to cache data that changes frequently.

Say for example the user hits the home page, this by default displays the latest 10 events happening and if that user is logged in those events are location specific. Is it possible to deploy some kind of caching system when dealing with logged in states and data that changes frequently, the system currently allows the user to "show more events: which is an ajax request to pull extra results from the db.

I haven't really found anything on this as I'm not sure what to search for but I'm really interested to know the techniques used for advanced caching systems that deal especially with data that changes and data specific to users?

I mean is it even worth it? are the other performance boosters when dealing with this sort of criteria?

Any articles or tips and info on this will be greatly appreciated!! Please let me know if any other info is required!!

like image 287
Mike Waites Avatar asked May 06 '11 17:05

Mike Waites


People also ask

Does PHP get cached?

PHP by itself does not do caching. But there are opensource frameworks to let the programmer design their code to work this way. In other words, it lets the code check if the data has already been retrieved before it goes and fetches it again, which is a relatively slow process since it has to query the database.

What are caching techniques?

In computing, a cache is a high-speed data storage layer which stores a subset of data, typically transient in nature, so that future requests for that data are served up faster than is possible by accessing the data's primary storage location.


2 Answers

Your basic solutions are:

  • file cache
  • memcached/redis
  • APC

Each used for slightly different goal.

File cache is usually something that you utilize when you can pre-render files or parts of them. It is used in templating solutions, partial views (mvc), css frameworks. That sort of stuff.

Memcached and redis are both more or less equal, except redis is more of a noSQL oriented thing. They are used for distributed cache ( multiple servers , same cached data ) and for storing the sessions, if you have cluster of webservers.

APC is good for two things: opcode cache and data cache. Faster then memcached, but works for each server separately.


Bottom line is : in a huge project you will use all of them. Each for a different task.

like image 196
tereško Avatar answered Sep 30 '22 18:09

tereško


So you have opcode caching, which speeds things up by saving already compiled PHP files in cache.

Then you have data caching, where you save variables or objects that take time to get like data built from SQL queries.

Then you have output caching, which is where you save entire blocks of your webpages in files, and output those files instead of building that block of your webpage on each request.

I once wrote a blog post about how to do output caching:

http://www.spotlesswebdesign.com/blog.php?id=17

If it's location specific, and there are a billion locations, your best bet is probably output caching assuming you have a lot of disc space, but you will have to use your head for what is best, as each situation is very different when it comes to how best to apply caching.

like image 41
dqhendricks Avatar answered Sep 30 '22 18:09

dqhendricks