Memcached study Note 6: Mechanism of memcached and detailed discussion of memcached

Source: Internet
Author: User

Appendix: Mechanism of memcached

A: ① Based on the C/S architecture, the Protocol is relatively simple

C/S architecture. At this time, memcached is the server end. We can use programs such as PHP and C ++/C to connect to the memcached server.

Memcached Server client communication does not use XML or other formats, but uses a simple text line-based protocol. Therefore, memcached can also store and read data through Telnet.

② Libevent event-based processing

Libevent is an encapsulation of the event processing interface of the entire platform. Its processing compatibility includes the following operating systems:

Event handling for Windows, Linux, BSD, Solaris, and other operating systems

Memcached uses libevent for concurrent network connection processing. It can maintain fast response while maintaining high concurrency!

③ Built-in memory storage

To improve performance, data stored in memcached is stored in the memory storage space built in memcached. Because the data is only stored in the memory, restart memcached, restarting the operating system will cause all data to disappear. When the memory capacity reaches the specified value, the unused cache will be automatically deleted based on the LRU (least recently used) algorithm. Memcached is a server designed for caching, so it does not take the permanent data into consideration.

④ Client-based distributed

Although memcached is a "distributed" cache server, the server does not have distributed functions. Each memcached does not communicate with each other to share information. How can it be distributed? This depends entirely on the implementation of the client.

Memcached1 memcached2 memcached3

                  

Distributed algorithms

Client Library

              

PHP applications

 

The PHP application needs to manage three memcached servers, the client performs operations, and then the data is stored on one memcached server that has passed the memcached distributed algorithm.

 

Memcached details:

1. Lifecycle

① After Apache is disabled

② Apache is not closed, but the cache time is up

③ Memcached service closed

2. Where does memcached data exist?

Stored in the memory of the machine where memcached is located

3. memcached Best Practices

① Store the data queried from the database in memcached

<? PHP

$ User_id = (INT) $ _ Get ['user _ id'];

// Determine whether user_id exists in memcached

$ MC = new memcache ();

If ($ user = $ Mc-> get ('user _ id ')){

// Locate and retrieve the value directly

Dump ($ user );

} Else {

// The query database is not found.

Ob_start ();

$ User = // query database operations to obtain data

// Cache, compress, and save for 24 hours

$ Mc-> set ($ user_id, $ user, memcached_compressed, 24x3600 );

// Allocate it to the template

$ This-> assign ('user', $ user );

Ob_end_flush ();

}

 

② Store session data in memcached

By default, session data is stored in files on the server.

Complete the following configurations:

① Ensure that memcache extension is enabled and memcached runs

② Set session. save_handler = memcache in PHP. ini

③ Set session. save_path = TCP: // localhost: 11211 in PHP. ini.

④ You can also use the PHP function ini_set () to perform settings on the page.

Summary:

We only need to perform the above configuration in PHP. ini, so the session will not be stored on the server according to the file format, but in the memcached server memory. When calling, you can still use the original session file storage format: $ user_id = $ _ session ['user _ id'];

③ Memcached's data is in the non-user State and will not be bound to a user.

④ Memcached access security

Windows

Set firewall: Open the firewall and set exceptions

 

Linux

        Iptables-A input-P protocol-S can access IP-dport-J accept

  4,

What data is suitable for storing in memcached?

Data with frequent changes and instability does not need to be written into the database in real time (such as online user status and online user count)

Portal website news and so on, feel that the static page still cannot meet the requirements

What kind of data is not suitable for storing in memcached?

Changes in the stock and securities system of Banks

The data volume is too large, such as Youku's video

    Summary:

Memcached is not considered for a small website with a small PV value.

Frequent changes and frequent queries, but not necessarily written to the database (suitable for memcached) (online user status and number of online users)

Frequent changes require warehouse receiving, such as stocks and finance (not suitable for memcached)

Frequent changes and frequent queries. It is suitable for memcached (SINA News Channel)

      

      

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.