Saturday, April 25, 2009

Memcache for Code and Performance

"Memcache for Code and Performance "

When website has high traffic then continuos every time fetch record from db and show on the client side is tedious job and we need high performance then we use Memcache.

What is Memcache?

"memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load."

What is MemcacheDB?

It's a distributed key-value storage system designed for persistent. It is NOT a cache solution, but a persistent storage engine for fast and reliable key-value based object storage and retrieval. It conforms to memcache protocol(not completed, see below), so any memcached client can have connectivity with it. MemcacheDB uses Berkeley DB as a storing backend, so lots of features including transaction and replication are supported.

Before you get too excited keep in mind this is a key-value store. You read and write records by a single key. There aren't multiple indexes and there's no SQL. That's why it can be so fast.

The original client for memcached was written in Perl by the folks at Danga Interactive. The Python client is now maintained by a 3rd party group. The PHP client is available as a PECL package.PHP client is written in C as a PHP extension. The other two are written in Perl and Python. So, all the complex code has to be interpretted. For PHP however, most of the work happens in the engine, not in PHP code. This makes the PHP client far superior in performance to the other two. By default, memcached uses the port 11211.

The system is used by several very large, well-known sites including YouTube, LiveJournal, Slashdot, Wikipedia, SourceForge, ShowClix, GameFAQs, Facebook, Digg, Twitter, Fotolog, BoardGameGeek,, deviantART, Jamendo, Kayak and in our Mobshare too.

PHP provides support for the Memcache functions through a PECL extension. To enable the PHP memcache extensions, you must build PHP using the --enable-memcache option to configure when building from source.

A few important things about memcache:

Memcache is a daemon, meaning it runs as a separate service on your machine. Just like MySQL runs as a separate service. In fact, to use memcache in PHP you have to connect to it, just like MySQL.

Think of memcache as the $_SESSION variable for PHP, but instead of it working on a per-user basis, it runs over the entire application — like MySQL. In fact, you can use memcache as you session handler for PHP.

Memcache to work on windows machine:

Download memcache [grab the 'win32 binary' version]

Install memcache as a service:

  • Unzip and copy the binaries to your desired directory (eg. c:\memcached) [you should see one file, memcached.exe] - thanks to Stephen for the heads up on the new version

  • If you’re running Vista, right click on memcached.exe and click Properties. Click the Compatibility tab. Near the bottom you’ll see Privilege Level, check “Run this program as an administrator”.

  • Install the service using the command: c:\memcached\memcached.exe -d install from the command line

  • Start the server from the Microsoft Management Console or by running one of the following commands: c:\memcached\memcached.exe -d start, or net start "memcached Server"

Now that you have memcache installed, you’ll have to tie it in with PHP in order to use it.

1. Check your php extensions directory [should be something like: C:\php\ext] for php_memcache.dll
If you don’t have any luck finding it, try looking here: [or look here for PHP 5.2.*]

2. Now find your php.ini file [default location for XP Pro is C:\WINDOWS\php.ini] and add this line to the extensions list:


3. Restart apache

4. Run this code to test the installation:


$memcache = new Memcache;

"Server's version: " . $memcache->getVersion();

$tmp_object = new stdClass;
$tmp_object->str_attr = "test";
$tmp_object->int_attr = 123;

echo "Store data in the cache (data will expire in 10 seconds)";

echo "Data from the cache:";

If you see anything but errors, you are now using memcache!


Memcached, by default, loads with 64mb of memory for it’s use which is low for most applications. To change this to something else, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\memcached Server in your registry, find the ImagePath entry and change it to look something like this:

“C:\memcached\memcached.exe” -d runservice -m 512

Now when you start the service via net start “memcached Server”, it will run with 512mb of memory at it’s disposal.

Memcache Functions:

The memcache object has several methods. With the Memcache::set method we can add an entry to the cache. The method had four arguments:

  • a key

  • any serializable variable we'd like to cache, in this case it's a string

  • a boolean whether we want to use on-the-fly zip compression using libz

  • the cache expiry measured in seconds

  • Ex: - $memcache->set("key",$tmp_object,false,10);
  • Memcache::add — Add an item to the server

  • Memcache::addServer — Add a memcached server to connection pool

  • Memcache::close — Close memcached server connection

  • Memcache::connect — Open memcached server connection

  • memcache_debug — Turn debug output on/off

  • Memcache::decrement — Decrement item's value

  • Memcache::delete — Delete item from the server

  • Memcache::flush — Flush all existing items at the server

  • Memcache::get — Retrieve item from the server

  • Memcache::getExtendedStats — Get statistics from all servers in pool

  • Memcache::getServerStatus — Returns server status

  • Memcache::getStats — Get statistics of the server

  • Memcache::getVersion — Return version of the server

  • Memcache::increment — Increment item's value

  • Memcache::pconnect — Open memcached server persistent connection

  • Memcache::replace — Replace value of the existing item

  • Memcache::setCompressThreshold — Enable automatic compression of large values

  • Memcache::setServerParams — Changes server parameters and status at runtime

The data is converted into a string with the serialize() function, so numbers, strings, arrays, and most objects will be stored easily… only resources (file descriptors, database connections, MemCache connections) can not be stored effectively.

It is important to keep in mind that if the MemCached server crashes or the process dies, all your cache data will be lost. Always store the actual data in a database server or another persistent storage system.


While the example we created is fairly trivial, it’s easy to see how this technique can be expanded throughout your site. Other queries and other data can be cached with similar benefits. Obviously, MemCached isn’t the only solution to web scaling and data caching, but it can be a pretty useful. Often, as seen in our example, a MemCached solution can be implemented and put in place with minimal code changes, minimal time, and, hopefully, minimum downtime for your users!

Future of Web Development with Hypertext Preprocessor V6

"Future of Web Development with Hypertext Preprocessor V6"

Are you ready for PHP V6? If you were upgrading tomorrow, would your scripts execute just fine or would you have work to do?

With the "PHP 6 Developers Meeting Notes" out I finally took a while to go and read through them and figure out what is the best way to move forward and start writing code that will be compatible not only for 4x and 5x but continue on though 6x.

New PHP V6 features

  • Unicode support:
    The primary alteration in PHP6 is support for Unicode(

    "Unicode is a computing industry standard allowing computers to consistently represent and manipulate text expressed in most of the world's writing systems.An encoding scheme that allows for virtually all major world languages to be encoded into a single character set. " )

    Much improved for PHP V6 is support for Unicode strings in many of the core functions. This new feature has a big impact because it will allow PHP to support a broader set of characters for international support. So, if you're a developer or architect using a different language, such as the Java™ programming language, because it has better internationalization (i18n) support than PHP, it'll be time to take another look at PHP when the support improves.

  • Namespaces:
    Namespaces are a way of avoiding name collisions between functions and classes without using prefixes in naming conventions that make the names of your methods and classes unreadable. So by using namespaces, you can have class names that someone else might use, but now you don't have to worry about running into any problems.

  • Move XML extension :
    The XMLReader and XMLWriter extensions will move into the core distribution and will be on by default.

  • Move Fileinfo exntesion :
    The extremely useful Fileinfo exntesion will move into the core distribution and enabled by default.

  • Static Binding :
    A new keyword will be created to allow for late static binding - static::static2(), this will perform runtime evaluation of statics.

  • FastCGI always on :
    The FastCGI code will be cleaned up and always enabled for the CGI SAPI, it will not be able to be disabled.

  • 64 bit integers added :
    64 bit integers added(int64), no int32(it is assumed unless you specify int64).

  • "break" keyword extended :
    break keyword will be extended with a static label - so you could do 'break first' and it'll jump to the label first: in your code.

  • foreach multi-dim arrays :
    foreach multi-dim arrays - you'll be able to foreach through array lists, i.e. "foreach( $a as $k => list($a, $b))".

  • {} vs [] :
    You can currently use both {} and [] to access string indexes. But the {} notation will raise an E_STRICT in PHP5.1 and will be gone totally in PHP6. Also the [] version will gain substr and array_slice functionality directly - so you could do "[2,]" to access characters 2 to the end, etc.

  • E_STRICT merges into E_ALL :
    This is quite a serious one! E_STRICT level messages will be added to E_ALL by default. This shows a marked move by the PHP team to educate developers on 'best practises' and displaying language-level warnings.

  • dl() only enable it when a SAPI(Server API) layer registers it explicitly

  • Make "var" an alias for "public" and remove the warning for it

Things removed from PHP V6

In addition to having new features, PHP V6 will not have some other functions and features that have been in previous versions. Most of these things, such as register_globals and safe_mode, are widely considered "broken" in current PHP, as they may expose security risks.

Features that will be removed from the PHP version include:

  • Magic Quotes to go :
    The magic quotes feature of PHP will be going, and as with register globals it's going to raise an E_CORE_ERROR if the setting is found anywhere. This will affect magic_quotes, magic_quotes_sybase and magic_quotes_gpc.

  • Register Globals to go :
    It will no longer be an ini file setting, and if found it will raise an E_CORE_ERROR,finally break all PHP3 era scripts with no recourse at all but to re-code it.

  • Register Long Arrays to go :
    Remember the HTTP_*_VARS globals from yesteryear? Well if you're not already using $_GET, $_POST, etc - start doing so now, because the option to enable long arrays is going (and will throw an E_CORE_ERROR).

  • Safe Mode to go :
    This may please developers who have web hosts that insist upon safe mode! But it will now go totally, again raising an E_CORE_ERROR if found. The reason is that apparently they felt it gave the 'wrong signal', implying that it made PHP secure, when infact it didn't at all. open_basedir will (thankfully) be kept.

  • Short php tags :
    Microsoft Active Server Pages (ASP)-style tags — the shorter version of the PHP tags — are no longer supported

  • ereg extension :
    The ereg extension, which supports Portable Operating System Interface (POSIX) regular expressions, is being removed from core PHP support.

  • Return by Reference will error :
    Don't initiate objects with the reference operator(E_STRICT error)

  • Stop Freetype 1 and GD 1 support
    Stop Freetype 1(TrueType rendering engine library) and GD 1(dynamic creation of images library) support

Some of the features mentioned here have also been ported to PHP V5.3, so that when you move to V6 of PHP, it'll be less of a jump.
The following list of features have been back-ported to V5.3, such as - " Namespaces"


That is about all If you are still using php4 to php5 compatibility, you might want to think about removing PHP5 in the coming months since there is already 6.0 dev versions out. Yes I know you guys do not like to give up your old ways(adoption of PHP6 even slower than that of PHP5 becuase of no backward compatibility) but it is time to move on.

Now PHP is becoming closer to enterprise apps. PHP6 is not a software revolution, but rather a standards evolution, which is sometimes more important in enterprise applications.