简体   繁体   English

PHP APC要缓存还是不缓存?

[英]PHP APC To cache or not to cache?

I don't really have any experience with caching at all, so this may seem like a stupid question, but how do you know when to cache your data? 我根本没有任何缓存经验,所以这看起来像是一个愚蠢的问题,但你怎么知道何时缓存你的数据? I wasn't even able to find one site that talked about this, but it may just be my searching skills or maybe too many variables to consider? 我甚至找不到一个谈论这个的网站,但它可能仅仅是我的搜索技巧或者可能需要考虑的太多变量?

I will most likely be using APC. 我很可能会使用APC。 Does anyone have any examples of what would be the least amount of data you would need in order to cache it? 有没有人有任何缓存它需要的数据量最少的例子? For example, let's say you have an array with 100 items and you use a foreach loop on it and perform some simple array manipulation, should you cache the result? 例如,假设您有一个包含100个项目的数组,并且您在其上使用foreach循环并执行一些简单的数组操作,是否应该缓存结果? How about if it had a 1000 items, 10000 items, etc.? 如果它有1000件物品,10000件物品等怎么样?

Should you be caching the results of your database query? 你应该缓存数据库查询的结果吗? What kind of queries should you be caching? 你应该缓存什么样的查询? I assume a simple select and maybe a couple joins statement to a mysql db doesn't need caching, or does it? 我假设一个简单的选择,也许一对mysql数据库的连接语句不需要缓存,或者它呢? Assuming the mysql query cache is turned on, does that mean you don't need to cache in the application layer, or should you still do it? 假设启用了mysql查询缓存,这是否意味着您不需要在应用程序层中缓存,或者您是否还应该这样做?

If you instantiate an object, should you cache it? 如果你实例化一个对象,你应该缓存它吗? How to determine whether it should be cached or not? 如何确定是否应该缓存? So a general guide on what to cache would be nice, examples would also be really helpful, thanks. 因此,关于缓存内容的一般指南会很好,示例也非常有用,谢谢。

When you're looking at caching data that has been read from the database in APC/memcache/WinCache/redis/etc, you should be aware that it will not be updated when the database is updated unless you explicitly code to keep the database and cache in synch. 当您在缓存从APC / memcache / WinCache / redis / etc中的数据库中读取的数据时,您应该知道在更新数据库时它不会更新,除非您明确编写代码以保留数据库和缓存同步。 Therefore, caching is most effective when the data from the database doesn't change often, but also requires a more complex and/or expensive query to retrieve that data from the database (otherwise, you may as well read it from the database when you need it)... so expensive join queries that return the same data records whenever they're run are prime candidates. 因此,当来自数据库的数据不经常更改时,缓存最有效,但是还需要更复杂和/或更昂贵的查询来从数据库中检索该数据(否则,您也可以在数据库中从数据库中读取它需要它)...如此昂贵的连接查询在运行时返回相同的数据记录是主要候选者。 And always test to see if queries are faster read from the database than from cache. 并始终测试以查看从数据库读取查询是否比从缓存更快。 Correct database indexing can vastly improve database access times, especially as most databases maintain their own internal cache as well, so don't use APC or equivalent to cache data unless the database overheads justify it. 正确的数据库索引可以极大地改善数据库访问时间,尤其是大多数数据库也维护自己的内部缓存,因此除非数据库开销合理,否则不要使用APC或等效数据来缓存数据。

You also need to be aware of space usage in the cache. 您还需要了解缓存中的空间使用情况。 Most caches are a fixed size and you don't want to overfill them... so don't use them to store large volumes of data. 大多数缓存都是固定大小的,您不希望它们溢出...所以不要使用它们来存储大量数据。 Use the apc.php script available with APC to monitor cache usage (though make sure that it's not publicly accessible to anybody and everybody that accesses your site.... bad security). 使用APC提供的apc.php脚本来监控缓存使用情况(尽管确保任何人和访问您网站的所有人都无法公开访问它......安全性不佳)。

When holding objects in cache, the object will be serialized() when it's stored, and unserialized() when it's retrieved, so there is an overhead. 当在缓存中保存对象时,对象将在存储时被序列化(),并在被检索时被反序列化(),因此存在开销。 Objects with resource attributes will lose that resource; 具有资源属性的对象将丢失该资源; so don't store your database access objects. 所以不要存储数据库访问对象。

It's sensible only to use cache to store information that is accessed by many/all users, rather than user-specific data. 仅使用缓存来存储由许多/所有用户访问的信息而不是用户特定数据是明智的。 For user session information, stick with normal PHP sessions. 对于用户会话信息,请坚持使用普通的PHP会话。

The simple answer is that you cache data when things get slow. 简单的答案是当事情变慢时缓存数据。 Obviously for any medium to large sized application, you need to do much more planning than just a wait and see approach. 显然,对于任何中型到大型应用程序,您需要做更多的规划而不仅仅是等待和观察的方法。 But for the vast majority of websites out there, the question to ask yourself is "Are you happy with the load time". 但对于绝大多数网站而言,问自己的问题是“你对加载时间感到满意吗”。 Of course if you are obsessive about load time, like myself, you are going to want to try to make it even faster regardless. 当然,如果你像我一样迷恋加载时间,你会想要试着让它更快,无论如何。

Next, you have to identify what specifically is the cause of the slowness. 接下来,您必须确定导致缓慢的具体原因。 You assumed that your application code was the source but its worth examining if there are other external factors such as large page file size, excessive requests, no gzip, etc. Use a site like http://tools.pingdom.com/ or an extension like yslow as a start for that. 您假设您的应用程序代码是源代码,但是如果存在其他外部因素(如大页面文件大小,请求过多,没有gzip等),则需要检查它。使用http://tools.pingdom.com/等网站像yslow这样的扩展就是一个开始。 (quick tip make sure keepalives and gzip are working). (快速提示确保Keepalive和gzip正常工作)。

Assuming the problem is the duration of execution of your application code, you are going to want to profile your code with something like xdebug (http://www.xdebug.org/) and view the output with kcachegrind or wincachegrind. 假设问题是应用程序代码的执行持续时间,那么您将需要使用xdebug(http://www.xdebug.org/)来编写代码,并使用kcachegrind或wincachegrind查看输出。 That will let you know what parts of your code are taking long to run. 这将让您知道代码的哪些部分需要很长时间才能运行。 From there you will make decisions on what to cache and how to cache it (or make improvements in the logic of your code). 从那里,您将决定缓存内容以及如何缓存它(或改进代码逻辑)。

There are so many possibilities for what the problem could be and the associated solutions, that it is not worth me guessing. 对于问题可能存在的可能性和相关解决方案有很多可能性,我不值得猜测。 So, once you identify the problem you may want to post a new question related to solving that specific problem. 因此,一旦确定问题,您可能希望发布与解决该特定问题相关的新问题。 I will say that if not used properly, the mysql query cache can be counter productive. 我会说如果使用不当,mysql查询缓存可能会适得其反。 Also, I generally avoid the APC user cache in favor of memcached. 另外,我通常避免使用APC用户缓存来支持memcached。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM