I hope to build memcache service to help you


Memcache

The role of Memcache is very good on the Internet. The simple point is to reduce the pressure of reading the database. The principle is also very simple:

The requested data will be fetched from memcache first, if not, fetch it from the database, and bring a copy to memcache by the way.

Every time data is updated, the data in memcache is also updated first, if not, the database is updated and memcache is updated at the same time.

Therefore, it should be noted that this data is easily stored.

One, Memcache overview

Memcache is a free, open source, high-performance, distributed high-speed cache system. Because Memcache reduces the number of times to read the database by caching data and objects in memory. At present, it is used by many websites to improve the access speed of the website, especially for some large-scale websites that require frequent access to the database.

Memcache is a HashMap that stores key-value pairs. Any data in the memory can be stored in a key-value manner. The database can come from database calls or API calls. The design concept of Memcache is small and powerful. Her simple design promotes rapid deployment, easy development and solves many problems of large-scale data caching. Its open API enables Memcache to be used in Java, C/C++/C#, Perl , Python and most popular programming languages.

Two, Memcache workflow

Note that although Memcache is called "distributed cache", Memcache itself does not have distributed functions at all. Memcache clusters will not communicate with each other. The so-called "distributed" depends entirely on client programs, as shown in the figure. :

mark

Memcahe workflow: (1) The application enters the data that needs to be written into the cache;
(2) The API enters the Key into the routing algorithm module, and the reason algorithm obtains the server number according to the Key and the Memcache cluster server list;
(3) The Memcache is obtained from the server number And its IP address and port number;
(4) API calls the communication module to communicate with the server with the specified number, write data to the server, and complete a distributed cache write operation;

Regardless of whether it is reading the cache or writing to the cache, as long as the same routing algorithm and server list are used, and the application queries the same key, the Memcache client always accesses the same client to read the data, as long as there is still in the server cache The cache of the data can guarantee a cache hit.

This Memcache cluster method is also considered from the aspect of partition fault tolerance. Assuming that Node2 is down, then the data stored on Node2 is not available. At this time, because Node0 and Node2 still exist in the cluster, the next request is to obtain Node2's Cached data, there must be no hits. At this time, the cached data is first obtained from the database, and the cached data is stored on Node0 or Node1 according to the routing algorithm. This kind of clustering is very good, but the disadvantage is that the cost is too high.

2.1: Mencache basic settings

-p 监听的端口
-l 连接的IP地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位MB。默认64MB
-M 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助

Three: Build Memcache

The installation of Memcache is divided into two processes: the installation of the memcache server and the installation of the memcached client. The so-called server-side installation is to install Memcache on the server (usually a linux system) to store data.

The so-called client-side installation refers to php (or other programs, Memcache and other good api interfaces) to use the functions provided by the server-side Memcache, and PHP needs to add extensions.

It can be seen that the establishment of Memcache requires the help of LAMP or LNMP, and this blog post uses the LAMP structure.

lab environment

A centos 7 as the server 20.0.0.41

A centos 7 as the client 20.0.0.42

Purpose

  • Set up memcache server and client, and check whether the server connection is normal by visiting the client
  • The working principle is:
  • Memcached has two core components: server and client.
  • In a memcached query, the client first determines the server location of the kv pair by calculating the hash value of the key.
  • When the server is determined, the client will send a query request to the corresponding server to find the exact data.
  • Because there is no interaction and multicast protocol between these, the impact of memcached interaction on the network is minimized:

3.1: Build a memcache server

[root@mencache ~]# ls
anaconda-ks.cfg               公共  文档
initial-setup-ks.cfg          模板  下载
libevent-2.1.8-stable.tar.gz  视频  音乐
memcached-1.5.6.tar.gz        图片  桌面
//解压事件包
[root@mencache ~]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt

//解压源码包
[root@mencache ~]# tar zxvf memcached-1.5.6.tar.gz -C /opt

//查看opt目录
[root@mencache ~]# cd /opt
[root@mencache opt]# ls
libevent-2.1.8-stable  memca:ched-1.5.6  rh

3.2: Compile and install memcache and event library

//安装编译器
[root@mencache opt]# yum install gcc gcc-c++ make -y

[root@server libevent-2.1.8-stable]# cd libevent-2.1.8-stable/
[root@server libevent-2.1.8-stable]# ./configure --prefix=/usr/local/libevent         //指定libevent目录
//编译及安装
[root@server libevent-2.1.8-stable]# make && make install

//memcache依赖于libevent

//进入memcache目录
[root@server libevent-2.1.8-stable]# cd ../memcached-1.5.6/
[root@server memcached-1.5.6]# ./configure \
> --prefix=/usr/local/memcached \	'//指定memcache路径
> --with-libevent=/usr/local/libevent	'//关联libevent事件库'
[root@server memcached-1.5.6]# make && make install

//建立memcache命令软连接
[root@mencache memcached-1.5.6]# ln -s /usr/local/memcached/bin/* /usr/local/bin/

//-u:守护进程  -m:缓存大小32M   -p端口为11211  -u指定用户
[root@mencache memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root

//检查是否开启成功
[root@mencache memcached-1.5.6]# netstat -ntap |grep memcache
tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      33866/memcached     
tcp6       0      0 :::11211                :::*                    LISTEN      33866/memcached     

//关闭防火墙
[root@mencache memcached-1.5.6]# systemctl stop firewalld.service 
[root@mencache :memcached-1.5.6]# setenforce 0

3.3: memcache test [data operation]

//安装telnet软件   否则没有连接
[root@mencache memcached-1.5.6]# yum install telnet -y

//进行链接 
[root@mencache memcached-1.5.6]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character i:s '^]'.

3.4: Basic operations of memcache database

//“set”表示按照相应的<key>存储该数据,没有的时候增加,有的覆盖。
//“add”表示按照相应的<key>添加该数据,但是如果该<key>已经存在则会操作失败。
//“replace”表示按照相应的<key>替换数据,但是如果该<key>不存在则操作失败
//cas,检查更新,更新因子需要相等,
//append,键值后追加数据
//prepend,键值前追加数据
//delete,删除
//flush_all,清楚所有缓存数据



Escape character is '^]'.
add shuai 0 0 7      //0:不使用序列号  0:用过期   5:指定字节长度 
1234567                 //上面设置字节长度为6  不为6 就会报错
STORED

get shuai           //查看
VALUE username 0 7     
123456

gets shuai          //查看
VALUE shuai 0 7 2   //更新因子  每次更新+1
1234567
END

set shuai 0 0 8     //更新数据,若键名不存在,则自行添加
12345678
STORED

gets shuai          //查看
VALUE shuai 0 8 3   //更新因子已经更新+1
12345678
END

replace niu 0 0 2    //更新的键名与键值要存在 set:以存在的数值进行替换 没有数值直接添加【相当于add添加】
12
NOT_STORED          //可以看到添加不上

replace shuai 0 0 9   //更新shuai键值
123456789
STORED

gets shuai            //查看
VALUE shuai 0 9 4     //更新因子已经+1   shuai:键值 9:长度 123456789:值
123456789
END

cas shuai 0 0 8 4   //检查更新,更新因子相等则更新返回EXISTS	
78945612
STORED

get shuai
VALUE shuai 0 8     //更新因子已经更新到8
78945612

cas shuai 0 0 8 2  //如果不指定更新因子就会出错
78945612
EXISTS

append shuai 0 0 4  //键值后面增加数据
book
STORED

get shuai           //可以看到boot已经添加成功
VALUE shuai 0 12
78945612book
END

prepend shuai 0 0 2  //键值前面增加数据
un
STORED

get shuai            //添加成功
VALUE shuai 0 14
un78945612book
END

delete shuai       //删除shuai
DELETED
quit
Connection closed by foreign host.

Four: build memcache client

  • Build LAMP

Here we will not list them one by one. You can check my previous blog: Deploying LAMP Architecture

Insert picture description here

4.1: Build memcache client

[root@localhost ~]# tar zxvf memcache-2.2.7.tgz -C /opt
//增加为PHP的模块后在对memcache进行配置
[root@localhost memcache-2.2.7]# /usr/local/php5/bin/phpize

Zend Extension Api No:   220131226
[root@localhost memcache-2.2.7]# ./configure \
--enbale-memcache \
--with-php-config=/usr/local/php5/bin/php-config 

//
[root@localhost memcache-2.2.7]# ./configure \
--enable-memcache \            //开启memcache
--with-php-config=/usr/local/php5/bin/php-config    //关联php配置文件

//编译
[root@localhost memcache-2.2.7]# make && make install

//共享文件
Installing shared extensions:     /usr/local/php5/lib/php/extensions/no-debug-zts-20131226/

vim /usr/local/php5/php.ini   //搜索并修改下面一行,在新增一行

//搜索extension——dir
; extension_dir = "ext"
//下面添加
extension_dir="/usr/local/php5/lib/php/extensions/no-debug-zts-20131226/"
extension=memcache.so           //指向memcache模块
....省略信息........

4.2: Test the server

  • Use the client to detect whether the server is connected normally
<?php
$memcache = new Memcache();
$memcache->connect('20.0.0.41',11211);   //指定memcache服务器地址
$memcache->set('key','Memcache test Successfull!',0,60);
$result = $memcache->get('key');
unset($memcache);
echo$result;
?>


//重启服务
[root@localhost memcache-2.2.7]# service httpd stop
[root@localhost memcache-2.2.7]# service httpd start

4.3: Test on the server

mark
This concludes this article, thanks for watching

Guess you like

Origin blog.csdn.net/weixin_47151643/article/details/108802137