21.5 memcached命令行

21.5 memcached命令行

[root@Dasoncheng ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set keyname 0 30 2
ab
STORED
set keyname2 0 30 3
abc
STORED

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@Dasoncheng ~]# 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    
##set是建立,若是存在key3会覆盖(add则会报错,replace若key3不存在则会报错)
1234
STORED
get key3     ##get 查看key3
VALUE key3 1 4
1234
END
replace key3 1 1000 2
ab
STORED
get key3
VALUE key3 1 2
ab
END
delete key3  ##delete删除
DELETED
get key3
END

21.6 memcached数据导出和导入

导出:
memcached-tool 127.0.0.1:11211 dump > data.txt
cat data.txt
导入:
nc 127.0.0.1 11211 < data.txt
若nc命令不存在,yum install nc
注意:导出的数据是带有一个时间戳的,这个时间戳就是该条数据过时的时间点,若是当前时间已经超过该时间戳,那么是导入不进去的php

[root@Dasoncheng ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set key1 1 0 1
a
STORED
set key2 1 10 2
ab
STORED
set key3 1 0 3
abc
STORED
set key4 1 0 4
abcd
STORED
^]
telnet> quit
Connection closed.

导出:linux

[root@Dasoncheng ~]# 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 key1 1 1507602916 1
a
add key4 1 1507602916 4
abcd
add key3 1 1507602916 3
abc
[root@Dasoncheng ~]# memcached-tool 127.0.0.1:11211 dump >data.txt
Dumping memcache contents
  Number of buckets: 1
  Number of items  : 3
Dumping bucket 1 - 3 total items
[root@Dasoncheng ~]# cat data.txt
add key1 1 1507602916 1
a
add key4 1 1507602916 4
abcd
add key3 1 1507602916 3
abc
[root@Dasoncheng ~]# memcached-tool 127.0.0.1:11211 stats
#127.0.0.1:11211   Field       Value
        curr_connections          11
              curr_items           3
               decr_hits           0
             decr_misses           0
             delete_hits           1
           delete_misses           0
       evicted_unfetched           0
               evictions           0
       expired_unfetched           2
                get_hits           8
……

导入:git

[root@Dasoncheng ~]# nc 127.0.0.1 11211 <data.txt    ##这里为何为失败呢?由于咱们用的是add,但nosql里面已经有了这些数据 因此:
NOT_STORED
NOT_STORED
NOT_STORED
[root@Dasoncheng ~]# systemctl restart memcached    ##重启服务,清除掉内存里面的数据
[root@Dasoncheng ~]# nc 127.0.0.1 11211 <data.txt   
STORED
STORED
STORED
[root@Dasoncheng ~]# !t
telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
END
get key2
END
^] 
##为何数据都不存在呢?咱们查看data.txt  看看有什么猫腻
telnet> quit
Connection closed.
[root@Dasoncheng ~]# cat data.txt     
add key1 1 1507602916 1
a
add key4 1 1507602916 4
abcd
add key3 1 1507602916 3
abc
[root@Dasoncheng ~]# date -d @1507602916    ##原来这里面add加上了他们建立的时候带的时间戳(即便咱们设定0用不过去),故导入的数据已通过期了
Tue Oct 10 10:35:16 CST 2017
[root@Dasoncheng ~]# date -d "+1 hour" +%s
1507690161
[root@Dasoncheng ~]# vim data.txt   ##这里咱们修改时间戳推后一小时
[root@Dasoncheng ~]# cat data.txt 
add key1 1 1507690161 1    ##修改成后一小时的时间戳
a
add key4 1 1507602916 4
abcd
add key3 1 100 3   ##修改过时时间为100秒
abc
[root@Dasoncheng ~]# nc 127.0.0.1 11211 <data.txt    ##成功导入
STORED
STORED
STORED
[root@Dasoncheng ~]# !t
telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
get key1
VALUE key1 1 1
a
END
get key2
END
get key3
VALUE key3 1 3
abc
END
get key4
END
^]
telnet> quit  
Connection closed.

21.7 php链接memcached

先安装php的memcache扩展
cd /usr/local/src/
wget  http://www.apelearn.com/bbs/data/attachment/forum/memcache-2.2.3.tgz
tar zxf memcache-2.2.3.tgz 
cd memcache-2.2.3
/usr/local/php-fpm/bin/phpize
./configure --with-php-config=/usr/local/php-fpm/bin/php-config
make && make install
安装完后会有相似这样的提示: Installing shared extensions: /usr/local/php-fpm/lib/php/extensions/no-debug-non-zts-20131226/
而后修改php.ini添加一行 extension="memcache.so“
检查/usr/local/php/bin/php-fpm -m
下载测试脚本 curl www.apelearn.com/study_v2/.memcache.txt > 1.php 2>/dev/null
1.php内容也能够参考https://coding.net/u/aminglinux/p/yuanke_centos7/git/blob/master/21NOSQL/1.php
执行脚本
/usr/local/php-fpm/bin/php 1.php
或者将1.php放到某个虚拟主机根目录下面,在浏览器访问,便可看到效果
最终能够看到数据以下:
[0] => aaa
[1] => bbb
[2] => ccc
[3] => ddd算法

21.8 memcached中存储session

本实例是在lamp/lnmp环境下实现sql

相关文章
相关标签/搜索