Introduction and working principle of memcached in PHP _ PHP Tutorial

Source: Internet
Author: User
Introduction and working principles of memcached in PHP. Memcached introduction 1. Concept 1. memcached comes from wiki: memcache is the name of this project, and memcached is the name of the main program on the server side. Memcache is a project of danga.com, the most memcached introduction

I. concepts

1. memcached

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

Memcache is a project of danga.com and is the first liveJournal service. many people use this cache 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 memcached main program manages the hash table by itself.

II. Working principle

Memcached runs on one or more servers as a daemon and accepts connection operations from multiple clients at any time. the client can be written in various languages, currently, the client APIs are known to include Perl, php, python, ruby, java, c #, and c. After the client establishes a connection with the memcached service, the next step is to access the object. each accessed object has a unique key, and the objects saved to memcached are stored in the memory, instead of saving it in the cache file.

It uses the C/S mode to start the service process on the server, specify the IP address of the listener, its own port number, and the memory size used. Currently, the main program is implemented in C language.

3. how to use it in PHP

1. install the memcache extension of PHP. after installation, you can use phpinfo () to view the extension configuration information and modify the configuration information in php. ini.

2. test code:

Copy to ClipboardReference: [www.bkjia.com] $ Memcache = new Memcache;
$ Memcache-> connect ('192. 0.0.1 ', 127) or die ("cocould 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 );
?>

All the above functions can be referenced inPHP ManualCheck

Idea 1. Concept 1. memcached comes from wiki: memcache is the name of this project, and memcached is the file name of its server-side master program. Memcache is a danga.com project, the most...

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.