添加Memcached/APC需要重新写代码吗?
我计划将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 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 :
首先,您只需安装 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: