M G SUKHON

RnD on Typical PHP problem

Memcache server

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.

The memcached server and clients work together to implement one global cache across as many machines as you have. In fact, it’s recommended you run both web nodes (which are typically memory-lite and CPU-hungry) and memcached processes (which are memory-hungry and CPU-lite) on the same machines. This way you’ll save network ports.

Installation

You have enable memcache extension from php.ini file.

Download memcache daemon from  http://jehiah.cz/projects/memcached-win32/

To install memcached as a service, follow the next steps:

  1. Unzip the binaries in your desired directory (eg. c:\memcached)
  2. Install the service using the command: ‘c:\memcached\memcached.exe -d install’ from either the command line
  3. Start the server from the Microsoft Management Console or by running the following command: ‘c:\memcached\memcached.exe -d start’
  4. Use the server, by default listening to port 11211

Sometimes  it says couldn’t connect because i was n`t running the daemon. When you install the memcached it only install the extension and this gives the interface (functions) to access the memcache but to be able to store and retrieve data you need to install and run the daemon.

Sample Code:

$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

$version = $memcache->getVersion();
echo
"Server's version: ".$version."<br/>\n";

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

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo
"Store data in the cache (data will expire in 10 seconds)<br/>\n";

$get_result = $memcache->get('key');
echo
"Data from the cache:<br/>\n";

var_dump($get_result);

Conclusion

Memcahce is very faster than shm_op. It uses libevent to scale to any number of open connections (using epoll on Linux, if available at runtime), uses non-blocking network I/O, refcounts internal objects (so objects can be in multiple states to multiple clients), and uses its own slab allocator and hash table so virtual memory never gets externally fragmented and allocations are guaranteed.

Reference: http://danga.com/memcached/

June 11, 2009 - Posted by mgsukhon | PHP Cache Handler | | 1 Comment

1 Comment »

  1. This is really a useful content about memcache manager which make the website more perfect against multiple concurrent users.

    Comment by william | August 24, 2009 | Reply


Leave a comment