21.5 memcached命令行
·memcached 语法规则
<command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n
注:\r\n在 windows 下是 Enter 键
·<command name> 可以是 set, add, replace
·set 表示按照相应的 <key> 存储该数据,没有的时候增加,有的时候覆盖
·add 表示按照相应的 <key> 添加该数据,但是如果该 <key> 已经存在则会操作失败
·replace 表示按照相应的 <key> 替换数据,但是如果该 <key> 不存在则操作失败。
·<key> 客户端需要保存数据的key,名称自定义
·<flags> 是一个16位的无符号的整数(以十进制的方式表示)。该标志将和需要存储的数据一起存储,并在客户端 get 数据时返回。客户端可以将此标志用做特殊用途,此标志对服务器来说是不透明的。
·<exptime> 为过期的时间,单位为秒。若为0表示存储的数据永远不过期(但可被服务器算法:LRU 等替换)。如果非0(unix时间或者距离此时的秒数),当过期后,服务器可以保证用户得不到该数据 (以服务器时间为标准)。
·<bytes> 需要存储的字节数,当用户希望存储空数据时 <bytes> 可以为0。字节数写几,那么必须输入几位
·<data block> 需要存储的内容,输入完成后,最后客户端需要加上 \r\n(直接点击Enter)作为结束标志。
·数据示例:
[[email protected] ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. set key3 1 100 4 1234 STORED get key3 VALUE key3 1 4 1234 END replace key3 1 0 5 abcde STORED get key3 VALUE key3 1 5 abcde END delete key3 DELETED get key3 END ^] telnet> quit Connection closed.
(输入错误需要按 Ctrl + 退格,退出按 Ctrl + ],再 quit )
21.6 memcached数据导出和导入
·重启memcached服务的时候,最好将数据导出,重启完之后,再将数据导入
我们先存储一些数据:
[[email protected] ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. get key3 END set key3 1 0 5 abcde STORED get key3 VALUE key3 1 5 abcde END set name 1 0 6 alexis STORED set age 1 0 2 29 STORED set k1 1 0 5 12345 STORED ^] telnet> quit Connection closed.
·查看数据
[[email protected] ~]# memcached-tool 127.0.0.1:11211 dump Dumping memcache contents Number of buckets: 1 Number of items : 4 Dumping bucket 1 - 4 total items add k1 1 1526958255 5 12345 add name 1 1526958255 6 alexis add age 1 1526958255 2 29 add key3 1 1526958255 5 abcde
·导出数据
[[email protected] ~]# memcached-tool 127.0.0.1:11211 dump > data.txt Dumping memcache contents Number of buckets: 1 Number of items : 4 Dumping bucket 1 - 4 total items
·导入数据:
[[email protected] ~]# nc 127.0.0.1 11211 < data.txt NOT_STORED NOT_STORED NOT_STORED NOT_STORED
(因为之前是add添加的数据,而且已经存在了,所以导入失败)
可以重启 memcached ,清空数据,再导入
[[email protected] ~]# systemctl restart memcached.service [[email protected] ~]# nc 127.0.0.1 11211 < data.txt STORED STORED STORED STORED
但是导入数据后,查看依然没有数据
[[email protected] ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. get k1 END get name END get age END get key3 END ^] telnet> quit Connection closed.
这是因为导出数据时,在 data.txt 中的时间戳已经过期
增加一小时时间戳,并更改 data.txt 中时间戳
[[email protected] ~]# date -d "+1 hour" +%s 1526967232 [[email protected] ~]# vim data.txt
[[email protected] ~]# systemctl restart memcached.service [[email protected] ~]# nc 127.0.0.1 11211 < data.txt STORED STORED STORED STORED [[email protected] ~]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. get k1 VALUE k1 1 5 12345 END get name VALUE name 1 6 alexis END get age VALUE age 1 2 29 END get key3 VALUE key3 1 5 abcde END ^] telnet> quit Connection closed.
数据已经导入
因此,我们在 set 数据时,添加的 <exptime> 最好大一些。
21.7 php连接memcached
·查看已安装php-fpm模块:
[[email protected] ~]# /usr/local/php-fpm/sbin/php-fpm -m
·下载memcached php扩展:
[[email protected] src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz --2018-05-22 22:09:29-- http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz 正在解析主机 www.apelearn.com (www.apelearn.com)... 47.104.7.242 正在连接 www.apelearn.com (www.apelearn.com)|47.104.7.242|:80... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:27366 (27K) [application/octet-stream] 正在保存至: “memcache-2.2.3.tgz” 100%[==============================================================================================================>] 27,366 --.-K/s 用时 0.1s 2018-05-22 22:09:29 (193 KB/s) - 已保存 “memcache-2.2.3.tgz” [27366/27366]) [[email protected] memcache-2.2.3]# /usr/local/php-fpm/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 [[email protected] memcache-2.2.3]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config ...... [[email protected] memcache-2.2.3]# make && make install ...... Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/ [[email protected] memcache-2.2.3]# ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/ memcache.so opcache.a opcache.so [[email protected] memcache-2.2.3]# vim /usr/local/php-fpm/etc/php.ini##编辑配置
加上extension=memcache.so
[[email protected] memcache-2.2.3]# /usr/local/php-fpm/bin/php -m
memcache模块已经添加成功
·下载测试脚本测试:
[[email protected] ~]# curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null [[email protected] ~]# cat 1.php <?php //连接Memcache Memcache $mem = new Memcache; $mem->connect("localhost", 11211); //保存数据 $mem->set('key1', 'This is first value', 0, 60); $val = $mem->get('key1'); echo "Get key1 value: " . $val ."<br>"; //替换数据 $mem->replace('key1', 'This is replace value', 0, 60); $val = $mem->get('key1'); echo "Get key1 value: " . $val . "<br>"; //保存数组数据 $arr = array('aaa', 'bbb', 'ccc', 'ddd'); $mem->set('key2', $arr, 0, 60); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "<br>"; //删除数据 $mem->delete('key1'); $val = $mem->get('key1'); echo "Get key1 value: " . $val . "<br>"; //清除所有数据 $mem->flush(); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "<br>"; //关闭连接 $mem->close(); ?> [[email protected] ~]# /usr/local/php-fpm/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 ) <br>Get key1 value: <br>Get key2 value: <br>[[email protected] ~]# [[email protected] ~]#
或者将1.php放到某个虚拟主机根目录下面,在浏览器访问,即可看到效果
最终可以看到数据如下:
[0] => aaa
[1] => bbb
[2] => ccc
[3] => ddd
(出现以上就是正确的)
错误汇总:
phpize时出现错误:
[[email protected] memcache-2.2.3]# /usr/local/php-fpm/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script.
解决方法:
[[email protected] memcache-2.2.3]# cd .. [[email protected] src]# wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz [[email protected] src]# tar -zvxf m4-1.4.9.tar.gz [[email protected] src]# cd m4-1.4.9/ [[email protected] m4-1.4.9]# ./configure && make && make install [[email protected] m4-1.4.9]# cd ../ [[email protected] src]# wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz [[email protected] src]# tar -zvxf autoconf-2.62.tar.gz [[email protected] src]# cd autoconf-2.62/ [[email protected] autoconf-2.62]# ./configure && make && make install [[email protected] autoconf-2.62]# cd ../memcache-2.2.3/ [[email protected] memcache-2.2.3]# /usr/local/php-fpm/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226
21.8 memcached中存储sessions
session.save_handler = memcache ##指定存储类型
session.save_path = "tcp://192.168.65.128:11211" ##指定memcached服务器和端口
在/data/wwwroot/default/ 中编辑1.php
<?php session_start(); if (!isset($_SESSION['TEST'])) { $_SESSION['TEST'] = time(); } $_SESSION['TEST3'] = time(); print $_SESSION['TEST']; print "<br><br>"; print $_SESSION['TEST3']; print "<br><br>"; print session_id(); ?>
[[email protected] test.com]# curl localhost/1.php 1527003347<br><br>1527003347<br><br>4bfh207asaueeasdelhu79em52
·如果使用的是apache,那么在httpd.conf中对应的虚拟主机中添加
php_value session.save_handler "memcache" php_value session.save_path "tcp://192.168.65.128:11211"
·或者php-fpm.conf对应的pool中添加
php_value[session.save_handler] = memcache
php_value[session.save_path] = " tcp://192.168.65.128:11211 "
[[email protected] test.com]# telnet 127.0.0.1 11211 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. get 4bfh207asaueeasdelhu79em52 VALUE 4bfh207asaueeasdelhu79em52 0 37 TEST|i:1527003347;TEST3|i:1527003347; END ^] telnet> quit Connection closed.
第四课写的比较匆忙,理解不充分,需要再看一遍
原文地址:http://blog.51cto.com/11530642/2119284