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的时候,整理的笔记,顺便也将它贴出来吧,若有不足或者错误的地方请各位指出哈)
作者:那一叶随风
PHP操作Memcached的方法汇总的更多相关文章
- linux的自动化操作相关使用方法汇总(转)
linux系统的web网站在运营状态时,我们常需要对网站进行维护,例如查看资源剩余并做出响应.日志分割.数据整理,在特定状态执行特定任务等等,这些都会需要linux能实现自动执行某些任任务.本篇博文介 ...
- linux的自动化操作相关使用方法汇总 专题
Crontab中的除号(slash)到底怎么用? crontab 是Linux中配置定时任务的工具,在各种配置中,我们经常会看到除号(Slash)的使用,那么这个除号到底标示什么意思,使用中有哪些需要 ...
- PHP操作Memcached
一.PHP连接Memcached: 一个简单的使用示例: $memcache = new Memcache; $memcache->connect("127.0.0.1",1 ...
- .net(C#)操作文件的几种方法汇总
.net(C#)操作文件的几种方法汇总 System.IO命名空间下类的用法:在System.IO名称空间中包含了用于文件输入输出的主要类.File:实用类,提供许多静态方法,用于移动.复制和删除文件 ...
- js如何操作表格(常用属性方法汇总)
js如何操作表格(常用属性方法汇总) 一.总结 一句话总结: 二.表格相关的属性和方法 1.1 Table 对象集合 cells[] 返回包含表格中所有单元格的一个数组. 语法:tableObject ...
- 将编码从GB2312转成UTF-8的方法汇总(从前台、程序、数据库)
这篇文章主要介绍了将编码从GB2312转成UTF-8的方法汇总(从前台.程序.数据库),需要的朋友可以参考下 一个网站如果需要国际化,就需要将编码从GB2312转成UTF-8,其中有很多的问题需要注意 ...
- Python操作memcached及redis
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
- Javascript对象属性与方法汇总
Javascript对象属性与方法汇总 发布时间:2015-03-06 编辑:www.jquerycn.cn 详细介绍下,javascript对象属性与对象方法的相关知识,包括javascript字符 ...
- asp.net中导出excel数据的方法汇总
1.由dataset生成 代码如下 复制代码 public void CreateExcel(DataSet ds,string typeid,string FileName) { Htt ...
随机推荐
- mongodb数据库安装
mongodb的安装 1,下载安装包: http://www.runoob.com/mongodb/mongodb-window-install.html 2,安装至: D:\MongoDB,将 ...
- SQLAlchemy框架用法详解
介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DBAPI之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQL,然后使用数据API执行SQL并获取执 ...
- 51 Nod 1027 大数乘法【Java大数乱搞】
1027 大数乘法 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出2个大整数A,B,计算A*B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 ...
- HDU6038-Function-数学+思维-2017多校Team01
学长讲座讲过的,代码也讲过了,然而,当时上课没来听,听代码的时候也一脸o((⊙﹏⊙))o 我的妈呀,语文不好是硬伤,看题意看了好久好久好久(死一死)... 数学+思维题,代码懂了,也能写出来,但是还是 ...
- 北京师范大学校赛C
https://www.nowcoder.com/acm/contest/submit/0dff89ad7b8444719df155d507f3e1dd?ACMContestId=3&tagI ...
- Quoit Design(最近点对+分治)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...
- 如何使用padlepadle 进行意图识别-开篇
前言 意图识别是通过分类的办法将句子或者我们常说的query分到相应的意图种类.举一个简单的例子,我想听周杰伦的歌,这个query的意图便是属于音乐意图,我想听郭德纲的相声便是属于电台意图.做好了意图 ...
- SLAM入门之视觉里程计(6):相机标定 张正友经典标定法详解
想要从二维图像中获取到场景的三维信息,相机的内参数是必须的,在SLAM中,相机通常是提前标定好的.张正友于1998年在论文:"A Flexible New Technique fro Cam ...
- FtpUtil.java测试 (淘淘商城第三课文件上传)
首先在common-taotao中创建一个utils包,复制FtpUtil.java到其中.然后如下: @Test public void testFtpUtil() throws Exception ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...