PHP操作memcache的方法:
http://php.net/manual/zh/book.memcache.php
header("Content-type:text/html;charset=utf-8"); $memcache=new Memcache(); if(!$memcache){ die("PHP不支持memcache"); } $flag=$memcache->connect("192.168.1.80",11211); if(!$flag){ die("memcache服务器连接失败"); } //$res=$memcache->set("username","james",0,600); $res=$memcache->set("username_age",33,0,600); if($res){ echo "插入成功"; }else{ echo "插入失败"; }
或者直接查看:
客户端操作:
telnet 192.168.1.80 11211
进行连接
1、set方法
基本语法结构:set key flags exptime bytes
key:设置的键
flags:是否压缩
exptime:多长时间过期
bytes:设置value值得长度
set username 0 1000 5 james STORED
2、get方法
基本语法结构:get key
get username VALUE username 0 5 james END
3、replace方法
基本语法结构:replace key flags exptime bytes
replace username 0 1000 13 songyongzhan1 STORED get username VALUE username 0 13 songyongzhan1 END replace username 0 500 5 james STORED get username VALUE username 0 5 james END
4、append
append key flags exptime bytes [noreply] set aa 0 900 5 james STORED append aa 0 900 4 abcd STORED get aa VALUE aa 0 9 jamesabcd END 设置 aa 这个变量 append aa 新增字符的长度为4 ,不是指的新字符串的长度。
5、gets
获取多个key的值,多个key使用空格分隔
get aa VALUE aa 0 9 jamesabcd END set book 0 900 4 java STORED get book VALUE book 0 4 java END get aa VALUE aa 0 9 jamesabcd END gets aa book VALUE aa 0 9 5 jamesabcd VALUE book 0 4 6 java END
gets aa book
同时显示多个结果
6、delete
删除key
delete aa
get aa VALUE aa 0 9 jamesabcd END delete aa DELETED get aa END
删除了,返回deleted
7、stats
stats STAT pid 2307 STAT uptime 1969 STAT time 1478155587 STAT version 1.4.32 STAT libevent 1.4.12-stable STAT pointer_size 32 STAT rusage_user 0.148977 STAT rusage_system 0.703892 STAT curr_connections 10 STAT total_connections 11 STAT connection_structures 11 STAT reserved_fds 20 STAT cmd_get 16 STAT cmd_set 6 STAT cmd_flush 0 STAT cmd_touch 0 STAT get_hits 11 STAT get_misses 5 STAT get_expired 1 STAT get_flushed 0 STAT delete_misses 0 STAT delete_hits 2 STAT incr_misses 0 STAT incr_hits 0 STAT decr_misses 0 STAT decr_hits 0 STAT cas_misses 0 STAT cas_hits 0 STAT cas_badval 0 STAT touch_hits 0 STAT touch_misses 0 STAT auth_cmds 0 STAT auth_errors 0 STAT bytes_read 667 STAT bytes_written 5767 STAT limit_maxbytes 104857600 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT time_in_listen_disabled_us 0 STAT threads 4 STAT conn_yields 0 STAT hash_power_level 16 STAT hash_bytes 262144 STAT hash_is_expanding 0 STAT malloc_fails 0 STAT log_worker_dropped 0 STAT log_worker_written 0 STAT log_watcher_skipped 0 STAT log_watcher_sent 0 STAT bytes 0 STAT curr_items 0 STAT total_items 6 STAT expired_unfetched 0 STAT evicted_unfetched 0 STAT evictions 0 STAT reclaimed 0 STAT crawler_reclaimed 0 STAT crawler_items_checked 0 STAT lrutail_reflocked 0 END
8、incr decr
incr key incrvalue
decr key num
set age 0 0 2 44 STORED incr age 1 45 incr age 2 ERROR incr age 2 47 decr age 2 45
增加、减少
9、stats slabs
stats slabs
STAT 1:chunk_size 80
STAT 1:chunks_per_page 13107
STAT 1:total_pages 1
STAT 1:total_chunks 13107
STAT 1:used_chunks 1
STAT 1:free_chunks 13106
STAT 1:free_chunks_end 0
STAT 1:mem_requested 54
STAT 1:get_hits 12
STAT 1:cmd_set 9
STAT 1:delete_hits 2
STAT 1:incr_hits 2
STAT 1:decr_hits 1
STAT 1:cas_hits 0
STAT 1:cas_badval 0
STAT 1:touch_hits 0
STAT active_slabs 1
STAT total_malloced 1048560
END
10、flush_all
清空memcache