PHP操作Memcached的方法汇总

 

memcached非关系型数据库安装、php中的memcache的扩展安装、以及php中的memcached的扩展安装可以参考:

http://www.cnblogs.com/phpstudy2015-6/p/6670103.html

(一)memcache扩展

1、bool Memcache::set ( string $key , mixed $var [, int $flag [, int $expire ]] )

#Key存在则更新值,不存在则设置k-v对。注:$var可以存储任何数据

2、bool Memcache::add ( string $key , mixed $var [, int $flag [, int $expire ]] )

#key不存在的时候才添加

3、bool Memcache::replace ( string $key , mixed $var [, int $flag [, int $expire ]] )

#替换存在的key值,不存在key则返回错误

4、string Memcache::get ( string $key [, int &$flags ] )

array Memcache::get ( array $keys [, array &$flags ] )

#获取一个或者多个值

5、bool Memcache::delete ( string $key [, int $timeout = 0 ] )

#删除key元素,设置了timeout则多少秒后删除

#【注意】有些版本对应memcached使用timeout将会导致删除失败(0可以)

6、int Memcache::increment ( string $key [, int $value = 1 ] )

#key存在且能转换为数字,则加int;否则直接更换为value。当key不存在,则返回false

7、int Memcache::decrement ( string $key [, int $value = 1 ] )

8、bool Memcache::flush ( void )

#全部元素失效

9、bool Memcache::connect ( string $host [, int $port [, int $timeout=1 ]] )

#连接memcache服务器,执行完脚本后会自动关闭(使用close可以主动关闭)

10、bool Memcache::close ( void )

#关闭memcache的链接(这个函数不会关闭持久化连接)

11、mixed Memcache::pconnect ( string $host [, int $port [, int $timeout ]] )

#建立持久化连接

12、bool Memcache::addServer ( string $host [, int $port = 11211 [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback [, int $timeoutms ]]]]]]]] )

#增加一台服务器到连接池,通过此方法打开的服务,将会在脚本结束的时候关闭或者主动关闭close

#使用此方法,网络连接不一定立即连接,而是等需要使用此服务器的时候,才会进行连接,因此即使添加大量的服务器到连接池也没有开销

参数:

$persistent   是否持久化,默认true

$weight   表示权重

$retry_interval   服务器连接失败时重试时间,默认为15秒,-1表示不重试

$status   控制此服务器是否被标记为在线状态(假若连接失败,连接池少了一个服务器,会影响原有的分配算法)

$failure_callback   连接失败后执行的函数(在故障转移前执行),包含两个参数,失败主机host和port

13、array Memcache::getExtendedStats ([ string $type [, int $slabid [, int $limit = 100 ]]] )

#getExtendedStats()返回一个二维关联数据的服务器统计信息

#getExtendedStats(‘slabs’)获取到每个服务器上活动slabs分块的id

#getExtendedStats('cachedump', $slabid, $limit)获取每个slab里面缓存的项

参数:

#type   期望抓取的统计信息类型,可以使用的值有{reset, malloc, maps, cachedump, slabs, items, sizes}

#slabid   用于与参数type联合从指定slab分块拷贝数据,cachedump命令会完全占用服务器通常用于 比较严格的调试。

#limit   用于和参数type联合来设置cachedump时从服务端获取的实体条数。

14、int Memcache::getServerStatus ( string $host [, int $port = 11211 ] )

#返回一个服务器的状态,0表示服务器离线,非0表示在线。

15、array Memcache::getStats ([ string $type [, int $slabid [, int $limit = 100 ]]] )

#getStats()返回一个关联数据的服务器统计信息。同上

16、string Memcache::getVersion ( void )

#返回版本号

17、bool Memcache::setCompressThreshold ( int $threshold [, float $min_savings ] )

#开启对于大值的自动压缩

参数:

#threshold   控制多大值进行自动压缩的阈值。

#min_saving   指定经过压缩实际存储的值的压缩率,支持的值必须在0和1之间。默认值是0.2表示20%压缩率

18、bool Memcache::setServerParams ( string $host [, int $port = 11211 [, int $timeout [, int $retry_interval = false [, bool $status [, callback $failure_callback ]]]]] )

#用于运行时修改服务器参数

#参数同上

 

(二)memcached扩展

1、Memcached::__construct ([ string $persistent_id ] )

#默认情况下,Memcached实例在请求结束后会被销毁。但可以在创建时通过persistent_id为每个实例指定唯一的ID,在请求间共享实例。所有通过相同的persistent_id值创建的实例共享同一个连接。

<?php
# 创建一个普通的对象
$m1 = new Memcached();
echo get_class($m);

/* 创建持久化对象 */
$m2 = new Memcached('story_pool');
$m3 = new Memcached('story_pool');

# 现在$m2和$m3共享相同的连接 ,可以使用isPresistent进行检测
?>

2、public bool Memcached::addServer ( string $host , int $port [, int $weight = 0 ] )

#增加指定服务器到服务器池中,此时不会建立与服务端的连接

3、public bool Memcached::addServers ( array $servers )

#添加多台服务器到服务池中

4、public bool Memcached::cas ( float $cas_token , string $key , mixed $value [, int $expiration ] )

#执行一个"检查并设置"的操作,它仅在当前客户端最后一次取值后,该key 对应的值没有被其他客户端修改的情况下, 才能够将值写入。通过cas_token参数进行检查

5、public bool Memcached::casByKey ( float $cas_token , string $server_key , string $key , mixed $value [, int $expiration ] )

#指定服务器,同上

#【$server_key也是一个普通的key, *ByKey系列接口的工作过程是: 首先, 对$server_key进行hash, 得到$server_key应该存储的服务器, 然后将相应的操作在 $server_key所在的服务器上进行】

6、public bool Memcached::set ( string $key , mixed $value [, int $expiration ] )

#将value值(值可以是任何有效的非资源型php类型)存到key下

7、public bool Memcached::setByKey ( string $server_key , string $key , mixed $value [, int $expiration ] )

#指定服务器,同上

8、public bool Memcached::setMulti ( array $items [, int $expiration ] )

#存储多个元素

#$items     array(‘key’=>’value’)

9、public bool Memcached::setMultiByKey ( string $server_key , array $items [, int $expiration ] )

#指定服务器,同上

10、public bool Memcached::add ( string $key , mixed $value [, int $expiration ] )

#向一个新的key下面增加一个元素,key存在则失败

11、public bool Memcached::addByKey ( string $server_key , string $key , mixed $value [, int $expiration ] )

#在指定服务器上的一个新的key下增加一个元素

12、public bool Memcached::touch ( string $key , int $expiration )

#为key设置新的过期时间

13、public bool Memcached::touchByKey ( string $server_key , string $key , int $expiration )

#为指定服务器中的key设置过期时间

14、public bool Memcached::append ( string $key , string $value )

#向已经存在的元素后追加value参数对应的字符串值

注意:如果Memcached::OPT_COMPRESSION常量开启,这个操作会失败,并引发一个警告,因为向压缩数据后追加数据可能会导致解压不了。

<?php

$a = new Memcached();

$a->addServer('192.168.95.11', 11211);

#$a->addServer('192.168.95.11', 11210);

#$a->setOption(Memcached::OPT_COMPRESSION, false);

$b=$a->append('e','popop');

echo "<pre>";

print_r($b);

echo "</pre>";die;

?>

15、public bool Memcached::appendByKey ( string $server_key , string $key , string $value )

#向指定服务器已经存在的元素后追加value参数对应的字符串值

16、public bool Memcached::prepend ( string $key , string $value )

#向一个已存在的元素前面追加数据

17、public bool Memcached::prependByKey ( string $server_key , string $key , string $value )

#向指定服务器已经存在的元素前追加value参数对应的字符串值

18、public bool Memcached::replace ( string $key , mixed $value [, int $expiration ] )

#替换已存在key下的元素

19、public bool Memcached::replaceByKey ( string $server_key , string $key , mixed $value [, int $expiration ] )

#替换指定服务器的key下的元素

20、public int Memcached::decrement ( string $key [, int $offset = 1 ] )

#减小数值元素的值

#不存在key返回错误、减到小于0结果为0、元素不是数值以0对待

21、public int Memcached::decrementByKey ( string $server_key , string $key [, int $offset = 1 [, int $initial_value = 0 [, int $expiry = 0 ]]] )

#指定服务器减小数值元素的值,不存在的key则初始化为0

22、public int Memcached::increment ( string $key [, int $offset = 1 ] )

#增加数值元素的值

23、public int Memcached::incrementByKey ( string $server_key , string $key [, int $offset = 1 [, int $initial_value = 0 [, int $expiry = 0 ]]] )

#同上

24、public bool Memcached::delete ( string $key [, int $time = 0 ] )

#删除一个元素

#设置时间后,表明在time时间后才删除,在这段时间内get、add、replace命令对该key都无效。

25、public bool Memcached::deleteByKey ( string $server_key , string $key [, int $time = 0 ] )

#同上

26、public bool Memcached::deleteMulti ( array $keys [, int $time = 0 ] )

#删除多个key

27、public bool Memcached::deleteMultiByKey ( string $server_key , array $keys [, int $time = 0 ] )

#同上

28、public bool Memcached::flush ([ int $delay = 0 ] )

#让所有缓冲区的数据失效

29、public mixed Memcached::get ( string $key [, callback $cache_cb [, float &$cas_token ]] )

#检索一个元素

#$callback     回调函数,没有$key之值时,将会调用这个函数,会传入三个参数memcache对象、key、引用传递变量的返回值(true时返回)

#$cas_token     配合cas使用。同一个客户端最后一个get将会生成一个64位唯一标识符存储,然后使用cas来查看更改,假若在此过程中被其他客户端修改则,返回false

30、public mixed Memcached::getByKey ( string $server_key , string $key [, callback $cache_cb [, float &$cas_token ]] )

#从特定的服务器检索元素

31、public mixed Memcached::getMulti ( array $keys [, array &$cas_tokens [, int $flags ]] )

#检索多个元素,提供$cas值,则添加cas值

#$flags     只能为Memcached::GET_PRESERVE_ORDER,保证返回的key的顺序和请求时一致。

32、public array Memcached::getMultiByKey ( string $server_key , array $keys [, string &$cas_tokens [, int $flags ]] )

#从特定服务器检索多个元素

33、public array Memcached::getAllKeys ( void )

# Gets the keys stored on all the servers

34、public bool Memcached::getDelayed ( array $keys [, bool $with_cas [, callback $value_cb ]] )

#向服务器端请求keys,这个方法不会等待响应而是立即返回bool,收集结果使用fetch、fetchAll

#$with_cas     true时,则表示同时记录cas值

#$value_cb     结果回调函数处理

35、public bool Memcached::getDelayedByKey ( string $server_key , array $keys [, bool $with_cas [, callback $value_cb ]] )

#从指定服务器中请求多个keys

36、public array Memcached::fetch ( void )

#从最后一次请求中抓取下一个结果。

37、public array Memcached::fetchAll ( void )

#抓取所有剩余的结果

38、public mixed Memcached::getOption ( int $option )

#获取Memcached的选项值

# OPT_*系列常量中的一个。

39、public bool Memcached::setOption ( int $option , mixed $value )

#设置一个memcached选项

40、public bool Memcached::setOptions ( array $options )

#设置多个memcached选项

41、public int Memcached::getResultCode ( void )

#返回最后一次操作的结果代码

42、public string Memcached::getResultMessage ( void )

#返回最后一次操作的结果描述消息

43、public array Memcached::getServerByKey ( string $server_key )

#获取key所映射的服务器信息

44、public array Memcached::getServerList ( void )

#获取服务器池中服务器表

45、public array Memcached::getStats ( void )

#获取服务器池中的统计信息

46、public array Memcached::getVersion ( void )

#获取服务器池中所有服务器版本信息

47、public bool Memcached::isPersistent ( void )

#测试服务器是否永久连接

48、public bool Memcached::isPristine ( void )

#测试memcache是否最近创建的

49、public bool Memcached::quit ( void )

#关闭连接

50、public bool Memcached::resetServerList ( void )

#重置所有服务器的服务器服务信息

51、public void Memcached::setSaslAuthData ( string $username , string $password )

#Set the credentials to use for authentication

 

(以上是自己在参考手册学习memcached的时候,整理的笔记,顺便也将它贴出来吧,若有不足或者错误的地方请各位指出哈)

作者:那一叶随风

 

posted @ 2017-04-06 09:23  那一叶随风  阅读(2646)  评论(0编辑  收藏  举报