PHP 连接 Memcached 服务

PHP Memcache 扩展安装

环境

ubuntu 20.04

查看php 所有扩展

php --ini
php -m

apt-cache查看需要安装的memcached扩展

apt-cache search memcached php

安装memcached 扩展

sudo apt install php-memcache

检测memcache扩展是否已安装

php -m | grep memcache

PHP 连接 Memcached

test.php

<?php
$memcache = new Memcache;             //创建一个memcache对象
$memcache->connect('localhost', 11211) or die ("Could not connect"); //连接Memcached服务器
$memcache->set('key', 'huyongjian');        //设置一个变量到内存中,名称是key 值是test
$get_value = $memcache->get('key');   //从内存中取出key的值
echo $get_value;
echo PHP_EOL;

运行test.php

ubuntu@VM-0-14-ubuntu:~$ php test.php
huyongjian