The basics of Memcached practice

1. Introduction to Memcached

Free and open source, high performance, distributed memory object caching system

Function: The data cache stores the session SMS verification code

Installing memcached is based on libevent, so you need to install libevent before installing memcached

Memcached common commands

The set command stores the value in the specified key. If the specified key already exists, update the value syntax for the key: Key The key in the key-value structure used to find integer arguments that cache flags can contain key-value pairs, The client uses it to store additional information about the key-value pair for the length of time that the expire holds the key-value pair in the cache. The length of the bytes stored in the cache is 0. The output information stored by the value is stored

The get command gets the value stored in the key syntax get key1 key2 key3

If the key does not exist, the empty syntax gets key1 key2 key3. In the output from using the gets command, the number in the last column represents the CAS token for the current key

Cas command check and set, this command only if the value corresponding to this key has not been changed by other clients after the last value of the current client, Cas key flags expire bytes unique_cas_token key key key key key key key key key key key key key key key key key value structure, used to find the cache value flags can contain integer parameters of the key value pair, The length of time that expire holds a key-value pair in the cache is in seconds. 0 represents permanent Bytes Number of bytes stored in the cache Unique_cas_token Unique_cas_token The value stored in the unique 64-bit value retrieved through the gets command

The delete key command deletes an existing key: Delete key is the key in the key-value structure that is used to search for the cached value. Output value indicates that a DELETED KEY was deleted due to a successful error syntax error or that a NOT_FOUND KEY did not exist

The prepend/append command appends prepend key flags expire bytes before it

– append key flage expire bytes – append key flage expire bytes If you want to look up the cache value, flags can contain the key-value pair. Expires holds the key-value pair in the cache for the length of time in seconds that is permanently valid. The bytes in the cache are the length of the bytes stored in the cache The storage failed client error

Incr/decr increments or decrements the numeric value of an existing key. The data that incr and decr operate on must be decimal 32-bit unsigned integers. If the key does not exist, return not found. Error syntax incr key increment_value decr key decrement_value parameter indicates the key in the key-value structure, Used to find the cache value increment_value: increment decrement_value: decrement

The add command

Store value in specified key If the key of add already exists, the data will not be updated, the previous value will remain the same, The key in the key-value structure is used to find the cached value flags that can contain the integer parameter expire of the key-value pair The length of time to hold a key-value pair in the cache is in seconds 0, which represents the length of bytes that the permanent Bytes will store in the cache

3. Memcached extension installs and stores sessions

Installed libmemcached=> Installed memcached=> Installed php-memcached extension Cookie and session

1, HTTP Request = "2, HTTP Reponse + Set Cookie (SessionID) =" 3, HTTP Request + Cookie => 4, HTTP Response

Disadvantages of storing file sessions:

1. Slow IO reading and writing; 2. Restricted distribution

Store session mode

  • Modify the PHP ini
  • Set files in the code
4. Use of the Memcached class

File cache Redis cache Memcahce cache

  1. Link server
$cache->resetServerList(); $cache - > addServers (array (127.0.0.1, 11211, 60), array (127.0.0.1, 11212, 40),)

The third parameter corresponds to the weight in the server to control the probability of the server being selected

  1. Set to get change data
$cache->setMulti( array( key1=>value1, key2=>value2, key3=>value3 ),time()+900 ); $cache->get(key1); $cache->getMulti(key1,key2,...) ; $cache->get('k1',null,$cache->get('k1',null,$cas);
  1. Commonly used method
$cache->add(key,value,time()+900); $cache->increment(key,incr); $cache->decrement(key,decr);

The incr and decr commands operate on data that must be 32-bit unsigned decimal integers. The result is no negative value

Memcached usage scenarios

  1. The Session storage
  2. Relieve the pressure of database and improve the speed of interaction
  3. Database master-slave synchronization relay

Matters needing attention

Value < 1M key < 512 value < 1M key < 512

Introduction to Memcached cache

advantage

  • An in-memory database is a database that operates directly with data in memory
  • Solve the problem of data efficiency, reduce IO consumption
  • It is divided into relational memory database and non-relational memory database

Characteristics of the

  • The protocol is simple, using a simple line-of-text protocol
  • The number of server connections can be adjusted flexibly based on libevent event processing
  • Memory storage, save and read fast
  • A distributed system that does not communicate with each other. Each server only manages its own data
  • Lack of certification and security controls

Distributed algorithms are implemented using memcached

  • Servers do not communicate with each other
  • Because the program is distributed
  • Consider the algorithm to disperse the storage pressure
  • Consider the hit ratio of the algorithm

The remainder of the distributed algorithm calculate dispersion method CRC is calculated according to the key, and then the modulus was carried out on the number of servers to get the memcached server nodes, servers can’t link, will try to add the number of links to the key behind to recalculate the faults: add or remove the server, almost all the cache will be rebuilt, avalanche breakdown problems are considered

Consistent hashing algorithm for distributed algorithms

  • Find the server node’s hash assigned to a circle of 0-2^32
  • Find the hash value of the stored data key to map to the circle
  • Start the search clockwise from the location to which the data is mapped, saving the data to the first server found

Advantages: less redundancy, less load balancing, over smooth storage balancing

Use memcached for session sharing

Sessions are stored in the common memcached server and shared by multiple servers. Disadvantages: Clustering errors can cause users to be unable to log in, and the recycling mechanism can cause users to drop out