PHP APC To cache or not to cache?

I actually have no caching experience, so this may seem like a dumb question, but how do you know when to cache your data? I couldn’t even find one site that talked about this, but it could be just my search skills or maybe too many variables to consider?

I will most likely use APC. Does anyone have examples of what would be the least amount of data you need to cache it? For example, let's say you have an array of 100 elements, and you use a foreach loop for it and do some simple manipulation of arrays if you cache the result? How about if he had 1,000 items, 10,000 items, etc.?

Should I cache the results of a database query? What queries should you cache? I assume that a simple choice and, possibly, an expression about joining a pair to mysql db does not require caching, or is it like that? Assuming mysql query cache is enabled, does this mean you don't need to cache at the application level or are you still doing this?

If you instantiate an object, do you need to cache it? How to determine whether to cache or not? So a general caching guide would be nice, examples would also be very helpful, thanks.

+8
php caching apc
source share
2 answers

When you look at caching data that has been read from a database in APC / memcache / WinCache / redis / etc, you should know that it will not be updated when the database is updated, unless you explicitly specify the code to keep the database synchronized and cache. Therefore, caching is most effective when the data from the database does not change often, but also requires a more complex and / or expensive query to retrieve this data from the database (otherwise you can also read it from the database when you need it). .. so costly join requests that return the same data records when they start are the first candidates. And always check if queries from the database are queried faster than from the cache. Correct database indexing can significantly improve database access time, especially because most databases also support their own internal cache, so do not use APC or the cache data equivalent if this does not justify its overhead.

You also need to know the use of space in the cache. Most caches are fixed in size and you do not want to overflow them ... so do not use them to store large amounts of data. Use the apc.php script available through APC to monitor cache usage (although make sure that it is not accessible to anyone and anyone who accesses your site .... poor security).

When storing objects in the cache, the object will be serialized () when it is saved, and unserialized () when it will be extracted, so there is overhead. Objects with resource attributes will lose this resource; therefore, do not save database access objects.

It is reasonable to use the cache only for storing information that is accessed by many / all users, and not from user data. For information about a user session, stick with regular PHP sessions.

+10
source share

The simple answer is that you cache data when something slows down. Obviously, for any medium to large sized application, you need to do a lot more planning than just wait and see the approach. But for the vast majority of websites, the question arises here: "Are you satisfied with the download time." Of course, if you are obsessive about load times, like me, you will want to try to do it even faster, despite this.

Then you must determine what exactly is the cause of the slowness. You suggested that your application code was the source, but it is worth exploring if there are other external factors, such as the large page file size, excessive requests, lack of gzip, etc. Use a site like http://tools.pingdom.com/ or an extension like yslow to get started. (quick tip make sure keepalives and gzip work).

Assuming the problem is the length of time your application code runs, you will want to profile your code with something like xdebug (http://www.xdebug.org/) and view the output with kcachegrind or wincachegrind. This will let you know which parts of your code are time consuming. From there, you will make decisions about what to cache and how to cache (or improve the logic of your code).

There are so many possibilities for solving a problem and related solutions that I should not guess. So, as soon as you identify the problem, you can post a new question related to solving this particular problem. I will say that if it is not used properly, the mysql query cache can be productive. Also, I generally avoid the APC user cache in favor of memcached.

0
source share

All Articles