Introduction to Memcached
memcached is a distributed caching system developed by Brad Fitzpatrick of LiveJournal but used by many websites.
memcached is a high-performance distributed memory cache server. The general purpose of use is to improve the speed and scalability of dynamic web applications by caching database query results and reducing the number of database accesses.
memcached data stream
Slab is composed of multiple Pages. Pages are cut into multiple chunks according to the specified size.
Mencached installation
yum install -y memcached libmemcached libevent
#memcached服务开启
systemctl start memcached
#状态查看
方式一:memcached-tool 127.0.0.1:11211 stats
方式二:yum -y install nc
echo stats |nc 127.0.0.1 11211
stats slabs #slabs
Memcached command line
yum install -y telnet
telnet 127.0.0.1 11211
Memcached syntax format
- \r\n \r\n
- · Note: \r\n is the Enter key under Windows
- · Can be set, add, replace
- · Set means to store the data accordingly, add it when it is not available, and overwrite it when it is available.
- · add means adding the data accordingly, but if the data already exists, the operation will fail.
- · replace means replacing the data accordingly, but if the data does not exist, the operation fails.
- · The client needs to save the key of the data
- · Is a 16-bit unsigned integer (expressed in decimal notation).
This flag will be stored together with the data that needs to be stored, and will be returned when the client gets the data.
Clients can use this flag for special purposes and are opaque to the server. - · is the expiration time.
If it is 0, it means that the stored data will never expire (but can be replaced by server algorithm: LRU, etc.).
If it is non-zero (unix time or the number of seconds since this time), the server can guarantee that the user will not get the data after expiration (based on server time). - · The number of bytes to be stored can be 0 when the user wants to store empty data
- · After inputting the content that needs to be stored, the client needs to add \r\n (click Enter directly) as the end mark.
#举例
telnet 127.0.0.1 11211
set key1 1 300 5
aaaaa
STORED
get key1
aaaaa
END
Memcached import and export
#memcached导出数据
memcached-tool 127.0.0.1:11211 dump > data.txt
#memcache 导入数据
nc 127.0.0.1 11211 < data.txt
如果导入失败,将memcached重新启动即可导入成功
systemctl restart memcached
nc 127.0.0.1 11211 < data.txt
php5.4 version installation
Current certification: Only version 5.4 of PHP can be used
PHP is installed using yum
yum install gcc gcc-c++ prce-devel expat-devel
yum -y install epel-release yum-utils
yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --enable remi-php80
yum install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mbstring php-curl php-xml php-pear php-bcmath php-json php-redis
PHP uses compilation and installation
yum install -y perl* --skip-broken httpd-devel openssl openssl-devel bzip2 bzip2-devel libpng libpng-devel freetype freetype-devel epel-release libmcrypt-devel libxml2-devel libtool
gcc make automake autoconf libtool bison flex
Specific installation configuration
wget https://www.php.net/distributions/php-5.4.45.tar.gz
cd /usr/local/src
tar -zxvf php-5.4.45.tar.gz
#安装必要的安装包
yum install -y openssl openssl-devel
yum install -y bzip2 bzip2-devel
yum install -y libpng libpng-devel
yum install -y freetype freetype-devel
yum install -y epel-release
yum install -y libmcrypt-devel
yum install -y libxml2-devel
cd php-5.6.30
#进行编译安装
./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-libxml-dir--with-gd --with-jpeg-dir --with-png-dir--with-freetype-dir --with-iconv-dir--with-zlib-dir --with-bz2 --with-openssl--with-mcrypt --enable-soap--enable-gd-native-ttf --enable-mbstring--enable-sockets --enable-exif
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
#出现此界面就可以直接编译安装了
make && make install
PHP connection Mencached
Install the memcache extension for php
cd /usr/local/src/
wget https://pecl.php.net/get/memcache-2.2.3.tgz
tar zxf memcache-2.2.3.tgz
cd memcache-2.2.3
#运行phpize文件
/usr/bin/phpize
./configure --with-php-config=/usr/bin/php-config
make && make install
File editing, data writing
#编辑文件
vim /etc/php.ini
#插入数据
extension="memcache.so"
#对插入的数据进行查询,红色返回结果
/usr/bin/php -m | grep memcache
memcache
#新建一个php文件
cat 1.php
test success
#使用命令进行运行
/usr/bin/php 1.php start
test success
Store sessions in Memcached
The session.save_handler configuration parameter in php.ini allows session_set_save_handler() to work properly.
The name of the handler to store and retrieve data associated with the session, defaults to files
#向php.ini中插入数据
vim php.ini
session.save_handler = memcache
session.save_path = "tcp://127.0.0.1:11211"
#或者http.conf
php_value session.save_handler "memcache"
php_value session.save_path "tcp://127.0.0.1:11211"
#或者php-fpm.conf对应的pool中添加
php_value[session.save_handler] = memcache
php_value[session.save_path] = " tcp://127.0.0.1:11211"
#php.ini配置文件,为了防止下面telnet 验证出错进行注释
;session.save_handler = files
session.save_handler = "memcache"
session.save_path = "tcp://127.0.0.1:11211"
session.php
<?php
session_start();
$_SESSION['name']='test';
echo session_id()."<br/>";
echo $_SESSION['name'];
?>
Test configuration
#获取session值
/usr/bin/php session.php start
4bit5200ot06m8d83se4fmu2a4<br/>test
#telnet测试
[root@slave htdocs]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get shqov9bq80vu3m3er5dg6v00a6
VALUE shqov9bq80vu3m3er5dg6v00a6 0 16
name|s:4:"test";
END