开发者

php caching techniques

开发者 https://www.devze.com 2023-03-04 16:35 出处:网络
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.

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开发者_Go百科 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!!


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.


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.


If done correctly, using memcached or similar solutions can give huge boosts to site performance. By altering the cached data directly instead of rehydrating it from the database you can bypass the database entirely for data that either doesn't need to be saved or can be trivially rebuilt. Since the database is often the most critical component in web applications, any load you can take off it is a bonus.

On the other hand, making sure your database queries are as light and efficient as possible will have a much larger impact on performance than most cache tweaks.

0

精彩评论

暂无评论...
验证码 换一张
取 消