PHP 使用 Memcached

PHP 想往 Memcached 中增删查改数据,需要先安装 memcache 扩展模块:

cd /usr/local/src
wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
tar xf memcache-2.2.3.tgz
cd memcache-2.2.3
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
echo 'extension = "memcache.so"' >> /usr/local/php/etc/php.ini
/usr/local/php/bin/php -m | grep memcache
/etc/init.d/php-fpm restart


测试 PHP 是否连接 Memcached:

[root@localhost ~]# curl http://www.apelearn.com/study_v2/.memcache.txt > 1.php
[root@localhost ~]# /usr/local/php/bin/php 1.php    //看到如下输出则表示连接成功
Get key1 value: This is first value<br>Get key1 value: This is replace value<br>Get key2 value: Array
(
    [0] => aaa
    [1] => bbb
    [2] => ccc
    [3] => ddd
)


把 PHP 的 session(就是用户登录的信息)存到 Memcached:

[root@localhost ~]$ vim /usr/local/php/etc/php-fpm.conf

[www]    // 在进程池中配置
......
php_value[session.save_handler] = memcache
php_value[session.save_path] = "tcp://127.0.0.1:11211"
[root@localhost ~]$ /etc/init.d/php-fpm restart

 

 

 

 

 

 

 

 

    

posted @ 2019-03-02 11:58  孔雀东南飞  阅读(180)  评论(0编辑  收藏  举报