PHP 中使用 Memcached 作缓存(3)- 使用 memcached扩展工作

上一篇 PHP 中使用 Memcached 作缓存(2)- 使用 memcache扩展工作 介绍了使用 memcache扩展,这一篇我们介绍 使用 memcached扩展。

memcache 和 memcached 扩展有以下一些区别:

 

pecl/memcache pecl/memcached
首次发布时间 2004-06-08 2009-01-29 (beta)
是否仍在开发?
是否依赖其他的组件 不依赖 libmemcached
特性
Automatic Key Fixup1 支持 不支持
Append/Prepend 不支持 支持
自动序列化2 支持 支持
二进制协议 不支持 可选
CAS 不支持 支持
压缩 支持 支持
通讯超时控制 仅连接时 更多选择
一致性哈希 支持 支持
延迟获取 不支持 支持
一次获取多个
会话支持 支持 支持
在制定的服务器上存取 不支持 支持
存储数字 转换为字符串 支持

注:此表来自于 http://code.google.com/p/memcached/wiki/PHPClientComparison

memcached 扩展只提供了面向对象的 API,类名为 Memcached,以下是方法介绍:
Memcached::__construct :构造函数,可以在参数总指定长连接 persistent_id,默认在请求结束时就释放了连接

服务器信息维护:
Memcached::addServer          :向服务器池中添加一个服务器
Memcached::addServers         :向服务器池中添加多个服务器
Memcached::getServerList      :获取服务器中池中的服务器列表
Memcached::getServerByKey     :将一个键和台服务器对应
Memcached::getResultCode      :返回上次操作的结果状态
Memcached::getResultMessage   :返回上次操作的结果状态的描述信息
Memcached::getStats           :获取服务器池的统计信息
Memcached::getVersion         :获取服务器池的版本信息
Memcached::setOption          :设置 Memcached服务器的选项
Memcached::getOption          :获取 Memcached服务器的选项

数据维护:
Memcached::add                :新添加一对键值
Memcached::set                :新添加或者修改指定键的值
Memcached::setMulti           :新添加或者修改多个指定键的值
Memcached::replace            :修改指定键的值
Memcached::append             :向已存储的值的结尾追加内容
Memcached::prepend            :向已存储的值的开头追加内容
Memcached::delete             :删除指定键的值
Memcached::getDelayed         :请求多个结果,但不等待请求返回,一般和 fetch、fetchAll 结合使用
Memcached::fetch              :获取最后一次请求的下一个结果
Memcached::fetchAll           :获取最后一次请求的剩余所有的结果
Memcached::flush              :将缓存中的所有内容值为无效
Memcached::get                :获取指定键的值
Memcached::getMulti           :获取多个指定键的值
Memcached::increment          :增加一个数字类型的值
Memcached::decrement          :减小一个数字类型的值
Memcached::cas                :仅在当前客户端最后一次获取数据后没有其它客户端更新此数据的情况下设置为新的值

针对特定服务器的数据维护:
Memcached::addByKey
Memcached::setByKey
Memcached::setMultiByKey
Memcached::replaceByKey
Memcached::appendByKey
Memcached::prependByKey
Memcached::deleteByKey
Memcached::getByKey
Memcached::getDelayedByKey
Memcached::getMultiByKey
Memcached::casByKey

下面我们举例说明以下:

<?php

$oMemCached = new \Memcached();
$oMemCached->addServer('127.0.0.1', 11211, 10);

print_r($oMemCached->getVersion());

echo "\n[Add one,two]\n";
$oMemCached->add('one', '1111111111');
$oMemCached->add('two', '2222222222');
echo 'one    : ', $oMemCached->get('one'), "\n";
echo 'two    : ', $oMemCached->get('two'), "\n";

echo "\n[Replace two]\n";
$oMemCached->replace('two', 'Two Two');
echo 'two    : ', $oMemCached->get('two'), "\n";

echo "\n[Set three]\n";
$oMemCached->set('three', '3333333333');
echo 'three  : ', $oMemCached->get('three'), "\n";

echo "\n[Set four]\n";
$oMemCached->set('four', '44444444');
echo 'four   : ', $oMemCached->get('four'), "\n";

echo "\n[Delete three]\n";
$oMemCached->delete('three');
echo 'one    : ', $oMemCached->get('one'), "\n";
echo 'two    : ', $oMemCached->get('two'), "\n";
echo 'three  : ', $oMemCached->get('three'), "\n";
echo 'four   : ', $oMemCached->get('four'), "\n";

$oMemCached->add('zero', '000000000000000');
echo "\n[fetch one,zero]\n";
$oMemCached->getDelayed(array('one', 'zero'));
print_r($oMemCached->fetch());
print_r($oMemCached->fetch());

echo "\n[fetchAll two,four]\n";
$oMemCached->getDelayed(array('two', 'four'));
print_r($oMemCached->fetchAll());

$oMemCached->setOption(\MemCached::OPT_COMPRESSION, false);
$oMemCached->set('five', ' [5] ');
echo 'five   : ', $oMemCached->get('five'), "\n";

echo "\n[Append five]\n";
$oMemCached->append('five', 'Four Four');
echo 'five   : ', $oMemCached->get('five'), "\n";

echo "\n[Prepend five]\n";
$oMemCached->prepend('five', '<<<<<<');
echo 'five   : ', $oMemCached->get('five'), "\n";

$oMemCached->set('num', 10);
echo "\n", 'num   : ', $oMemCached->get('num'), "\n";
echo "\n[Increment num+5]\n";
$oMemCached->increment('num', 5);
echo 'num   : ', $oMemCached->get('num'), "\n";

echo "\n[Decrement num-6]\n";
$oMemCached->decrement('num', 6);
echo 'num   : ', $oMemCached->get('num'), "\n";


echo "\n[statistic]\n";
print_r($oMemCached->getStats());

echo "\n[Flush]\n";
$oMemCached->flush();
echo 'one    : ', $oMemCached->get('one'), "\n";
echo 'two    : ', $oMemCached->get('two'), "\n";
echo 'three  : ', $oMemCached->get('three'), "\n";
echo 'four   : ', $oMemCached->get('four'), "\n";

输出:

Array
(
    [127.0.0.1:11211] => 1.4.14
)

[Add one,two]
one    : 1111111111
two    : 2222222222

[Replace two]
two    : Two Two

[Set three]
three  : 3333333333

[Set four]
four   : 44444444

[Delete three]
one    : 1111111111
two    : Two Two
three  : 
four   : 44444444

[fetch one,zero]
Array
(
    [key] => one
    [value] => 1111111111
)
Array
(
    [key] => zero
    [value] => 000000000000000
)

[fetchAll two,four]
Array
(
    [0] => Array
        (
            [key] => two
            [value] => Two Two
        )

    [1] => Array
        (
            [key] => four
            [value] => 44444444
        )

)
five   :  [5] 

[Append five]
five   :  [5] Four Four

[Prepend five]
five   : <<<<<< [5] Four Four

num   : 10

[Increment num+5]
num   : 15

[Decrement num-6]
num   : 9

[statistic]
Array
(
    [127.0.0.1:11211] => Array
        (
            [pid] => 21070
            [uptime] => 25445
            [threads] => 4
            [time] => 1345804418
            [pointer_size] => 32
            [rusage_user_seconds] => 0
            [rusage_user_microseconds] => 503923
            [rusage_system_seconds] => 0
            [rusage_system_microseconds] => 584911
            [curr_items] => 7
            [total_items] => 226
            [limit_maxbytes] => 67108864
            [curr_connections] => 5
            [total_connections] => 65
            [connection_structures] => 7
            [bytes] => 492
            [cmd_get] => 509
            [cmd_set] => 223
            [get_hits] => 363
            [get_misses] => 146
            [evictions] => 0
            [bytes_read] => 13278
            [bytes_written] => 16438
            [version] => 1.4.14
        )

)

[Flush]
one    : 
two    : 
three  : 
four   : 

Post a Comment

Your email is never shared. Required fields are marked *

*
*