Home  >  Article  >  Backend Development  >  Introduction and working principle of memcached in PHP_PHP tutorial

Introduction and working principle of memcached in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:55:27860browse

Introduction to memcached

1. Concept

1. memcached

From wiki: memcache is the name of this project, and memcached is the file name of its server-side main program.

Memcache is a project of danga.com, which first served liveJournal. Currently, many people use this caching project to build their own large-load websites to share the pressure on the database. Its working mechanism is to open up a space in the memory and then create a hash table. The main program of memcached manages this hash table by itself

2. Working principle

memcached runs in one or more servers as a daemon, accepting connection operations from multiple clients at any time. Clients can be written in various languages. Currently known client APIs include Perl/php/python/ruby /java/c#/c etc. After the client establishes a connection with the memcached service, the next step is to access objects. Each accessed object has a unique key. Objects saved in memcached are placed in memory rather than in cache files. .

It adopts C/S mode, starts the service process on the server side, specifies the listening IP, its own port number, and the memory size used. The main program of the current version is implemented in C language

3. How to use it in PHP

1. Install PHP’s memcache extension. After installation, you can view the extension configuration information through phpinfo(), and you can change the configuration information in php.ini.

2. Test code:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] $memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Could not connect");
$version = $memcache- >getVersion();
echo "Server's version: ".$version."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)n";
$get_result = $memcache->get('key');
echo "Data from the cache:n";
var_dump( $get_result);
?>

References to all the above functions can be found in the PHP Manual

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364436.htmlTechArticle memcached introduction 1. Concept 1. memcached From wiki: memcache is the name of this project, and memcached is its server-side host The filename of the program. memcache is a project of danga.com, the most...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn