php memcached方法有:1、set();1、add();3、replace();4、get();5、delete();6、increment();7、decrement();8、flush();9、connect()等等。
本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑
PHP操作Memcached的方法汇总
(一)memcache扩展
1、boolMemcache::set( string$key
, mixed$var
[, int$flag
[, int$expire
]] )
#Key存在则更新值,不存在则设置k-v对。注:$var可以存储任何数据
2、boolMemcache::add( string$key
, mixed$var
[, int$flag
[, int$expire
]] )
#key不存在的时候才添加
3、boolMemcache::replace( string$key
, mixed$var
[, int$flag
[, int$expire
]] )
#替换存在的key值,不存在key则返回错误
4、stringMemcache::get( string$key
[, int&$flags
] )
arrayMemcache::get( array$keys
[, array&$flags
] )
#获取一个或者多个值
5、boolMemcache::delete( string$key
[, int$timeout
= 0 ] )
#删除key元素,设置了timeout则多少秒后删除
#【注意】有些版本对应memcached使用timeout将会导致删除失败(0可以)
6、intMemcache::increment( string$key
[, int$value
= 1 ] )
#key存在且能转换为数字,则加int;否则直接更换为value。当key不存在,则返回false
7、intMemcache::decrement( string$key
[, int$value
= 1 ] )
8、boolMemcache::flush( void )
#全部元素失效
9、boolMemcache::connect( string$host
[, int$port
[, int$timeout=1
]] )
#连接memcache服务器,执行完脚本后会自动关闭(使用close可以主动关闭)
10、boolMemcache::close( void )
#关闭memcache的链接(这个函数不会关闭持久化连接)
11、mixedMemcache::pconnect( string$host
[, int$port
[, int$timeout
]] )
#建立持久化连接
12、boolMemcache::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、arrayMemcache::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、intMemcache::getServerStatus( string$host
[, int$port
= 11211 ] )
#返回一个服务器的状态,0表示服务器离线,非0表示在线。
15、arrayMemcache::getStats([ string$type
[, int$slabid
[, int$limit
= 100 ]]] )
#getStats()返回一个关联数据的服务器统计信息。同上
16、stringMemcache::getVersion( void )
#返回版本号
17、boolMemcache::setCompressThreshold( int$threshold
[, float$min_savings
] )
#开启对于大值的自动压缩
参数:
#threshold 控制多大值进行自动压缩的阈值。
#min_saving 指定经过压缩实际存储的值的压缩率,支持的值必须在0和1之间。默认值是0.2表示20%压缩率
18、boolMemcache::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
值创建的实例共享同一个连接。
2、public boolMemcached::addServer( string$host
, int$port
[, int$weight
= 0 ] )
#增加指定服务器到服务器池中,此时不会建立与服务端的连接
3、public boolMemcached::addServers( array$servers
)
#添加多台服务器到服务池中
4、public boolMemcached::cas( float$cas_token
, string$key
, mixed$value
[, int$expiration
] )
#执行一个"检查并设置"的操作,它仅在当前客户端最后一次取值后,该key 对应的值没有被其他客户端修改的情况下, 才能够将值写入。通过cas_token
参数进行检查
5、public boolMemcached::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 boolMemcached::set( string$key
, mixed$value
[, int$expiration
] )
#将value值(值可以是任何有效的非资源型php类型)存到key下
7、public boolMemcached::setByKey( string$server_key
, string$key
, mixed$value
[, int$expiration
] )
#指定服务器,同上
8、public boolMemcached::setMulti( array$items
[, int$expiration
] )
#存储多个元素
#$items array(‘key’=>’value’)
9、public boolMemcached::setMultiByKey( string$server_key
, array$items
[, int$expiration
] )
#指定服务器,同上
10、public boolMemcached::add( string$key
, mixed$value
[, int$expiration
] )
#向一个新的key下面增加一个元素,key存在则失败
11、public boolMemcached::addByKey( string$server_key
, string$key
, mixed$value
[, int$expiration
] )
#在指定服务器上的一个新的key下增加一个元素
12、public boolMemcached::touch( string$key
, int$expiration
)
#为key设置新的过期时间
13、public boolMemcached::touchByKey( string$server_key
, string$key
, int$expiration
)
#为指定服务器中的key设置过期时间
14、public boolMemcached::append( string$key
, string$value
)
#向已经存在的元素后追加value
参数对应的字符串值
注意:如果Memcached::OPT_COMPRESSION常量开启,这个操作会失败,并引发一个警告,因为向压缩数据后追加数据可能会导致解压不了。
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 ""; print_r($b); echo "";die; ?>Salin selepas log masuk
15、public boolMemcached::appendByKey( string$server_key
, string$key
, string$value
)
#向指定服务器已经存在的元素后追加value
参数对应的字符串值
16、public boolMemcached::prepend( string$key
, string$value
)
#向一个已存在的元素前面追加数据
17、public boolMemcached::prependByKey( string$server_key
, string$key
, string$value
)
#向指定服务器已经存在的元素前追加value
参数对应的字符串值
18、public boolMemcached::replace( string$key
, mixed$value
[, int$expiration
] )
#替换已存在key下的元素
19、public boolMemcached::replaceByKey( string$server_key
, string$key
, mixed$value
[, int$expiration
] )
#替换指定服务器的key下的元素
20、public intMemcached::decrement( string$key
[, int$offset
= 1 ] )
#减小数值元素的值
#不存在key返回错误、减到小于0结果为0、元素不是数值以0对待
21、public intMemcached::decrementByKey( string$server_key
, string$key
[, int$offset
= 1 [, int$initial_value
= 0 [, int$expiry
= 0 ]]] )
#指定服务器减小数值元素的值,不存在的key则初始化为0
22、public intMemcached::increment( string$key
[, int$offset
= 1 ] )
#增加数值元素的值
23、public intMemcached::incrementByKey( string$server_key
, string$key
[, int$offset
= 1 [, int$initial_value
= 0 [, int$expiry
= 0 ]]] )
#同上
24、public boolMemcached::delete( string$key
[, int$time
= 0 ] )
#删除一个元素
#设置时间后,表明在time时间后才删除,在这段时间内get、add、replace命令对该key都无效。
25、public boolMemcached::deleteByKey( string$server_key
, string$key
[, int$time
= 0 ] )
#同上
26、public boolMemcached::deleteMulti( array$keys
[, int$time
= 0 ] )
#删除多个key
27、public boolMemcached::deleteMultiByKey( string$server_key
, array$keys
[, int$time
= 0 ] )
#同上
28、public boolMemcached::flush([ int$delay
= 0 ] )
#让所有缓冲区的数据失效
29、public mixedMemcached::get( string$key
[, callback$cache_cb
[, float&$cas_token
]] )
#检索一个元素
#$callback 回调函数,没有$key之值时,将会调用这个函数,会传入三个参数memcache对象、key、引用传递变量的返回值(true时返回)
#$cas_token 配合cas使用。同一个客户端最后一个get将会生成一个64位唯一标识符存储,然后使用cas来查看更改,假若在此过程中被其他客户端修改则,返回false
30、public mixedMemcached::getByKey( string$server_key
, string$key
[, callback$cache_cb
[, float&$cas_token
]] )
#从特定的服务器检索元素
31、public mixedMemcached::getMulti( array$keys
[, array&$cas_tokens
[, int$flags
]] )
#检索多个元素,提供$cas值,则添加cas值
#$flags 只能为Memcached::GET_PRESERVE_ORDER,保证返回的key的顺序和请求时一致。
32、public arrayMemcached::getMultiByKey( string$server_key
, array$keys
[, string&$cas_tokens
[, int$flags
]] )
#从特定服务器检索多个元素
33、public arrayMemcached::getAllKeys( void )
# Gets the keys stored on all the servers
34、public boolMemcached::getDelayed( array$keys
[, bool$with_cas
[, callback$value_cb
]] )
#向服务器端请求keys,这个方法不会等待响应而是立即返回bool,收集结果使用fetch、fetchAll
#$with_cas true时,则表示同时记录cas值
#$value_cb
结果回调函数处理
35、public boolMemcached::getDelayedByKey( string$server_key
, array$keys
[, bool$with_cas
[, callback$value_cb
]] )
#从指定服务器中请求多个keys
36、public arrayMemcached::fetch( void )
#从最后一次请求中抓取下一个结果。
37、public arrayMemcached::fetchAll( void )
#抓取所有剩余的结果
38、public mixedMemcached::getOption( int$option
)
#获取Memcached的选项值
# OPT_*系列常量中的一个。
39、public boolMemcached::setOption( int$option
, mixed$value
)
#设置一个memcached选项
40、public boolMemcached::setOptions( array$options
)
#设置多个memcached选项
41、public intMemcached::getResultCode( void )
#返回最后一次操作的结果代码
42、public stringMemcached::getResultMessage( void )
#返回最后一次操作的结果描述消息
43、public arrayMemcached::getServerByKey( string$server_key
)
#获取key所映射的服务器信息
44、public arrayMemcached::getServerList( void )
#获取服务器池中服务器表
45、public arrayMemcached::getStats( void )
#获取服务器池中的统计信息
46、public arrayMemcached::getVersion( void )
#获取服务器池中所有服务器版本信息
47、public boolMemcached::isPersistent( void )
#测试服务器是否永久连接
48、public boolMemcached::isPristine( void )
#测试memcache是否最近创建的
49、public boolMemcached::quit( void )
#关闭连接
50、public boolMemcached::resetServerList( void )
#重置所有服务器的服务器服务信息
51、public voidMemcached::setSaslAuthData( string$username
, string$password
)
#Set the credentials to use for authentication
(以上是自己在参考手册学习memcached的时候,整理的笔记,顺便也将它贴出来吧,若有不足或者错误的地方请各位指出哈)
推荐学习:《PHP视频教程》
Atas ialah kandungan terperinci php memcached方法有哪些. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!