PHP Learning Notes (24) PHP uses and installs the Memcache extension library

Source: Internet
Author: User
Tags flush memcached php memcached iptables
<?php/** * Memcached (Memory Cache) * High performance distributed Memory object caching system (maintenance of a huge hash table (Key=>value) via memory) * Memory maintenance software (reduce IO 150k open source) * http://l 			ocalhost:11211 * * memcached based on Libevent event library * Linux: * 1. Install libevent * 2. Install memcached and specify libevent location * 3. Use: * Start: memcached-d-M 128-l 192.168.0.1-p 11211-u root * stop: Kill Cat/tmp/memcached.pid * Killall memcached *
 Windows: * Use compiled software * 1. Install to System services: * In Doc: Execute this software memcached.exe-d install (if prompted for error, to find Cmd.exe open as Administrator) * 2. Uninstall: * In Doc: Execute this software memcached.exe-d Uninstall * 3. Start service: (Default IP is native, port is 11211) * memcached.exe-d start * memcached . exe-d-M 128-l 127.0.0.1-p 11211 Start Specifies parameters * Configuration parameters: *-P Listening Port *-L link IP address, default is native * D-start memcached service *- d Restart Restart memcached service *-D stop|shutdown shutdown running memcached Service *-d Install install memcached service *-d uninstall uninstall memcached suit  *-U in what capacity (only valid when run as root) *-m maximum use memory, in MB, by default is 64MB, the maximum should be 2G *-M memory exhaustion when return error, instead of delete item *-C maximum simultaneous connection number, default is 1024 * f Block size growth factor, default is 1.25 *-nMinimum allocation space, key+value+float default is the * H Display Help * * Operation memcached (Command mode telnet as client) * telnet localhost 11211---memcached * * PHP configuration memcached file: Add in Ext: Php_memcache.dll (for correct version) * and modify php.ini extension * Extension=php_memcache.dll * [memcache] * MEMC  
 Ache.allow_failover = 1 * memcache.max_failover_attempts=20 * memcache.chunk_size =8192 * Memcache.default_port = 11211 * Memcache Common object-oriented interfaces include: * Memcache::connect open a link to Memcache * Memcache::p Connect open a long connection to Memcache * Memcache:: Close closes a Memcache link * memcache::set save data to Memcache Server * memcache::add Add data to Memcache server * Memcache::get extract a saved in M		Emcache Server data * Memcache::replace replaces an item that already exists on the Memcache server (features similar to memcache::set) * Memcache::d elete Delete a saved item from the Memcache server * Memcache::flush refresh so Memcache items saved on the server (similar to removing all) * Memcache::getstats get current Memcache server running status * Me 
 Mcache::addserver distributed server Add a server * * * when and where to use memcache * One, database read data (SELECT) Use memcache processing * Two, use in session control sessions * * Memcache Safety advice: * 1. Intranet Access: * memcached -d-u root-l 192.168.0.111-p 11211 * 2. Setting up a firewall: * iptables-a input-p tcp-s 192.168.1.111-dport the 11211-j
T * iptables-a input-p udp-s 192.168.1.111-dport 11211-j ACCEPT *//Call Memcache test method//memcachetest ();

Call the method that stores the query statement Memcachesql ();
 /** * Query Database * Cached query data into Memcache * NOTE: * 1. The installation of the same project two times, key to have a different prefix * 2.sql statements can be cached as subscript (prevent SQL leak?)
	*/function Memcachesql () {//LINK database Store Select query Statement//Create Memcache service object $mem = new Memcache;
	$mem->connect ("localhost", 11211);
	$sql = "SELECT * from users where ID <12";
	$key = MD5 ($sql);
	$data = $mem->get ($key);
		if (! $data) {$mysql = new mysqli ("localhost", "root", "root", "hibernate");
		$result = $mysql->query ($sql);
		$data = Array ();
		while ($row = $result->fetch_assoc ()) {$data []= $row;
		} $result->free ();
		
		$mysql->close ();
		$mem->set ($key, $data, memcache_compressed,60);
	Echo $sql;
	echo "<pre>";
	Print_r ($data);
	
	echo "</pre>";
$mem->close ();
 }
	

/*** Memcache Server * Data test/function Memcachetest () {//Create Memcache service object $mem = new Memcache;
	$mem->connect ("localhost", 11211);
	Add multiple servers//$mem->addserver ("www.sid.com", 11221);
	
	$mem->addserver ("192.167.1.112", 11211);
	Add $mem->add ("MyStr", "This is a memcache test!", memcache_compressed,60);
	Modify $mem->set ("MyStr", "This is a update memcache test!", memcache_compressed,60);
	Get $str = $mem->get ("mystr"); echo $str. "
	
	<br> ";
	Add array $mem->add ("Myarr", Array ("ASDFSA", "Sdfsdf"), memcache_compressed,60);
	Get $str = $mem->get ("Myarr"); echo $str. "
	
	<br> ";
		Add object class per{var $name = "san";
	var $age = 12;
	} $mem->add ("Myper", new per,memcache_compressed,60);
	Get $str = $mem->get ("Myper"); echo $str->name. "
	
	<br> "; Get information about the server echo $mem->getversion ().
	<br> ";
	echo "<pre>";
	Echo Var_dump ($mem->getstats ());
	
	echo "</pre>";
	$mem->flush ();
$mem->close (); }?>

PHP memcached Extension Library Download (php5.2 and php5.3)

Click to open the link

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.