views:

124

answers:

2

Hello

I have enabled the memcache extension for my PHP Apache localserver server (XAMPP)

but I don't know how to use it to speed up my PHP scripts Is writing the following code in the top of every page I want to speed up is enough? or I should do something else?

       /* procedural API */
       $memcache_obj = memcache_connect('127.0.0.1', 11211);

       /* OO API */
       $memcache = new Memcache;
       $memcache->connect('127.0.0.1', 11211);

the phpinfo() shows the following details

        memcache support    enabled
        Active persistent connections   0
        Version 2.2.4-dev
        Revision    $Revision: 1.99 $

Thanks

+6  A: 

You might want to consider using memcache_pconnect() instead but there are pros and cons to that.

You've got memcache setup. It won't speed up anything unless you use it for something. There are four basic things you can cache:

  1. Loading stuff from a file;
  2. Loading stuff from a database;
  3. Loading stuff from a remote host; and
  4. Storing the result of something that takes significant time to calculate.

Until you use it for something, it won't do anything.

cletus
+1  A: 

You need to do some basic profiling of your code and the algorithms it uses. Memcache is like adding salt to a recipe: you need to add it to the right part or you don't get the benefit and instead gets in the way.

In PHP, there are several types of caching that can improve performance of your web pages, and memcache is only one of them. If you have lots of identical queries, you can turn the query cache on in your database. If you have many pages connecting with little query traffic, you would benefit from connection pooling (this caches the TCP connection) and thread caching in the DB (this caches CPU resources on the DB).

If you are throwing lots of data around, particularly strings and big arrays or objects, you can start thinking about references and updating your string handling that doesn't involve unnecessary re-copying (e.g. concatenating strings in a loop is expensive - add them to an array and implode it once at the end). This can be a big performance and memory improvement.

If you have lots of queries getting the same information over and over again in one page, you need to improve your data storage layer. This will allow you to make one query to get a row and then re-use the data throughout the page instead of fetching it again. (This is usually the best reason to go from a hotch-potch of arbitrary SQL to an object persistence layer.) Note that this may involve a considerble re-architect of the app. You can add caching of object data to your object layer - this will benefit everything that uses that sort of object, which can be a big win. This is a good point for memcache, and the application code doesn't even have to know. Just be careful of data lifetimes and if or how it is updated early.

If you have lots of pages making the same somewhat expensive query (even by proxy through a data-access layer), then you can cache that for a few seconds. It doesn't have to be to memcache, but that is a good choice.

Finally, if you have lots of files that never change, you can use a separate server that only servers static files. This gives your execution environment more room to execute code and you can tune the static files servers to be good at returning files fast. This can be a big win for perception of website performance.

IMO, caching should improve rather than enable your application.

staticsan