11月8号

21.5 memcached命令行

Memcached语法规则:

1. <command name> <key> <flags> <exptime> <bytes>\r\n <data block>\r\n

注:\r\n在windows下是Enter键

2. <command name> 可以是set, add, replace

3. set表示按照相应的<key>存储该数据,没有的时候增加,有的时候覆盖

4. add表示按照相应的<key>添加该数据,但是如果该<key>已经存在则会操作失败

5. replace表示按照相应的<key>替换数据,但是如果该<key>不存在则操作失败。

6. <key> 客户端需要保存数据的key

7.  <flags> 是一个16位的无符号的整数(以十进制的方式表示)。该标志将和需要存储的数据一起存储,并在客户端get数据时返回。客户端可以将此标志用做特殊用途,此标志对服务器来说是不透明的。

8. <exptime> 为过期的时间。若为0表示存储的数据永远不过期(但可被服务器算法:LRU 等替换)。如果非0(unix时间或者距离此时的秒数),当过期后,服务器可以保证用户得不到该数据(以服务器时间为标准)。

9. <bytes> 需要存储的字节数,当用户希望存储空数据时<bytes>可以为0

10. <data block>需要存储的内容,输入完成后,最后客户端需要加上\r\n(直接点击Enter)作为结束标志。

启动:

[root@hao-01 ~]# systemctl start memcached

1. 安装telnet

[root@hao-01 ~]# yum install -y telnet

2. 用telnet 指定ip 端口,进入telnet界面:

[root@hao-01 ~]# telnet 127.0.0.1 11211

写入存储格式(英语单词表示):

set key1 flags exptime bytes

data block

提示:在telnet模式下,退格键需要按住Ctrl键

写入存储格式:

set key1 0 30 5  

12345

查看key1存储的内容(查看超出了存储时间,会查不到的):

get key1

get key1

提示:在telnet模式下,退格键需要按住Ctrl键

实验案例:

设定key1:flags为1 120秒到期时间 4个存储字节

set key1 1 120 4          

1234

替换key1:flags为1 0为不限制时间 6个存储字节

replace key1 1 0 6

123456

查看key1的值:

删除key1的值:

delete key1

退出 Ctrl+] 在输入quit

21.6 memcached数据导出和导入

1. 导出memcached的数据:

[root@hao-01 ~]# memcached-tool 127.0.0.1:11211 dump

2. 把 导出memcached的数据,重定向到指定文件data.txt内:

[root@hao-01 ~]# memcached-tool 127.0.0.1:11211 dump > data.txt

3. 重启memcached:

(清理之前存储的数据,为了导入已存在的数据会不被覆盖的)

[root@hao-01 ~]# systemctl restart memcached

4. data.txt导入到memcached:

若nc命令不存在,yum install nc

注意:导出的数据是带有一个时间戳的,这个时间戳就是该条数据过期的时间点,如果当前时间已经超过该时间戳,那么是导入不进去的

21.7 php连接memcached

1. 进入/...src/目录下,下载memcache包:

[root@hao-01 ~]# cd /usr/local/src/

[root@hao-01 src]# wget http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz

2. 解压:

[root@hao-01 src]# tar zxvf memcache-2.2.3.tgz

3. 进入memcache-2.2.3,生成phpize文件:

[root@hao-01 src]# cd memcache-2.2.3

#[root@hao-01 src]# yum install -y autoconf

[root@hao-01 memcache-2.2.3]# /usr/local/php-fpm/bin/phpize

4. 编译

[root@hao-01 memcache-2.2.3]# ./configure --with-php-config=/usr/local/php-fpm/bin/php-config

[root@hao-01 memcache-2.2.3]# echo $?

5. make和make install执行:

[root@hao-01 memcache-2.2.3]# make && make install

[root@hao-01 memcache-2.2.3]# echo $?

6. make install 生成如下文件:

[root@hao-01 memcache-2.2.3]# ls /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/

7. 编辑php-ini:

[root@hao-01 memcache-2.2.3]# vim /usr/local/php-fpm/etc/php-ini

添加内容扩展:

extension=memcache.so

8. 检查有没有memcache这个模块:

[root@hao-01 memcache-2.2.3]# /usr/local/php-fpm/bin/php -m

21.8 memcached中存储session

1. 下载session存储脚本:

[root@hao-01 ~]# wget http://study.lishiming.net/.mem_se.txt

2. 进入

[root@hao-01 ~]# cd /data/wwwroot/test/

3. 移动session存储脚本,并重命名为php格式脚本:

[root@hao-01 test.com]# mv /root/.mem_se.txt  1.php

4. curl访问1.php:

[root@hao-01 test.com]# curl localhost/1.php

5. 查看tmp下有没有sess_格式的文件:

[root@hao-01 test.com]# ls -lt /tmp/

6. 编辑php-ini:

[root@hao-01 test.com]# vim /usr/local/php-fpm/etc/php-ini

注释掉这行:;session.save_handler = files

添加:

session.save_handler = memcache session.save_path

"tcp://192.168.0.9:11211" 

本实例是在lamp/lnmp环境下实现(下面哪种没问题,就用哪种)

1. 编辑php.ini添加两行

session.save_handler = memcache session.save_path

"tcp://192.168.0.9:11211" 

2. 或者httpd.conf中对应的虚拟主机中添加

php_value session.save_handler "memcache" php_value session.save_path "tcp://192.168.0.9:11211" 

3. 或者php-fpm.conf对应的pool中添加

php_value[session.save_handler] = memcache

php_value[session.save_path] = " tcp://192.168.0.9:11211 "

7. curl访问1.php:

[root@hao-01 test.com]# curl localhost/1.php

转载于:https://my.oschina.net/u/3869214/blog/2874807

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值