PHP7访问Memcached

一、客户端安装

memcached 是高性能的分布式内存缓存服务器,而PHP memcache 和 memcached 都是 Memcached 服务器的 PHP 扩展。其中memcache 比 memcached 早出现,所以一些老的代码可能还在用 memcache 扩展。memcached 后来出现,并且大部分框架都支持。

memcache 是一个原生版本,完全是在 PHP 框架内开发的,支持 OO(面向对象) 和非 OO 两套接口并存,而 memcached 是建立在 libmemcached 的基础上的,只支持 OO 接口。这就意味着在安装 memcache 扩展的时候不要求安装其他的东西,但是在安装 memcached 的时候会要求你安装 libmemcached。别嫌麻烦!因为 libmemcached 是 memcache 的 C 客户端,它具有低内存,线程安全等优点。据说新浪微博之前就全面将 PHP 的 memcache 替换成 PHP 的 memcached,在高并发下,稳定性果断提高。 比较尴尬的一点是 memcached 和 memcache 对应的服务端名字都是 memcached,大家使用 ps aux | grep memcache 就会发现守护进程的名字是 memcached,其实后面的 d 可以理解为守护进程,因此网上有的人说 memcache 是客户端, memcached 是服务端就是这个意思,所以不要将客户端的 memcached 和服务端的 memcached 混淆了就好。

1.1 安装PHP7

1.1.1 安装PHP7

参考《PHP安装(CentOS)》

1.2 安装memcached扩展

1.2.1 安装libmemcached

官网:https://launchpad.net/libmemcached

下载最新版本libmemcached-1.0.18.tar.gz,并上传到服务器

[root@localhost ~]# ll
total 3976
-rw-r--r--. 1 root root  276963 Aug  5 19:51 1.so
-rw-r--r--. 1 root root   25062 Jul 23 00:57 536403.webp
-rw-r--r--  1 root root 1039649 Aug 19 22:04 libmemcached-1.0.18.tar.gz
-rw-r--r--  1 root root  162697 Aug 19 22:04 php-memcached-master.zip

解压缩:tar -zxvf libmemcached-1.0.18.tar.gz

root@localhost ~]# tar -zxvf libmemcached-1.0.18.tar.gz 
libmemcached-1.0.18/
libmemcached-1.0.18/Makefile.am
libmemcached-1.0.18/aclocal.m4
libmemcached-1.0.18/COPYING
......
libmemcached-1.0.18/win32/include.am
libmemcached-1.0.18/AUTHORS
[root@localhost ~]# 
[root@localhost ~]# ll
total 3980
-rw-r--r--.  1 root root  276963 Aug  5 19:51 1.so
-rw-r--r--.  1 root root   25062 Jul 23 00:57 536403.webp
drwxr-xr-x  23  800  800    4096 Feb  9  2014 libmemcached-1.0.18
-rw-r--r--   1 root root 1039649 Aug 19 22:04 libmemcached-1.0.18.tar.gz
-rw-r--r--   1 root root  162697 Aug 19 22:04 php-memcached-master.zip

移动到安装目录:mv libmemcached-1.0.18 /usr/libmemcached

[root@localhost ~]# cd /usr/libmemcached/
[root@localhost libmemcached]# ll
total 2260
-rw-r--r-- 1 800 800   40316 Feb  9  2014 aclocal.m4
-rw-r--r-- 1 800 800     468 Feb  9  2014 AUTHORS
drwxr-xr-x 2 800 800     128 Feb  9  2014 build-aux
-rw-r--r-- 1 800 800   21952 Feb  9  2014 ChangeLog
drwxr-xr-x 2 800 800    4096 Feb  9  2014 clients
-rwxr-xr-x 1 800 800 1064573 Feb  9  2014 configure
-rw-r--r-- 1 800 800   12069 Feb  9  2014 configure.ac
-rw-r--r-- 1 800 800    1559 Feb  9  2014 COPYING
drwxr-xr-x 2 800 800      42 Feb  9  2014 docs
drwxr-xr-x 2 800 800     195 Feb  9  2014 example
drwxr-xr-x 2 800 800    4096 Feb  9  2014 libhashkit
drwxr-xr-x 2 800 800     277 Feb  9  2014 libhashkit-1.0
drwxr-xr-x 4 800 800    4096 Feb  9  2014 libmemcached
drwxr-xr-x 5 800 800    4096 Feb  9  2014 libmemcached-1.0
drwxr-xr-x 3 800 800      36 Feb  9  2014 libmemcachedinternal
drwxr-xr-x 2 800 800     199 Feb  9  2014 libmemcachedprotocol
drwxr-xr-x 2 800 800      92 Feb  9  2014 libmemcachedprotocol-0.0
drwxr-xr-x 2 800 800     118 Feb  9  2014 libmemcachedutil
drwxr-xr-x 2 800 800     130 Feb  9  2014 libmemcachedutil-1.0
drwxr-xr-x 5 800 800    4096 Feb  9  2014 libtest
drwxr-xr-x 2 800 800    4096 Feb  9  2014 m4
-rw-r--r-- 1 800 800    3536 Feb  9  2014 Makefile.am
-rw-r--r-- 1 800 800 1068952 Feb  9  2014 Makefile.in
drwxr-xr-x 2 800 800    8192 Feb  9  2014 man
-rw-r--r-- 1 800 800   17133 Feb  9  2014 mem_config.in
-rw-r--r-- 1 800 800      14 Feb  9  2014 NEWS
-rw-r--r-- 1 800 800    1461 Feb  9  2014 README
-rw-r--r-- 1 800 800     922 Feb  9  2014 README.FIRST
-rw-r--r-- 1 800 800     802 Feb  9  2014 README.win32
drwxr-xr-x 2 800 800      24 Feb  9  2014 rpm
drwxr-xr-x 2 800 800     127 Feb  9  2014 support
drwxr-xr-x 3 800 800    4096 Feb  9  2014 tests
-rw-r--r-- 1 800 800     630 Feb  9  2014 THANKS
-rw-r--r-- 1 800 800     416 Feb  9  2014 TODO
drwxr-xr-x 2 800 800     207 Feb  9  2014 util
-rw-r--r-- 1 800 800      38 Feb  9  2014 version.m4
drwxr-xr-x 2 800 800      42 Feb  9  2014 win32
[root@localhost libmemcached]# 

检测:./configure -prefix=/usr/libmemcached –with-memcached

编译:make

安装之前,需要把gcc版本切换回默认版本,之前编译redis时,切换到了9,否则会报错error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

[root@localhost libmemcached]# gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 

安装:make install

1.2.2 安装memcached

php使用memcache的扩展有两个,一个memcache,一个memcached,前者比较老,推荐使用第二个,我们这里以第二个为例:

github上找到适用于php7的分支:https://github.com/php-memcached-dev/php-memcached

下载并上传到服务器:php-memcached-master.zip

[root@localhost ~]# ll
total 3976
-rw-r--r--. 1 root root  276963 Aug  5 19:51 1.so
-rw-r--r--. 1 root root   25062 Jul 23 00:57 536403.webp
-rw-r--r--  1 root root 1039649 Aug 19 22:04 libmemcached-1.0.18.tar.gz
-rw-r--r--  1 root root  162697 Aug 19 22:04 php-memcached-master.zip

解压缩:unzip php-memcached-master.zip

[root@localhost ~]# ll
total 3980
-rw-r--r--. 1 root root  276963 Aug  5 19:51 1.so
-rw-r--r--. 1 root root   25062 Jul 23 00:57 536403.webp
-rw-r--r--  1 root root 1039649 Aug 19 22:04 libmemcached-1.0.18.tar.gz
drwxr-xr-x  6 root root    4096 Mar 11 07:00 php-memcached-master
-rw-r--r--  1 root root  162697 Aug 19 22:04 php-memcached-master.zip

移动到安装目录:mv php-memcached-master /usr/php-memcached

[root@localhost ~]# mv php-memcached-master /usr/php-memcached
[root@localhost ~]# cd /usr/php-memcached/
[root@localhost php-memcached]# ll
total 308
-rw-r--r-- 1 root root  12047 Mar 11 07:00 ChangeLog
-rw-r--r-- 1 root root  15022 Mar 11 07:00 config.m4
-rw-r--r-- 1 root root   1502 Mar 11 07:00 config.w32
-rw-r--r-- 1 root root     59 Mar 11 07:00 CREDITS
drwxr-xr-x 2 root root     53 Mar 11 07:00 fastlz
-rw-r--r-- 1 root root   2297 Mar 11 07:00 g_fmt.c
-rw-r--r-- 1 root root   1181 Mar 11 07:00 g_fmt.h
-rw-r--r-- 1 root root   3218 Mar 11 07:00 LICENSE
-rw-r--r-- 1 root root   7223 Mar 11 07:00 memcached-api.php
-rw-r--r-- 1 root root   6504 Mar 11 07:00 memcached.ini
-rw-r--r-- 1 root root  28115 Mar 11 07:00 package.xml
-rw-r--r-- 1 root root   2702 Mar 11 07:00 php_libmemcached_compat.c
-rw-r--r-- 1 root root   1707 Mar 11 07:00 php_libmemcached_compat.h
-rw-r--r-- 1 root root 136038 Mar 11 07:00 php_memcached.c
-rw-r--r-- 1 root root   1878 Mar 11 07:00 php_memcached.h
-rw-r--r-- 1 root root   6596 Mar 11 07:00 php_memcached_private.h
-rw-r--r-- 1 root root  24349 Mar 11 07:00 php_memcached_server.c
-rw-r--r-- 1 root root   1473 Mar 11 07:00 php_memcached_server.h
-rw-r--r-- 1 root root  14272 Mar 11 07:00 php_memcached_session.c
-rw-r--r-- 1 root root   1607 Mar 11 07:00 php_memcached_session.h
-rw-r--r-- 1 root root   1875 Mar 11 07:00 README.markdown
-rw-r--r-- 1 root root    812 Mar 11 07:00 README.win32.txt
drwxr-xr-x 2 root root     96 Mar 11 07:00 server-example
drwxr-xr-x 3 root root   4096 Mar 11 07:00 tests

生成检测文件:phpize

[root@localhost php-memcached]# phpize
Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303

检测:./configure –enable-memcached –with-php-config=/usr/bin/php-config –with-libmemcached-dir=/usr/libmemcached

[root@localhost php-memcached]# ./configure --enable-memcached --with-php-config=/usr/bin/php-config --with-libmemcached-dir=/usr/libmemcached
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
......
checking for libmemcached location... configure: error: Unable to find memcached.h under /usr/libmemcached
[root@localhost php-memcached]# 

编译:make

安装:make install

1.3 修改php.ini文件

在/etc/php.ini文件之中加入下面这句:

extension=/usr/lib64/php/modules/memcached.so
[root@localhost php-memcached]# find / -name memcached.so
/usr/lib64/php/modules/memcached.so
/usr/php-memcached/modules/memcached.so
/usr/php-memcached/.libs/memcached.so

检测模块是否正确安装:php-m

[root@localhost php-memcached]# php -m
PHP Warning:  Module 'json' already loaded in Unknown on line 0
[PHP Modules]
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
json
libxml
mbstring
memcached
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

[root@localhost php-memcached]# 

存在memcached模块,说明安装正确

二、开发测试

2.1 启动memcached

指定参数启动

memcached -d -m 64 -u root -l 192.168.0.150 -p 11211 -c 1024

查看状态

[root@localhost php-memcached]# ps -ef | grep memcached
root      52699      1  0 23:04 ?        00:00:00 memcached -d -m 64 -u root -l 192.168.0.150 -p 11211 -c 1024
root      52745   1374  0 23:05 pts/0    00:00:00 grep --color=auto memcached

配置文件参数启动:/etc/sysconfig/memcached

启动:systemctl start memcached
状态:systemctl status memcached
[root@localhost php-memcached]# systemctl status memcached
● memcached.service - Memcached
   Loaded: loaded (/usr/lib/systemd/system/memcached.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2020-08-19 23:03:47 EDT; 5s ago
 Main PID: 52627 (memcached)
   CGroup: /system.slice/memcached.service
           └─52627 /usr/bin/memcached -u memcached -p 11211 -m 64 -c 1024

Aug 19 23:03:47 localhost.localdomain systemd[1]: Started Memcached.
Aug 19 23:03:47 localhost.localdomain systemd[1]: Starting Memcached...

2.2 代码

2.2.1 编写
<?php

$mem = new Memcached(); //创建一个memcached对象  
$mem->addServer('192.168.0.150',11211);    //连接memcached
$mem->set("key","test123");//设置一个变量到内存中,名称是key 值是test  
$get_value = $mem->get("key"); //从内存中取出key的值  

echo "start...\n";      
var_dump($get_value);
echo $get_value . "\n";  
2.2.2 运行
[root@localhost php-memcached]# php test.php
PHP Warning:  Module 'json' already loaded in Unknown on line 0
start...
string(7) "test123"
test123

三、类常用函数

Memcached::add — 向一个新的key下面增加一个元素
Memcached::addByKey — 在指定服务器上的一个新的key下增加一个元素
Memcached::addServer — 向服务器池中增加一个服务器
Memcached::addServers — 向服务器池中增加多台服务器
Memcached::append — 向已存在元素后追加数据
Memcached::appendByKey — 向指定服务器上已存在元素后追加数据
Memcached::cas — 比较并交换值
Memcached::casByKey — 在指定服务器上比较并交换值
Memcached::__construct — 创建一个Memcached实例
Memcached::decrement — 减小数值元素的值
Memcached::delete — 删除一个元素
Memcached::deleteByKey — 从指定的服务器删除一个元素
Memcached::fetch — 抓取下一个结果
Memcached::fetchAll — 抓取所有剩余的结果
Memcached::flush — 作废缓存中的所有元素
Memcached::get — 检索一个元素
Memcached::getByKey — 从特定的服务器检索元素
Memcached::getDelayed — 请求多个元素
Memcached::getDelayedByKey — 从指定的服务器上请求多个元素
Memcached::getMulti — 检索多个元素
Memcached::getMultiByKey — 从特定服务器检索多个元素
Memcached::getOption — 获取Memcached的选项值
Memcached::getResultCode — 返回最后一次操作的结果代码
Memcached::getResultMessage — 返回最后一次操作的结果描述消息
Memcached::getServerByKey — 获取一个key所映射的服务器信息
Memcached::getServerList — 获取服务器池中的服务器列表
Memcached::getStats — 获取服务器池的统计信息
Memcached::getVersion — 获取服务器池中所有服务器的版本信息
Memcached::increment — 增加数值元素的值
Memcached::prepend — 向一个已存在的元素前面追加数据
Memcached::prependByKey — Prepend data to an existing item on a specific server
Memcached::replace — 替换已存在key下的元素
Memcached::replaceByKey — Replace the item under an existing key on a specific server
Memcached::set — 存储一个元素
Memcached::setByKey — Store an item on a specific server
Memcached::setMulti — 存储多个元素
Memcached::setMultiByKey — Store multiple items on a specific server
Memcached::setOption — 设置一个memcached选项

发表回复