Memcached and memcached installation _ PHP Tutorial

Source: Internet
Author: User
Install Memcached and memcached. Memcached and memcached are a high-performance distributed memory object cache system for dynamic Web applications to reduce database load. It is installed by caching data and object Memcached and memcached in the memory.

Memcached is a high-performance distributed memory object cache system for dynamic Web applications to reduce database load. It caches data and objects in the memory to reduce the number of reads to the database, thus improving the speed of dynamic and database-driven websites.

Memcached is based on a hashmap that stores key/value pairs. Its daemon is written in C, but the client can write it in any language and communicate with the daemon through memcached protocol.

Storage method:

To improve performance, data stored in memcached is stored in the memory storage space built in memcached. Because the data only exists in the memory, restarting memcached and the operating system will cause all data to disappear. In addition, when the content capacity reaches the specified value, the unused cache is automatically deleted based on the LRU (Least Recently Used) algorithm. Memcached itself is a server designed for caching, so it does not take the permanent data into consideration.

Tips:

In many languages, clients connected to memcached are implemented, including Perl and PHP.

1. here we will introduce how to install windows.

1. download the stable version of memcache for windows and decompress it to a disk, for example, in c: \ memcached

2. enter 'C: \ memcached \ memcached.exe-d install' in cmd to install

3. enter 'C: \ memcached \ memcached.exe-d start' to start.

In the future, memcached will be used as a windows service to automatically start every time it is started. In this way, the server has been installed.

4. simple. However, you have just installed a memcached caching server and have not yet established an association with php. So it cannot be used in php programs.

Since my php version is 5.2.17, the downloaded php_memcache.dll also corresponds. If your php version is 5.3 +, you can download it here.

Php_memcache-cvs-20090703-5.3-nts-VC6-x86.zip

The installation is exactly the same as adding extensions at ordinary times. copy the dll file to the ext directory of your php Directory. then,

Add extension = php_memcache.dll to php. ini and restart the server. you can see the configuration information in phpinfo.

II. installation under CentOS

Install yum-y install memcached

Set to start chkconfig -- level 2345 memcached on

Start and stop/etc/init. d/memcached start | stop

Supplement: if the installation lacks other support, you can:

Yum groupinstall "Development Tools"

Common operations

Memcache: add a value. if it already exists, false is returned.

Memcache: addServer add an available server address

Memcache: close closes a Memcache object.

Memcache: connect creates a Memcache object.

Memcache: debug control debugging function

Memcache: decrement: deletes the value of a saved key.

Memcache: delete deletes a key value.

Memcache: flush clear all cached data

Memcache: get a key value

Memcache: getExtendedStats: obtains the running system statistics of all processes in the process pool.

Memcache: getServerStatus get the parameters of the running server

Memcache: getStats returns some running statistics of the server.

Memcache: getVersion: returns the version information of the running Memcache.

Memcache: increment adds the value of a saved key.

Memcache: pconnect creates a persistent connection object for Memcache.

Memcache: replace R overwrites an existing key.

Memcache: set adds a value. if it already exists, it overwrites it.

Memcache: setCompressThreshold compresses data larger than a certain size.

Memcache: setServerParams modify server parameters at runtime

 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 ();?>

How memcached works:

First, memcached runs on one or more servers as a daemon and accepts client connection operations at any time. the client can be written in various languages, currently, known client APIs include Perl, PHP, Python, Ruby, Java, C #, and C.

After the PHP client establishes a connection with the memcached service, the next thing is to access the object. each accessed object has a unique identifier key, and the access operation is performed through this key, objects saved to memcached are actually stored in the memory, not in the cache file. This is why memcached is so efficient and fast. Note that these objects are not persistent. after the service is stopped, the data in the objects will be lost.

Memcachedb:

MemcacheDB is a distributed, key-value persistent storage system. It is not a cache component, but a reliable and fast persistent storage engine based on object access. The protocol is consistent with memcache (incomplete), so many memcached clients can connect to it. MemcacheDB uses Berkeley DB as a persistent storage component. Therefore, many Berkeley DB features are supported.

We are standing on the shoulders of giants. The front-end cache of MemcacheDB is Memcached.

Front-end: memcached network layer

Backend: BerkeleyDB storage

What is the relationship and difference between memcached and smarty?

Memcache is a high-performance distributed memory object cache system that records the cache to the memory ..

For example, if you want to retrieve the list display from the database but do not want to read the database every time, you need to use the cache. memcache is one of them, which stores records in the memory.

For example, to retrieve data from the database again, choose db> memcache> client.

First, check whether memcache has data. if not, read the database and save the records obtained by the database in memcache.

The next time you need to read the record, you can directly read the record in memcache, so that you can share the burden on the database, and the speed is much faster.

Smarty is a template engine written in PHP. it aims to separate PHP programmers from the front-end personnel so that programmers can change the logic content of the program without affecting the front-end personnel's page design.

Smarty is run in the view of the MVC structure.

For example, to display a variable in php, we need to write echo $;

However, if you use smarty, you need to write {$ a} like this. after compilation, The echo $ a; will be automatically displayed. it is the same. the cache in smarty. after php is running, can be output to the browser. it is necessary to calculate the html output generated by running php, and smarty will save the html generated by running php, if you call this php again, the previous html is directly output. cache.

Memcache has nothing to do with smarty. the two functions are different and there is no connection between them.

Memcached is a high-performance distributed memory object cache system for dynamic Web applications to reduce database load. It caches data and objects in the memory...

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.