本文主要是介绍Memcached命令行, Memcached数据导出和导入, php连接memcached, memcached中存储sessions,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Memcached命令行
[root@draft ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set key1 1 0 3
abc
STORED
get key1
VALUE key1 1 3
abc
END
replace key1 1 100 3
def
STORED
get key1
VALUE key1 1 3
def
END
add key2 1 20 2
ab
STORED
get key2
VALUE key2 1 2
ab
END
get key2
END
get key1
ENDset key3 1 0 3
ghj
STORED
get key3
VALUE key3 1 3
ghj
END
delete key3
DELETED
get key3
END
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)作为结束标志。
Memcached 导出导入
[root@draft ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set k1 1 3600 3
abc
STORED
set k2 1 300 3
cde
STORED
set k3 1 0 3
efg
STORED
get k1
VALUE k1 1 3
abc
END
get k3
VALUE k3 1 3
efg
END
^]telnet> quit
Connection closed.
[root@draft ~]# memcached-tool 127.0.0.1:11211 dump
Dumping memcache contentsNumber of buckets: 1Number of items : 3
Dumping bucket 1 - 3 total items
add k2 1 1575960644 3
cde
add k1 1 1575963927 3
abc
add k3 1 1575960294 3 #不过期的项目,备份时变成时间戳,恢复后变成过期;
efg
[root@draft ~]# memcached-tool 127.0.0.1:11211 dump > dump.txt #备份;
Dumping memcache contentsNumber of buckets: 1Number of items : 3
Dumping bucket 1 - 3 total items
[root@draft ~]# systemctl restart memcached #重启清除数据,不重启会导不进去;
[root@draft ~]# memcached-tool 127.0.0.1:11211 stats
#127.0.0.1:11211 Field Valuecurr_items 0
[root@draft ~]# nc 127.0.0.1 11211 < dump.txt #恢复;
STORED
STORED
STORED
[root@draft ~]# 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 3
abc
END
get k2
VALUE k2 1 3
cde
END
get k3 #过期值为空;
END
PHP5.6连接memcached
1006 2019/12/06 19:57:35wget -c http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz tar zxf memcache-2.2.3.tgz1052 2019/12/06 20:31:48cd /root/memcache #进入解压后的包;1054 2019/12/06 20:32:33/usr/local/php-fpm/bin/phpize #运行phpize,生成configure文件;yum install -y m4 #可能需要这个包;yum install -y autoconf #可能需要这个包;1056 2019/12/06 20:33:31./configure --with-php-config=/usr/local/php-fpm/bin/php-config #路径是php的目录;1058 2019/12/06 20:33:43make1059 2019/12/06 20:33:52make install
Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/ #最后输出语句;
[root@draft memcache-2.2.3]# ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/ #在目录下生成so文件;
memcache.so opcache.a opcache.so1064 2019/12/06 20:36:28vim /usr/local/php-fpm/etc/php.ini #配置文件路径;extension=memcache.so #添加这个语句; [root@tanyvlinux3 no-debug-zts-20180731]# /usr/local/php/bin/php -i |grep dir #查询模块目录
extension_dir => /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226 => /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226[root@tanyvlinux3 no-debug-zts-20180731]# /usr/local/php/bin/php -i |grep ini #查询ini信息;
Configuration File (php.ini) Path => /usr/local/php-fpm/etc
Loaded Configuration File => /usr/local/php-fpm/etc/php.ini1073 2019/12/06 20:42:33service php-fpm restart
[root@draft ~]# /usr/local/php-fpm/bin/php -m |grep memcache #查看模块是否增加;
memcache
- 测试连接
[root@draft ~]# curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null #下载测试php文件或直接生成;
[root@draft ~]# cat 1.php #程序的意思就是使用Memcache作为变量(增加模块后,可调用Memcache作为变量),新建显示,删除显示,另外可存入不同数据类型;
<?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();
?>[root@draft ~]# /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>
- php7使用的包
1018 2019/12/06 20:09:53git clone https://github.com/websupport-sk/pecl-memcache memcache #php7要使用这个包;使用memcache的较新版本;https://pecl.php.net/package/memcached #要区分开两个工具;https://pecl.php.net/package/memcache
使用Memcached存放session
- 修改php的pool文件(最后两行),php5是修改php-fpm.conf文件(也是相当于php7的pool文件)
注意最后一行的分号,IP;[root@draft php-fpm]# cat /usr/local/php-fpm/etc/php-fpm.conf
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
php_value[session.save_handler] = memcache
php_value[session.save_path] = " tcp://127.0.0.1:11211 "
- 使用php页面测试session,页面内容如下
[root@draft php-fpm]# cat /usr/share/nginx/html/2.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();
?>
请自行做好web,web与php连接的相关事项
- 测试
[root@draft php-fpm]# curl 127.0.0.1/2.php
1576123039<br><br>1576123039<br><br>85b014km0h019vp7b63t68aif2 #正常的结果;
[root@draft php-fpm]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get 85b014km0h019vp7b63t68aif2 #get一个键;
VALUE 85b014km0h019vp7b63t68aif2 0 37
TEST|i:1576123039;TEST3|i:1576123039; #返回值;session已存在memcached中;
END
^] telnet> quit
Connection closed.
[root@draft php-fpm]# memcached-tool 127.0.0.1:11211 dump #使用dump查看memcached储存的值;
Dumping memcache contentsNumber of buckets: 2Number of items : 2
Dumping bucket 1 - 1 total items
add key1 0 1576066113 3
123
Dumping bucket 3 - 1 total items
add 85b014km0h019vp7b63t68aif2 0 1576088482 37
TEST|i:1576123039;TEST3|i:1576123039;
- 对比不修改session语句时(恢复php配置文件,php-fpm.conf),php生成的session文件都保存在/tmp下;
[root@draft php-fpm]# curl 127.0.0.1/2.php
1576124128<br><br>1576124128<br><br>n1b6rhium4i8rae5ajt390bfj6
[root@draft php-fpm]# ll /tmp/sess_n1b6rhium4i8rae5ajt390bfj6
-rw-------. 1 php-fpm php-fpm 37 12月 12 12:15 /tmp/sess_n1b6rhium4i8rae5ajt390bfj6
这篇关于Memcached命令行, Memcached数据导出和导入, php连接memcached, memcached中存储sessions的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!