[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
[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
導出:
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.
先安裝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算法
本實例是在lamp/lnmp環境下實現sql