Linux学习-PHP连接Memcached服务

Posted 丢爸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习-PHP连接Memcached服务相关的知识,希望对你有一定的参考价值。

PHP 连接 Memcached 服务

#下载memcache
[root@mysql1 ~]# wget http://pecl.php.net/get/memcache-2.2.7.tgz
--2021-04-18 03:29:49--  http://pecl.php.net/get/memcache-2.2.7.tgz
Resolving pecl.php.net... 104.236.228.160
Connecting to pecl.php.net|104.236.228.160|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 36459 (36K) [application/octet-stream]
Saving to: “memcache-2.2.7.tgz”

100%[===========================================================>] 36,459       129K/s   in 0.3s    

2021-04-18 03:29:50 (129 KB/s) - “memcache-2.2.7.tgz” saved [36459/36459]

[root@mysql1 ~]# tar xf memcache-2.2.7.tgz 
[root@mysql1 ~]# cd memcache-2.2.7
#phpize对php进行添加扩展。并且phpize编译的扩展库可以随时启用或停用
[root@mysql1 memcache-2.2.7]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
[root@mysql1 memcache-2.2.7]# ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
[root@mysql1 memcache-2.2.7]# make && make install

#添加配置文件
[root@mysql1 memcache-2.2.7]# mkdir /etc/php.d
[root@mysql1 memcache-2.2.7]# vim /etc/php.d/memcache.ini
extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so
#重新启动php-fpm

#测试memcached连接功能
#----php测试脚本
<?php
 #phpinfo();
 $mem=new Memcache;
 $mem->connect("127.0.0.1",11211);
 $version=$mem->getVersion();
 echo "Server's version:".$version."<br>\\n";
 $mem->set("testkey","helloworld",0.600);
 echo "Store data in the cache</br>";
 $get_result=$mem->get("testkey");
 echo "The Result is:$get_result.";
?>
#----php测试脚本

配置PHP将会话保存至memcached中

#编辑/etc/php.ini,添加以下两行
session.save_handler = memcache
session.save_pah = "tcp://localhost:11211?persistent=1&weight=1timeout=1&retry_interval=15"
#测试页面--testphpsession.php
<?php
  session_start();
  if (!isset($_SESSION['www.tye.com'])) {
    $_SESSION['www.tye.com'] = time();
  }
  print $['www.tye.com'];
  print "<br><br>";
  print "Session ID:" .session_id();
?>
#测试页面--showphpsession.php
<?php
  session_start();
  $memcache_obj=new Memcache;
  $memcache_obj->connect('127.0.0.1',11211);
  $my_session=session_id();
  var_dump($memcache_obj->get($my_session));
  $memcache_obj->close();
?>

以上是关于Linux学习-PHP连接Memcached服务的主要内容,如果未能解决你的问题,请参考以下文章

linux 安装memcached服务端和php memcached扩展

php memcached在windows上的使用

php开发memcached

Memcached安装和PHP连接Memcached

linux php安装memcached扩展

linux php安装memcached扩展