phpmemcachedapcmemcachedb

Does adding Memcached/APC require writing codes again?


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


Solution

  • 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 :