Perl Memcached模块-Cache::Memcached::Fast
Cache::Memcached::Fast是Perl环境下目前仍在更新的Memcached操作模块,其核心由C语言写成,尽量减少系统调用的数量,避免任何键/值复制以提高速度。因此它的CPU消耗非常低,其次API的使用上尽量保持了与Cache::Memcached这个纯perl实现模块的相似性。-------------------------------
构建对象
new方法
my $memd = new Cache::Memcached::Fast($params);
这里将会创建一个新的客户对象,$params将会做为一个引用参数的形式传入,可有如下的键值对:
servers
servers => [ { address => 'localhost:11211', weight => 2.5 },
'192.168.254.2:11211',
{ address => '/path/to/unix.sock', noreply => 1 } ],
这里给出了servers这个引用的数组的一些写法,可以直接写ip:port的方式;还可以构建一个匿名hash,在其中以address指定地址信息(除了前面的ip:port的网络连接方式外,还可以是本地的socket)。
权重(weight)值,不指定即为1,将会以权重值来分配键值对在各个服务实例上的分布情况;另外要注意weight必须为正数且所有值加起来不能超过32768。
是否开启noreply属性,启用noreply后,在void上下文中执行的命令将指示服务器不发送回复。与下面的"nowait"比较。memcached从1.2.5版本开始实现noreply。如果为早期的服务器版本启用noreply,则会出现错误,客户端最终会阻塞。要小心使用。
namespace
namespace => 'my::'
(default: '')
该值是一个标量,将在传递给memcached服务器的所有键名前面加上该标量以区分不同的应用,默认为空。通过使用不同的名称空间,客户使用者之间可以避免相互干扰。
注:Redis以库的编号来做名字空间的隔离,它默认有16个库,默认的库号为0。
hash_namespace
hash_namespace => 1
(default: disabled)
该值是一个布尔值,用于启用(true)或禁用(false)命名空间键前缀的哈希。默认情况下,为了与Cache::Memcached模块兼容,命名空间前缀不会与键一起散列。因此:
namespace => 'prefix/',
...
$memd->set('key', $val);
可能使用不同的memcached服务器
namespace => '',
...
$memd->set('prefix/key', $val);
因为"key"和"prefix/key"的哈希值可能就是不同。
然而有时有必要对名称空间前缀进行hash运算,例如为了与其它没有名称空间概念的使用者互操作时。当hash_namespace被启用时,上面的两个例子将使用同一个服务器,即按"prefix/key"来映射。请注意,由于名称空间前缀只被散列一次,因此没有性能损失。请参阅"namespace"。
nowait
nowait => 1
(default: disabled)
该值是启用(true)或禁用(false)的布尔值。默认为禁用,如果启用,则在void上下文中调用仅返回其成功状态(如"set")的方法时,它会将请求发送到服务器并立即返回,而不是等待答复。这以不确定的命令结果为代价避免了往返延迟。
在内部有一个计数器,指示应该有多少未完成的回复,client在任何命令上读取并丢弃已经到达的回复。稍后在非void上下文中执行某个方法时,将等待所有未完成的答复,然后读取并返回此命令的回复情况。
connect_timeout
connect_timeout => 0.7
(default: 0.25 seconds)
该值是等待建立连接的非负有理秒数。仅适用于网络连接,为零时禁用超时,但请记住,操作系统有自己的启发式连接超时。
请注意,网络连接过程由几个步骤组成:目标主机地址查找,在一般情况下可能返回多个地址(特别是对于IPv6),然后尝试连接到其中一个地址。connect_timeout就适用于此类连接,即一次connect(2)调用。因此,整个连接过程可能需要比连接超时秒更长的时间,但这是不可避免的。
io_timeout (or deprecated select_timeout)
io_timeout => 0.5
(default: 1.0 seconds)
该值是在放弃与服务器通信之前等待的非负有理秒数。设定为零时表示禁止超时。
请注意,对于与多个服务器通信的命令(如"get_multi"),超时适用于每个服务器集,而不是每个服务器。因此,如果一台服务器通信速度足够快,即使其他服务器保持沉默,它也不会过期。但是如果一些服务器死了,那些活着的服务器将完成通信,然后死的服务器将超时。
close_on_error
close_on_error => 0
(default: enabled)
该值是一个布尔值,用于启用(true)或禁用(false)关闭错误模式。启用时,memcached服务器的任何错误响应都会使客户端关闭连接。注意,这种“错误响应”不同于“否定响应”。后者意味着服务器处理了命令并产生了负面结果。前者意味着服务器由于某种原因无法处理该命令。为安全起见,默认情况下会启用出错时关闭。
当连接断开或者客户端收到无法理解的回复时,它会关闭套接字,而不考虑close_on_error设置。
compress_threshold
compress_threshold => 10_000
(default: -1)
压缩阈值为整数。如果为正,则表示以字节为单位阈值大小,大于或等于此值的数据应进行压缩处理。请参阅下面的“压缩比(compress_ratio)”和“压缩方法(compress_methods)”。负值禁用压缩。
compress_ratio
compress_ratio => 0.9
(default: 0.8)
该值是介于0和1之间的小数。当"compress_threshold"触发压缩时,压缩大小应小于或等于(original-size * compress_ratio)。否则数据将以未压缩的方式存储。
compress_methods
compress_methods => [ \&IO::Compress::Gzip::gzip,\&IO::Uncompress::Gunzip::gunzip ]
(default: [ sub { ${$_[1]} = Compress::Zlib::memGzip(${$_[0]}) },sub { ${$_[1]} = Compress::Zlib::memGunzip(${$_[0]}) } ] when Compress::Zlib is available)
该值是对一个数组的引用,该数组分别包含压缩和解压缩例程的两个代码引用。
当传递给"set"方法族的$value的大小大于或等于"compress_threshold"时,将调用压缩例程(另请参阅"compress_ratio")。执行压缩的事实与数据一起被记住,并且在使用"get"方法族进行数据检索时调用解压缩例程。这些例程的接口应该与IO::Compress系列相同(例如,请参见IO::Compress::Gzip::Gzip和IO::Uncompress::Gunzip::Gunzip)。即压缩例程引用标量值和存储压缩结果的标量。解压例程使用压缩数据引用标量,并引用存储未压缩结果的标量。两个例程都应该在成功时返回true,在出错时返回false。
默认情况下,我们使用Compress::Zlib,因为在编写本文时,它似乎比IO::Uncompress::Gunzip快得多。
max_failures
max_failures => 3
(default: 0)
该值是非负整数。如果为正值,则如果在故障超时秒数内发生了故障,则客户端在另一个故障超时秒数(failure_timeout)内不会尝试连接到此特定服务器。该值为零时将禁用此行为。
failure_timeout
failure_timeout => 30
(default: 10 seconds)
该值是以秒为单位的正整数,请参阅"max_failures"。
ketama_points
ketama_points => 150
(default: 0)
该值是非负整数。如果为正值则启用Ketama一致哈希算法,并指定权重为1的服务器将映射到的点数。因此每个服务器将映射到连续统一体中的ketama_点*权重点。值越大,分布越均匀。请注意,内部bucket结构的数量以及内存消耗将与这些乘积的总和成正比。但是bucket结构本身很小(每个结构有两个整数),所以不必太担心。
设定为零值将禁用Ketama算法。另请参见上面"servers"中的服务器权重值。
serialize_methods
serialize_methods => [ \&Storable::freeze, \&Storable::thaw ],
(default: [ \&Storable::nfreeze, \&Storable::thaw ])
该值是对数组的引用,数组中分别包含两个用于序列化和反序列化例程的代码引用。
当传递给"set"方法族的$value是引用时,将调用序列化例程。序列化是与数据一起执行处理,反序列化例程在使用"get"方法族进行数据检索时被调用。这些例程的接口应该与Storable::nfreeze和Storable::thaw的接口相同。即序列化例程获取一个引用并返回一个标量字符串,它不应该失败。反序列化例程获取标量字符串并返回引用,如果反序列化失败(例如,错误的数据格式),它应该抛出一个异常(调用die)。异常将被模块捕获,然后在通过"get"方法查找对应的key时将不会被找到。
utf8
utf8 => 1
(default: disabled)
该值是一个布尔值,它在存储时启用(true)或禁用(false)utf8的支持功能,即将Perl字符串转换为UTF-8编码的八位字节序列,在获取时启用或禁用反向转换(当检索到的数据被标记为UTF-8八位字节序列时)。
max_size
max_size => 512 * 1024
(default: 1024 * 1024)
该值是要存储在memcached中的项的最大大小。当试图将一个键设置为一个大于max_size字节的值(在序列化和压缩之后)时,不会向服务器发送任何内容,set方法返回undef。
请注意,服务器上的实际最大值小于1MB,这取决于key长度等。所以在[1MB-N bytes,1MB]范围内的一些值,其中N是几百,仍然会被发送到服务器,并在那里被拒绝。可以将"max_size"设置为较小的值以避免出现这种情况。
check_args
check_args => 'skip'
(default: not 'skip')
值是字符串。当前唯一可识别的字符串是'skip'。默认情况下,所有构造函数参数名都被检查为可识别,并且对未知参数发出警告。这将捕获可能不被注意的拼写错误。设置为'skip'时,将绕过检查。当在不同的客户机版本之间或在不同的客户机之间共享相同的参数散列时,可能需要这样做。
-------------------------------
调用方法
enable_compress
$memd->enable_compress($enable);
布尔值$enable为true时启用压缩,为false时禁用压缩。
请注意,只有在将"compress_threshold"设置为某个正值并且设置了"compress_methods"时,才能启用压缩。
Return: none.
namespace
$memd->namespace;
$memd->namespace($string);
如果没有参数,则返回当前命名空间前缀。否则将命名空间前缀设置为$string,并返回旧前缀。
Return: scalar, the namespace prefix that was in effect before the call.
set
$memd->set($key, $value);
$memd->set($key, $value, $expiration_time);
将$value存储在服务器上$key下,该定义值可以是任何Perl数据类型。当它是引用时,引用的Perl数据结构将被使用"serialize_methods"指定的例程透明地序列化。可选的$expiration_time是一个正整数秒数,在该秒数之后,该值将过期并且不再可访问(过期被服务器删除)。
Return: boolean, true for positive server reply, false for negative server reply, or undef in case of some error.
set_multi
$memd->set_multi(
[$key, $value],
[$key, $value, $expiration_time],
...
);
与"set"类似,但操作多个键。获取对数组的引用列表,每个数组包含$key、$value和可选的$expiration_time。请注意,多命令不是全部执行成功,有些操作可能成功,而有些操作可能失败。
Return: in list context returns the list of results, each $list[$index] is the result value corresponding to the argument at position $index. In scalar context, hash reference is returned, where $href->{$key} holds the result value.
cas
$memd->cas($key, $cas, $value);
$memd->cas($key, $cas, $value, $expiration_time);
在服务器上的$key下存储$value,但前提是与此键关联的CAS(一致访问存储)值等于$cas,即只有该$key在没有其它客户端修改时才会被cas来set成功,它是以"gets"或"gets_multi"或"gats"或"gats_multi"返回的不透明对象。这个$cas变量即是全局的也是该变量私有的,但只有在对该变量$key进行写的时候才体现出来:对其写操作时,该变量会记住当时的全局$cat并做为其自有的$cas,然后全局的$cas的增长与其无关了;只有在对该变量进行写的时候才会存储全局的$cas,从而当前操作者通过该值的变化来感知是否有其它的客户是否对其进行修改过。此操作是原子类型的,该设计也是相当巧妙。
有关$key、$value、$expiration_timee参数的说明,请参见"set"。cas指令首现于memcached的1.2.4版本后。
Return: boolean, true for positive server reply, false for negative server reply, or undef in case of some error. Thus if the key exists on the server, false would mean that some other client has updated the value, and "gets", "gats", "cas" command sequence should be repeated.
cas_multi
$memd->cas_multi(
[$key, $cas, $value],
[$key, $cas, $value, $expiration_time],
...
);
就像"cas",但操作在多个key上。获取对每个持有$key、$cas、$value和可选$expiration_time的数组引用列表。
请注意,cas_multi操作有些可能成功,而有些可能失败。
Return: in list context returns the list of results, each $list[$index] is the result value corresponding to the argument at position $index. In scalar context, hash reference is returned, where $href->{$key} holds the result value. See "cas" to learn what the result value is.
add
$memd->add($key, $value);
$memd->add($key, $value, $expiration_time);
在服务器上的$key下存储$value,但前提是该键在服务器上不存在。有关$key、$value、$expiration_time参数的说明,请参见"set"。
Return: boolean, true for positive server reply, false for negative server reply, or undef in case of some error.
add_multi
$memd->add_multi(
[$key, $value],
[$key, $value, $expiration_time],
...
);
与"add"类似,但操作多个键。获取对数组的引用列表,每个数组包含$key、$value和可选的$expiration_time。请注意,add_multi命令有些操作可能成功,而有些可能失败。
Return: in list context returns the list of results, each $list[$index] is the result value corresponding to the argument at position $index. In scalar context, hash reference is returned, where $href->{$key} holds the result value. See "add" to learn what the result value is.
replace
$memd->replace($key, $value);
$memd->replace($key, $value, $expiration_time);
在服务器上的$key下存储$value,但前提是服务器上确实存在该键。有关$key、$value、$expiration_time参数的说明,请参见"set"。该指令首现于memcached的1.2.4版本后。
Return: boolean, true for positive server reply, false for negative server reply, or undef in case of some error.
replace_multi
$memd->replace_multi(
[$key, $value],
[$key, $value, $expiration_time],
...
);
类似于"replace",但操作多个键。获取对数组的引用列表,每个数组包含$key、$value和可选的$expiration_time。请注意,replace_multi命令不是全部或全无,有些操作可能成功,而有些操作可能失败。
Return: in list context returns the list of results, each $list[$index] is the result value corresponding to the argument at position $index. In scalar context, hash reference is returned, where $href->{$key} holds the result value.
append
$memd->append($key, $value);
将$value附加到服务器上$key下的当前值后。 $key和$value应该是标量,以及服务器上的当前值。append操作不影响值的过期时间。该指令首现于memcached的1.2.4版本后。
Return: boolean, true for positive server reply, false for negative server reply, or undef in case of some error.
append_multi
$memd->append_multi(
[$key, $value],
...
);
与"append"类似,但操作多个键。获取对每个包含$key,$value的数组的引用列表。请注意,append_multi命令有些操作可能成功,而有些可能失败。
Return: in list context returns the list of results, each $list[$index] is the result value corresponding to the argument at position $index. In scalar context, hash reference is returned, where $href->{$key} holds the result value.
prepend
$memd->prepend($key, $value);
在$key下,将$value前置到服务器上的当前值前。$key和$value应该是标量,以及服务器上的当前值。prepend不影响值的过期时间。该指令首现于memcached1.2.4版本后。
Return: boolean, true for positive server reply, false for negative server reply, or undef in case of some error.
prepend_multi
$memd->prepend_multi(
[$key, $value],
...
);
与"prepend"类似,但操作多个键。获取对每个包含$key,$value的数组的引用列表。请注意,prepend_multi命令全部操作可能成功,而有些可能失败。
Return: in list context returns the list of results, each $list[$index] is the result value corresponding to the argument at position $index. In scalar context, hash reference is returned, where $href->{$key} holds the result value. See "prepend" to learn what the result value is.
get
$memd->get($key);
检索$key下的值,应该是标量。
Return: value associated with the $key, or nothing.
get_multi
$memd->get_multi(@keys);
检索与@keys数组中关联的多个值,@key应该是标量数组。
Return: reference to hash, where $href->{$key} holds corresponding value.
gets
$memd->gets($key);
检索$key的值及其CAS,$key应该是标量。
Return: reference to an array [$cas, $value], or nothing. You may conveniently pass it back to "cas" with @$res,该指令首见于memcached 1.2.4版本。
my $cas_val = $memd->gets($key);
# Update value.
if(defined $cas_val){
$$cas_val[1] = 3;
$memd->cas($key, @$cas_val);
}
gets_multi
$memd->gets_multi(@keys);
检索@keys中几个健之值及其与关联的cas值,@键应该是标量数组。
Return: reference to hash, where $href->{$key} holds a reference to an array [$cas, $value]. Compare with "gets".
incr
$memd->incr($key);
$memd->incr($key, $increment);
增加$key的值。从memcached1.3.3引入,为该$key设置为一个数字,否则命令将失败。当假定未给定1时,可选的$increment应为正整数。请注意,服务器不会检查数字的溢出。
Return: unsigned integer, new value for the $key, or false for negative server reply, or undef in case of some error.
incr_multi
$memd->incr_multi(
@keys,
[$key],
[$key, $increment],
...
);
类似于"incr",但操作不止一个键。可对键和对数组的引用中列表进行,每个数组包含$key和可选的$increment。请注意,incr_multi命令有些操作可能成功,而有些可能失败。
Return: in list context returns the list of results, each $list[$index] is the result value corresponding to the argument at position $index. In scalar context, hash reference is returned, where $href->{$key} holds the result value. See "incr" to learn what the result value is.
decr
$memd->decr($key);
$memd->decr($key, $decrement);
递减$key的值。从memcached1.3.3引入,$key应该为一个数字,否则命令将失败。当假定未给定减去值时其值为1,可选的$decrement应为正整数。请注意,服务器确实检查下溢,尝试将该值减到零以下会将该值设置为零。与DBI类似,zero返回为"0E0",在布尔上下文中计算为true。
Return: unsigned integer, new value for the $key, or false for negative server reply, or undef in case of some error.
decr_multi
$memd->decr_multi(
@keys,
[$key],
[$key, $decrement],
...
);
与"decr"类似,但操作多个键。包括键和对数组的引用的列表,每个数组包含$key和可选的$decrement。请注意,decr_multi命令有些操作可能成功,而有些可能失败。
Return: in list context returns the list of results, each $list[$index] is the result value corresponding to the argument at position $index. In scalar context, hash reference is returned, where $href->{$key} holds the result value.
delete
$memd->delete($key);
从memached中删除指定的$key
Return: boolean, true for positive server reply, false for negative server reply, or undef in case of some error.
delete_multi
$memd->delete_multi(@keys);
类似"delete",但操作多个键。移除一组key及其值。请注意,delete_multi命令有些操作可能成功,而有些可能失败。
Return: in list context returns the list of results, each $list[$index] is the result value corresponding to the argument at position $index. In scalar context, hash reference is returned, where $href->{$key} holds the result value.
touch
$memd->touch($key, $expiration_time);
更新$key的过期时间而不获取它。可选的$expiration_time是一个正整数秒数,在该秒数之后,该值将过期并且不再可访问。该指令首见于memcached 1.4.8版本。
Return: boolean, true for positive server reply, false for negative server reply, or undef in case of some error.
touch_multi
$memd->touch_multi(
[$key],
[$key, $expiration_time],
...
);
类似于"touch",但操作不止一个键。获取对数组的引用列表,每个数组包含$key和可选的expiration_time。 请注意,touch_multi命令有些操作可能成功,而有些操作可能失败。
Return: in list context returns the list of results, each $list[$index] is the result value corresponding to the argument at position $index. In scalar context, hash reference is returned, where $href->{$key} holds the result value.
gat
$memd->gat($expiration_time, $key);
更新过期时间并返回$key的值。$key应该是标量expiration_time是一个正整数秒数,在该秒数之后,该值将过期并且不再可访问。该命令首见于memcached 1.5.3版本。
Return: value associated with the $key, or nothing.
gat_multi
$memd->gat_multi($expiration_time, @keys);
更新@keys中的key过期时间并获取相关的值,@keys应该是标量数组。
Return: reference to hash, where $href->{$key} holds corresponding value.
gats
$memd->gats($expiration_time, $key);
更新其过期时间并返回$key的值及其CAS。该指令首见于memcached 1.5.3版本。
Return: reference to an array [$cas, $value], or nothing. You may conveniently pass it back to "cas" with @$res:
my $cas_val = $memd->gats($expiration_time, $key);
# Update value.
if(defined $cas_val){
$$cas_val[1] = 3;
$memd->cas($key, @$cas_val);
}
gats_multi
$memd->gats_multi($expiration_time, @keys);
更新过期时间并检索@keys其下值及其与之关联的CASs,@keys应该是标量数组。
Return: reference to hash, where $href->{$key} holds a reference to an array [$cas, $value].
flush_all
$memd->flush_all;
$memd->flush_all($delay);
清空客户端的所有缓存。此命令使缓存中的所有项无效,在后续的检索命令中不会返回任何项,$delay是一个可选的非负整数秒数来提供延迟操作。延迟将分布在服务器上。例如,当有三台服务器,并调用flush_all(30)时,服务器将分别获得30、15和0秒的延迟。省略或为零时,即立即清空。
Return: reference to hash, where $href->{$server} holds corresponding result value. $server is either host:port or /path/to/unix.sock, as described in "servers". Result value is a boolean, true for positive server reply, false for negative server reply, or undef in case of some error.
nowait_push
$memd->nowait_push;
将所有挂起的请求推到服务器,然后等待所有答复。启用"nowait"模式时,在void上下文中发出的请求可能不会立即到达服务器(因为未等待应答)。相反,它们可能会在本地主机上的发送队列中或远程主机上的接收队列中停留相当长时间。此方法确保将它们传递到服务器并在那里处理,并且答复已到达(或发生了一些错误,导致某些连接关闭)。
析构函数将调用此方法以确保在关闭连接之前所有请求会得到处理。
Return: nothing.
server_versions
$memd->server_versions;
取得每服务器的版本信息。
Return: reference to hash, where $href->{$server} holds corresponding server version. $server is either host:port or /path/to/unix.sock, as described in "servers".
disconnect_all
$memd->disconnect_all;
关闭memcached服务器的所有打开的套接字。如果父进程对其有打开的套接字,则必须在perlfunc中的"fork"之后调用(因为子进程继承了套接字,因此两个进程最终使用同一套接字,从而导致协议错误)
Return: nothing.
最新版本:0.27
项目主页:https://metacpan.org/release/Cache-Memcached-Fast