Memcache

来源:互联网 发布:海信电视tv软件 编辑:程序博客网 时间:2024/05/29 19:28

Memcache

memcache和memcached

1,memcache是完全在PHP框架内开发的,memecached是使用libmemcached的。2,memcached 会比 memcache 多几个方法。

linux下操作memcache

1,启动memcache参数

memcache -h 可以查询帮助    -p <num>      设置TCP端口号(默认设置为: 11211)    -U <num>      UDP监听端口(默认: 11211, 0 时关闭)     -l <ip_addr>  绑定地址    -d            以daemon方式运行    -u <username> 绑定使用指定用于运行进程<username>    -m <num>      允许最大内存用量,单位M (默认: 64 MB)    -P <file>     将PID写入文件<file>,这样可以使得后边进行快速进程终止, 需要与-d 一起使用

2,连接以及退出

telnet 127.0.0.1 11211quitversion 查看版本

3,基本五种命令

set (key) (flags) (time) (bytes)

key 键值,存在则覆盖,不存在则添加flags 16位无符号整数,十进制表示,一起存入,作为特殊的透明用途time 时间,0表示永远保存bytes 需要存储的字节数例如:    set id 0 0 5    abcde

get

get key;

delete

delete key;

add //set

add id 0 0 5abcde也是存入数据,如果存在则无用,如果不存在则添加

replace //set

add id 0 0 5abcde也是存入值,如果存在则替换,如果不存在则无用

gets

set id  0 0 41234STOREDget idVALUE id 0 41234ENDgets idVALUE id 0 4 181234END可以知道多了一个18,用于标识名称/值对,如果对此名称/值对执行另一个set 命令,则gets 返回的额外值将会发生更改,表示已经更新

cas

它使用与 set 命令相类似的语法,但包括一个额外的值:gets 返回的额外值。set id 0 0 4  qwerSTOREDgets idVALUE id 0 4 20qwerENDcas id 0 0 4 63333EXISTScas id 0 0 4 204444STOREDgets 和cas 命令可以防止您使用自上次读取后经过更新的名称/值对

4,缓存管理命令

stats   转储所连接的 memcached 实例的当前统计数据stats itemsstats slabs   显示各个slab的信息,包括chunk的大小、数目、使用情况等stats sizes 输出所有item的大小和个数stats reset 清空统计数据------------------------------------------------------------------------------STAT pid 22459                             进程IDSTAT uptime 1027046                        服务器运行秒数STAT time 1273043062                       服务器当前unix时间戳STAT version 1.4.4                         服务器版本STAT libevent 2.0.21-stableSTAT pointer_size 64                       操作系统字大小(这台服务器是64位的)STAT rusage_user 0.040000                  进程累计用户时间STAT rusage_system 0.260000                进程累计系统时间STAT curr_connections 10                   当前打开连接数STAT total_connections 82                  曾打开的连接总数STAT connection_structures 13              服务器分配的连接结构数STAT reserved_fds 20STAT cmd_get 54                            执行get命令总数STAT cmd_set 34                            执行set命令总数STAT cmd_flush 3                           指向flush_all命令总数STAT get_hits 9                            get命中次数STAT get_misses 45                         get未命中次数STAT delete_misses 5                       delete未命中次数STAT delete_hits 1                         delete命中次数STAT incr_misses 0                         incr未命中次数STAT incr_hits 0                           incr命中次数STAT decr_misses 0                         decr未命中次数STAT decr_hits 0                           decr命中次数STAT cas_misses 0                          cas未命中次数STAT cas_hits 0                            cas命中次数STAT cas_badval 0                          使用擦拭次数STAT touch_hits 0STAT touch_misses 0STAT auth_cmds 0STAT auth_errors 0STAT bytes_read 15785                      读取字节总数STAT bytes_written 15222                   写入字节总数STAT limit_maxbytes 67108864               分配的内存数(字节)STAT accepting_conns 1                     目前接受的链接数STAT listen_disabled_num 0                STAT time_in_listen_disabled_us 0STAT threads 4                             线程数STAT conn_yields 0STAT hash_power_level 16STAT hash_bytes 524288STAT hash_is_expanding 0STAT malloc_fails 0STAT conn_yields 0STAT bytes 0                               存储item字节数STAT curr_items 0                          item个数STAT total_items 34                        item总数STAT expired_unfetched 0STAT evicted_unfetched 0STAT evictions 0                           为获取空间删除item的总数STAT reclaimed 0STAT crawler_reclaimed 0STAT crawler_items_checked 0STAT lrutail_reflocked 0

5,清除命令

flush_all 将缓存重置到干净的状态

6,其他

1,append 追加到缓存数据之后,跟着

set id 0 0 41234STOREDget idVALUE id 0 41234ENDappend id 0 0 2abSTOREDget idVALUE id 0 61234abEND

2,prepend 将数据追加到当前缓存数据的之前,当缓存数据存在时才存储

prepend id 0 0 5      poiuySTOREDget idVALUE id 0 11poiuy1234abEND

PHP操作memcache的简单demo

<?php    header('content-type:text/html;charset=utf-8');    if (!class_exists('Memcached')) {        echo 'PHP Memcached extension was not installed';        exit;    }    $mem = new Memcached();    $mem->addServer("127.0.0.1", 11211) or die ("Could not connect");    //显示版本    print_r($mem->getVersion());    $mem->set('key1', 'This is first value', 60);    $val = $mem->get('key1');    echo "Get key1 value: " . $val ."<br />";    $mem->replace('key1', 'This is replace value', 60);    $val = $mem->get('key1');    echo "Get key1 value: " . $val . "<br />";    $arr = array('aaa', 'bbb', 'ccc', 'ddd');    $mem->set('key2', $arr, 60);    $val2 = $mem->get('key2');    echo "Get key2 value: ";    print_r($val2);    echo "<br />";    echo "<pre>";    print_r($mem->getStats());    $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 />";?>
0 0