Thursday, February 17, 2011

PHP Memcache

PHP Memcache Manual
http://php.net/manual/en/book.memcache.php
Memcache module provides handy procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.

Note: there are two *distinct* memcache PHP implementations
1) pecl-memcache
2) pecl-memcached

Using Memcache vs Memcached with PHP
http://stackoverflow.com/questions/1442411/using-memcache-vs-memcached-with-php

Memcache vs Memcached
http://stackoverflow.com/questions/1825256/memcache-vs-memcached


Comparison
http://code.google.com/p/memcached/wiki/PHPClientComparison

Install on Fedora
yum install php-pecl-memcache
fedora 100% |=========================| 2.1 kB 00:00
http://rpm.livna.org/fedora/7/i386/repodata/repomd.xml: [Errno 12] Timeout:
Trying other mirror.
http://livna.cat.pdx.edu/fedora/7/i386/repodata/repomd.xml: [Errno 14] HTTP Error 404: Not Found
Trying other mirror.


Check
ps -eaf | grep memcache

Check if memcache installed
phpinfo();

Check if memcache is running
telnet localhost 11211

Connection Refused


10 baby step to install Memcached Server and access it with PHP
http://www.webdeveloperjuice.com/2010/01/25/10-baby-steps-to-install-memcached-server-and-access-it-with-php/
Step1 Install libevent ,libmemcached and libmemcached devel (dependency)
yum install libevent
Setting up Install Process
Parsing package install arguments
Package libevent - 1.3b-1.fc7.i386 is already installed.
Nothing to do

yum install libmemcached libmemcached-devel
Setting up Install Process
Parsing package install arguments
No package libmemcached available.
No package libmemcached-devel available.
Nothing to do

Step2 Install Memcached Server
yum install memcached
Setting up Install Process
Parsing package install arguments
Resolving Dependencies
--> Running transaction check
---> Package memcached.i386 0:1.2.3-7.fc7 set to be updated
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
memcached i386 1.2.3-7.fc7 updates 50 k

Transaction Summary
=============================================================================
Install 1 Package(s)
Update 0 Package(s)
Remove 0 Package(s)

Total download size: 50 k
Is this ok [y/N]: y
Downloading Packages:
(1/1): memcached-1.2.3-7. 100% |=========================| 50 kB 00:00
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing: memcached ######################### [1/1]

Installed: memcached.i386 0:1.2.3-7.fc7
Complete!

Step3 Start Memcached server
memcached -d -m 512 -l 127.0.0.1 -p 11211 -u nobody
(d = daemon, m = memory, u = user, l = IP to listen to, p = port)

Step4 Check your memcached server is running successfully
ps -eaf | grep memcached
nobody 1888 1 0 07:29 ? 00:00:00 memcached -d -m 512 -l 127.0.0.1 -p 11211 -u nobody
root 1897 1827 0 07:30 pts/1 00:00:00 grep memcached

Step 5: Connect Memcached server via telnet
telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

Step 6: Check current status of Memcached Server on telnet prompt
stats
STAT pid 1888
STAT uptime 153
STAT time 1297945907
STAT version 1.2.3
STAT pointer_size 32
STAT rusage_user 0.054991
STAT rusage_system 0.027995
STAT curr_items 0
STAT total_items 0
STAT bytes 0
STAT curr_connections 1
STAT total_connections 2
STAT connection_structures 2
STAT cmd_get 0
STAT cmd_set 0
STAT get_hits 0
STAT get_misses 0
STAT evictions 0
STAT bytes_read 7
STAT bytes_written 0
STAT limit_maxbytes 536870912
STAT threads 4
END

Step 7: Exit telnet
quit
Connection closed by foreign host.

Step 8: Install PHP client to access Memcached Server
pecl install memcache
It will make “memcache.so”, you have to just put it on your /etc/php.ini file.

WARNING: channel "pecl.php.net" has updated its protocols, use "channel-update pecl.php.net" to update
Skipping package "pecl/memcache", already installed as version 3.0.2
No valid packages found
install failed

Step 9: Restart your apache server
service httpd restart

Step 10: Open your favorite editor to type below code and execute it, it will cache your data into Memcached server and access it back for you

$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect"); //connect to memcached server
$mydata = "i want to cache this line"; //your cacheble data
$memcache->set('key', $mydata, false, 100); //add it to memcached server
$get_result = $memcache->get('key'); //retrieve your data
var_dump($get_result); //show it

2 comments:

  1. Thank you for the info. It sounds pretty user friendly. I guess I’ll pick one up for fun. thank u


    Symfony Development

    ReplyDelete
  2. Your blog is like a search engine optimization magazine. All templates are cool and attractive. God bless
    blog Wordpress themes
    ecommerce wordpress themes

    ReplyDelete