添加Memcached/APC需要重新写代码吗?

发布于 2024-11-01 22:31:47 字数 403 浏览 5 评论 0原文

我计划将 APC 或 MEMCACHED 添加到我的 PHP 代码中! 我的问题是它需要重写所有代码吗? 当我查看 PHP 手册时,我得到了这个!

function get_foo(foo_id)
 foo = memcached_get("foo:" . foo_id)
 return foo if defined foo

 foo = fetch_foo_from_database(foo_id)
 memcached_set("foo:" . foo_id, foo)
 return foo
end

因此,为了存储变量,我需要执行 memcached_set(...) 或者就像我添加插件并获得性能提升!

我对 APC / Memcache 不了解,所以欢迎对此进行任何讨论

I'm planing to add APC or MEMCACHED to my PHP code !
my question is does it require to rewrite all the code ?
as i looked in to PHP Manual and there i got this !

function get_foo(foo_id)
 foo = memcached_get("foo:" . foo_id)
 return foo if defined foo

 foo = fetch_foo_from_database(foo_id)
 memcached_set("foo:" . foo_id, foo)
 return foo
end

So, for storing the variable i need to do memcached_set(...) or it's like that i add the plugin and get the performance boost !

I have no idea on APC / Memcache, so any discussion on this is welcome

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

庆幸我还是我 2024-11-08 22:31:47

使用 APC,您首先获得一个操作码缓存 - 对于这一部分,您无需在代码中进行任何修改:只需安装扩展并启用它即可。

操作码缓存通常会加快速度:它通过将操作码(PHP 文件编译的结果)保留在内存中,防止 PHP 脚本被一次又一次地编译。

然后,APC和memcached允许将数据存储在内存中;通常,这用于缓存长时间/成本高昂的操作的结果(例如复杂的 SQL 查询、Web 服务调用……)。

关于这一点,没有什么神奇之处:您必须编写一些代码,将数据存储到缓存中,然后从缓存中获取数据——如果数据不在缓存中或缓存已过期,则执行耗时/成本高昂的操作。

以下是一些问题/答案,可能会为您提供一些额外的信息:

With APC, you first get an opcode cache -- for that part, you having nothing to modify in your code : just install the extension, and enable it.

The opcode cache will generally speed up things : it prevents the PHP scripts from being compiled again and again, by keeping the opcodes -- the result of the compilation of the PHP files -- in memory.

Then, APC and memcached allow one to store data in memory ; typically, this is used to cache the result of long/costly operations (like complex SQL queries, webservices calls, ...).

About that, there is no magic : you will have to code a bit, to store data into the cache, and fetch it from it -- doing the long/costly operation if the data is not in cache, or the cache has expired.

Here are a couple of questions/answsers that might get you some additional informations :

奢华的一滴泪 2024-11-08 22:31:47

首先,您只需安装 APC 即可获得性能提升。当执行脚本时,它会通过 Zend_Compile 运行,将 PHP 代码转换为 OPCODES,然后通过 Zend_Execute 运行。每次页面加载时将 PHP 转换为 OPCODES 的过程都是相同的,因此下次再这样做是一种浪费。 APC(替代 PHP 缓存)将这些操作码保存在内存中,因此下次可以跳过该步骤并使页面加载速度更快。

当涉及到脚本中的缓存时,您将需要进行一些更改。您可以随着时间的推移逐步进行这些更改,每次都会获得更高的性能,因此您无需担心一次完成所有这些操作。如果您有一台服务器,我会使用 APC,如果您将来可能有多个服务器,我会使用 Memcache。

提高性能的唾手可得的成果:

  • 查找加载次数较多的内容,例如您的主页。使用额外的函数重写该控制器,该函数检查缓存中是否有数据,如果可用则使用它。如果没有,则以旧方式加载,并将其存储在缓存中。请记住,您可以将数组和对象放入这两个数据存储中,因此应该很快就能完成。
  • 查看需要很长时间执行的数据库查询,并将它们缓存起来。
  • 寻找其他计算成本较高的东西来缓存,这些很大程度上取决于您的特定应用程序。

First you will get a performance boost just for installing APC. When a script is executed it is run through Zend_Compile that turns your PHP code into OPCODES that it then runs through Zend_Execute to run. The process of turning PHP into OPCODES is identical every time the page loads, so doing it again next time is a waste. APC (Alternative PHP Cache) saves those opcodes in memory, so next time it can skip that step and make the page load faster.

When it comes to caching in your script, you will need to make some changes. You can make these changes incrementally over time, getting a bit more performance each time, so you don't need to worry about doing it all at once. If you've got a single server I'd use APC, if you may have multiple servers in the future I'd go with Memcache.

Low hanging fruit for performance improvements:

  • Find things that are loaded a lot, like your home page. Re-write that controller with an extra function that checks to see if there's data in the cache, and uses it if it's available. If not, load it the old way, and store it in the cache. Remember that you can put arrays and objects in both of these data stores, so it should be pretty quick to do.
  • Look at database queries that take a long time to execute, cache them as well.
  • Find other computationally expensive things to cache, these will depend a lot on your specific application.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文