PHP Tutorial memcached installation and use
1. Introduction
The Memcache module is an efficient daemon that provides procedural procedures for memory caching and object-oriented
Convenient interface, especially when designing dynamic web programs, to reduce access to the database tutorials.
Memcache also provides processing for communication dialogs (Session_handler).
More information about Memcache modules can be accessed from http://www.danga.com/memcached/.
1.1.memcache configuration Item list in php.ini memcache list of configuration items in php.ini
Name
Default value
is variable
Change Log
Memcache.allow_failover
"1"
Php_ini_all
Available since Memcache 2.0.2.
Memcache.max_failover_attempts
"20"
Php_ini_all
Available since Memcache 2.1.0.
Memcache.chunk_size
"8192"
Php_ini_all
Available since Memcache 2.0.2.
Memcache.default_port
"11211"
Php_ini_all
Available since Memcache 2.0.2.
Memcache.hash_strategy
"Standard"
Php_ini_all
Available since Memcache 2.2.0.
Memcache.hash_function
"CRC32"
Php_ini_all
Available since Memcache 2.2.0.
Session.save_handler
"Files"
Php_ini_all
Supported since Memcache 2.1.2
Session.save_path
""
Php_ini_all
Supported since Memcache 2.1.2
For further details and definitions of php_ini_* constants see the PHP Manual php.ini configuration options.
Here is a piece of code to give you an intuitive impression: (assuming memcached installed on the 172.10.10.10
Face, port number 12121)
$memcache = new memcached ();
$memcahce->connect (' 172.10.10.10 ', 12121);
$memcache->set (' key ', ' value ');
$memcache->get (' key ');
The above mainly completes the memcached simple operation Flow: connects the memcached server, sets the value, takes
Value (' key ' value is ' value ');
Here we need to explain the Addserver function and the Connect function, Addserver is to add multiple servers
In the connection pool, and connect will only connect to a certain server, if you use the Addserver, then make
With connect, only one server will be used here.
The following code is a simple simulated client operation
$fp = Fsocketopen (' 172.10.10.10 ', 12121, $errorno, $ERRSTR, 1);
if (! $fp)
echo "$errstr";
Else
{
$out = "Get key RN";
Fwrite ($out);
while (!feof ($FP))
$str. = Fgets ($FP);
if (Stripos ($str, ' end ') ===0)
Exit ("No value find")
$arr = Implode (' rn ', $STR);
echo $arr [1];
}
Simply explain the code above, use Fsocketopen to open the socket communication interface of the server, and then send
Sends the GET KEY command, then gets the returned data and resolves the returned data. There's nothing unusual here.
Processing, to be filled in when programming
Memcached is a key-value storage system that stores critical data in memory, dramatically reducing access to data
Of time. The benefit of using memcached is self-evident, it not only reduces the pressure on the system to access the database, but
and improve the reaction speed of the system. As to what kind of system needs to use Memcache, the author suggests that the data volume is large
, access frequent systems can take memcached as the middle tier of caching.
The disadvantage of using memcached in addition to increasing the amount of code in the program, there is no guarantee of real-time database,
Additional time is required to initialize the database for the first time. But these disadvantages are more than the advantages of it
Insignificant