Memcached PHP calls Memcahce through PHP. First, you need to install Memcache on the server. how to install Memcache is not the focus of this article,
About memcache installation, interested friends please refer here: http://blog.csdn.net/xifeijian/article/details/22000173
The following uses a simple Demo to describe how to use php to call Memcahce.
I. install the memcache PHP module
# Wget http://pecl.php.net/get/memcache-2.2.4.tgz
# Tar zxvf memcache-2.2.4.tgz
# Cd memcache-2.2.4
Find the phpize path
# Whereis phpize
Here is/root/app/php-5.3.3/bin/phpize (usually in the bin directory of the php Installation Path)
#/Root/app/php-5.3.3/bin/phpize
#./Configure -- enable-memcache -- with-php-config =/root/app/php-5.3.3/bin/php-config
# Make
# Make install
Add a line to the php. ini file (under the/etc directory)
Extension = memcache. so
Restart httpd
# Service httpd restart
Use phpinfo () in php to view the memcache instructions.
Note: If you only use php-m to view the extensions loaded by php, this does not indicate that the extensions have taken effect.
Note:
After installation, a message similar to this may appear:
Installing shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20050922/
Remember this, modify php. ini, and
Extension_dir = "./"
Change
Extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20050922 /"
Add a row
Extension = memcache. so
II. PHP calls memcached
Write a PHP program and test it.
Connect ('localhost', 9023) or die ("cocould not connect"); // connect to the Memcached server $ memcache-> set ('key', 'Hello, XiFeiJian '); // set a variable to the memory and its name is test $ get_value = $ memcache-> get ('key '); // retrieve the key value from memory echo $ get_value;?>
II. Appendix: common memcached operations
Connect ("localhost", 11211); // Save Data $ mem-> set ('key1', 'This is first value', 0, 60 ); $ val = $ mem-> get ('key1'); echo "Get key1 value :". $ val."
"; // Replace Data $ mem-> replace ('key1', 'This is replace value', 0, 60 ); $ val = $ mem-> get ('key1'); echo "Get key1 value :". $ val."
"; // Save array data $ arr = array ('AAA', 'BBB ', 'CCC', 'ddd '); $ mem-> set ('key2 ', $ arr, 0, 60); $ val2 = $ mem-> get ('key2'); echo "Get key2 value:"; print_r ($ val2); echo"
"; // Delete data $ mem-> delete ('key1'); $ val = $ mem-> get ('key1'); echo" Get key1 value :". $ val."
"; // Clear all data $ mem-> flush (); $ val2 = $ mem-> get ('key2'); echo" Get key2 value :"; print_r ($ val2); echo"
"; // Close the connection $ mem-> close ();?>