什么时候应该开始使用Memcached?

时间:2021-04-30 20:34:12

I'm starting to learn about using Memcached with PHP and I was wondering; is there a point at which you should start using it? Is it always more efficient to cache data or does it only become effective once your site gets a certain number of hits? Presumably there is an overhead associated with Memcached, so when does the speed benefit outweigh this?

我开始学习如何在PHP中使用Memcached,我很想知道;你应该开始使用它吗?缓存数据总是更有效率,还是只有在您的网站获得一定数量的点击后才会生效?据推测,存在与Memcached相关的开销,那么什么时候速度优势超过这个?

Thanks in advance for any advice.

提前感谢任何建议。

4 个解决方案

#1


9  

You should start using memcached when not using it starts to affect your site / server.

您应该在不使用时开始使用memcached开始影响您的站点/服务器。

So when you just have 100 visitors a day and your site still responds quickly, don't bother using it.

因此,当您每天只有100位访问者并且您的网站仍然快速响应时,请不要费心使用它。

But when you have running times somewhere near 200ms or more per page, and the site feels slow too, you should look out for the bottlenecks using some PHP profiler (XDebug integrates one that you can use in combination with WinCacheGrind or KCacheGrind). Especially caching database requests should save you some load time.

但是当你的运行时间接近每页200毫秒或更多,并且网站也感觉很慢时,你应该使用一些PHP分析器来查找瓶颈(XDebug集成了一个可以与WinCacheGrind或KCacheGrind结合使用的)。特别是缓存数据库请求应该节省一些加载时间。

#2


5  

The #1 rule when it comes to performance optimization is: Don't optimize until you need to!

性能优化方面的首要原则是:在需要之前不要进行优化!

If you don't have performance problems, then you don't need to optimize. Other good rules include:

如果您没有性能问题,则无需进行优化。其他好的规则包括:

  • Don't optimize until you're done writing the application. You rarely know where real-world performance problems will crop up while developing, and it'll only make your code sloppier and more difficult to modify when you need to make a change down the road.

    在完成应用程序编写之前不要进行优化。你很少知道在开发过程中会出现什么样的现实性能问题,而且只有当你需要在路上做出改变时,它才会使你的代码更加笨拙,更难以修改。

  • Use a profiler or some other way of measuring specific chunks of code to actually measure what's taking a long time, and optimize those sections. Don't just guess at what's taking a long time. Actually measure it.

    使用分析器或其他一些测量特定代码块的方法来实际测量需要花费很长时间的内容,并优化这些部分。不要只是猜测需要花费很长时间。实际测量它。

#3


3  

If you're using MySQL as database, run show processlist command on MySQL console during the peak time. If there're many queries from the PHP side waiting for more than 500ms, it's time you turned to memcached.

如果您使用MySQL作为数据库,请在高峰时间在MySQL控制台上运行show processlist命令。如果有很多来自PHP端的查询等待超过500毫秒,那么你应该转向memcached。

#4


1  

First think about what/when/if to cache, before you think about if you should be using Memcached to do the caching.

在考虑是否应该使用Memcached进行缓存之前,首先考虑什么/何时/是否要缓存。

When you have some “object” that is used to respond to lots of requests that takes lots of database reads to create. And your site cannot keep up with its load due to recreating that “object” offen. You also need a nice well-defined key for the item you are caching.

当你有一些“对象”用于响应大量需要创建大量数据库读取的请求时。由于重新创建了“对象”,您的网站无法跟上其负载。对于要缓存的项目,还需要一个定义良好的键。

The “object” you are caching must be read a lot more often then the database rows used to create it is changed, otherwise the benefit of cache is likely to be less the cost of managing the cache after time a bit of data is changed.

您正在缓存的“对象”必须经常读取,然后更改用于创建它的数据库行,否则缓存的好处可能是在更改一些数据后管理缓存的成本。

Caching single database rows will not give you match benefit.

缓存单个数据库行不会给您带来匹配的好处。

#1


9  

You should start using memcached when not using it starts to affect your site / server.

您应该在不使用时开始使用memcached开始影响您的站点/服务器。

So when you just have 100 visitors a day and your site still responds quickly, don't bother using it.

因此,当您每天只有100位访问者并且您的网站仍然快速响应时,请不要费心使用它。

But when you have running times somewhere near 200ms or more per page, and the site feels slow too, you should look out for the bottlenecks using some PHP profiler (XDebug integrates one that you can use in combination with WinCacheGrind or KCacheGrind). Especially caching database requests should save you some load time.

但是当你的运行时间接近每页200毫秒或更多,并且网站也感觉很慢时,你应该使用一些PHP分析器来查找瓶颈(XDebug集成了一个可以与WinCacheGrind或KCacheGrind结合使用的)。特别是缓存数据库请求应该节省一些加载时间。

#2


5  

The #1 rule when it comes to performance optimization is: Don't optimize until you need to!

性能优化方面的首要原则是:在需要之前不要进行优化!

If you don't have performance problems, then you don't need to optimize. Other good rules include:

如果您没有性能问题,则无需进行优化。其他好的规则包括:

  • Don't optimize until you're done writing the application. You rarely know where real-world performance problems will crop up while developing, and it'll only make your code sloppier and more difficult to modify when you need to make a change down the road.

    在完成应用程序编写之前不要进行优化。你很少知道在开发过程中会出现什么样的现实性能问题,而且只有当你需要在路上做出改变时,它才会使你的代码更加笨拙,更难以修改。

  • Use a profiler or some other way of measuring specific chunks of code to actually measure what's taking a long time, and optimize those sections. Don't just guess at what's taking a long time. Actually measure it.

    使用分析器或其他一些测量特定代码块的方法来实际测量需要花费很长时间的内容,并优化这些部分。不要只是猜测需要花费很长时间。实际测量它。

#3


3  

If you're using MySQL as database, run show processlist command on MySQL console during the peak time. If there're many queries from the PHP side waiting for more than 500ms, it's time you turned to memcached.

如果您使用MySQL作为数据库,请在高峰时间在MySQL控制台上运行show processlist命令。如果有很多来自PHP端的查询等待超过500毫秒,那么你应该转向memcached。

#4


1  

First think about what/when/if to cache, before you think about if you should be using Memcached to do the caching.

在考虑是否应该使用Memcached进行缓存之前,首先考虑什么/何时/是否要缓存。

When you have some “object” that is used to respond to lots of requests that takes lots of database reads to create. And your site cannot keep up with its load due to recreating that “object” offen. You also need a nice well-defined key for the item you are caching.

当你有一些“对象”用于响应大量需要创建大量数据库读取的请求时。由于重新创建了“对象”,您的网站无法跟上其负载。对于要缓存的项目,还需要一个定义良好的键。

The “object” you are caching must be read a lot more often then the database rows used to create it is changed, otherwise the benefit of cache is likely to be less the cost of managing the cache after time a bit of data is changed.

您正在缓存的“对象”必须经常读取,然后更改用于创建它的数据库行,否则缓存的好处可能是在更改一些数据后管理缓存的成本。

Caching single database rows will not give you match benefit.

缓存单个数据库行不会给您带来匹配的好处。