Meet Memcached
Memcached Introduction
Memcached is an open-source, high-performance distributed memory object caching system that stores all of the data in memory because a huge Hash table is maintained in memory, so data from any storage type is supported. Many websites use Memcached to increase the speed of access to a website, especially for large sites that require frequent access to data.
Memcached is a typical C/s architecture, so you need to install Memcached server and Memcached API client. The Memcached server is written in C voice, and the Memcached API client can be written in any language, such as PHP, Python, Perl, etc., and communicate with the Memcached server through the Memcached protocol.
How to store and how data expires
Memcached has a unique way of storing and data expiration
Data storage mode: Slab Allocation
Slab Allocation is allocating memory by group, allocating one Slab at a time, equivalent to a page of size 1MB, and then dividing the same size Chunk in 1MB space according to the data. This method can effectively resolve memory fragmentation problems, but may be wasteful of memory space.
- Data expiration mode: LRU, Laxzy expiration
LRU is when the additional data space is insufficient, the most recently used records are eliminated according to the LRU situation. Laxzy expiration, which is lazy expiration, is to check the record time using get, thus checking whether the record has expired.
Memcached caching mechanism
The cache is a memory-resident data that can be read quickly. And Memcached is such a very good cache software, when the program writes the cache data request, the Memcached API interface routes the Key input routing algorithm module to one server in the cluster, then communicates with the server by the API interface and completes a distributed cache write.
Memcached distributed
Memcached distributed deployment relies primarily on Memcached clients to implement, and multiple Memcached servers are independent. How distributed data is stored is determined by the routing algorithm.
When the data reaches the client library, the client's algorithm determines the saved Memcached server based on the routing algorithm. When the data is read, the client reads the data based on the same server that the data was saved in when the route algorithm was selected and stored.
Memcached Routing algorithm
- Finding the remainder hash algorithm
To find the remainder of the hash algorithm is to use key to do hash operation to get an integer, and then to do the hash algorithm, according to the remainder of the route. This algorithm is suitable for most of the requirements, but is not suitable for dynamic environments, such as a large number of machines added or deleted, will cause a large number of objects storage location invalidated.
- Consistent hash algorithm
The consistent hash algorithm is suitable for use in dynamically changing environments. The principle is to follow the hash algorithm to the corresponding key through a certain hash algorithm processing, mapping to form a closed loop, and then by using the same hash algorithm as the object storage to map the machine to the ring, in a clockwise direction all the objects to their nearest machine.
Installing the Memcached case
- This case is completed using two CentOS 7.4 systems, one is Memcached server, one is based on the LAMP architecture for Memcached expansion of Memcached API client, can be based on enterprise requirements for architecture adjustment.
- Case Environment
Host |
IP Address |
Major Packages |
Memcached Server |
192.168.100.201 |
Memcached-1.5.6.tar.gz, libevent-2.1.8-stable.tar.gz |
Memcached API Client |
192.168.100.202 |
Memcached-2.2.7.tgz, httpd, MySQL, PHP |
Start deployment
- Memcached API Client
- Manually compile and install the lamp architecture: see the previous blog post, not much in this narrative
- Test that the database is working properly
# mysql -u root -p > CREATE DATABASE sky;> GRANT all ON sky.* TO ‘skyuser‘@‘%‘ IDENTIFIED BY ‘admin123‘;> flush privileges; # vi /usr/local/httpd/htdocs/index.php <?php$link=mysql_connect(‘192.168.100.202‘,‘skyuser‘,‘admin123‘);if($link) echo "
- Add test home after accessing "http://192.168.100.202/index.php"
Show Success for normal operation
Installing the memcached API client
# yum install autoconf -y# tar xf memcached-2.2.7.tgz -C /opt/# cd /opt/memcached-2.2.7 //使用PHP的phpize脚本生成配置脚本configure 再进行配置编译# /usr/local/php5/bin/phpize # ./configure --enable-memcache --with-php-config=/usr/local/php5/bin/php-config# make && make install............/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/ //记录此行(共享组件的位置) 下面用到
configuring PHP, adding memcached components
- Write a test page to test whether memcached is working properly
# vi /usr/local/httpd/htdocs/index.php<?php$memcache = new Memcache();$memcache->connect(‘192.168.100.202‘,11211);$memcache->set(‘key‘,‘Memcache test Successfull!‘,0,60);$result = $memcache->get(‘key‘);unset($memcache);echo $result;?># service httpd restart
Memcached database operation and Management
- Memcached database operation and management is very simple, installation using Telnet connection 11211 port can be
# yum install telnet -y# telnet 127.0.0.1 11211
- New data
add username 0 0 7 //不进行压缩和序列化标识 数据过期时间为永不过期 标识号是7就需要输入7位数。example //输入数据
- Get Data
get username //获取数据VALUE username 0 7example gets usernameVALUE username 0 7 1 //最后一位是更新因子会自增1example
- Update data
set username 0 0 10 //更新信息,若键名不存在,则自行添加everything replace username 0 0 8 //更新信息,若键名不存在,则报错12345678
- Check for Updates
gets usernameVALUE username 0 8 412345678cas username 0 0 7 4 //检查更新,更新因子相等则更新否则返回EXISTSlodgingSTORED
- Append Data
append username 0 0 7 //键值后追加数据exampleSTORED prepend username 0 0 2 //键值前追加数据unSTORED
- Clear Data
delete username //清除指定的键值数据flush_all //清除所有缓存数据OK
- View server Statistics
stats //显示状态信息stats items //返回所有键值对的统计信息stats cachedump 1 0 //返回指定存储空间的键值对 stats slabs //显示各个slab的信息,包括chunk的大小、数目、使用情况等stats sizes //输出所有item的大小和个数stats reset //清空统计数据
High-Performance memory object cache--memcached