<?php
namespace
YBL;
class
memcache
{
protected
$config
=
array
();
public
$memcache_obj
= '';
protected
$host_list
= '';
protected
$connectFailures
= 0;
public
function
__construct(
$config
=
array
())
{
$this
->config =
array
(
'host' => '127.0.0.1',
'port' => 11211,
'persistent' => false,
'weight' => 1,
'timeout' => 1,
'retry_interval' => 15,
'status' => false,
'threshold' => 1,
'min_savings' => 0.2,
'max_connect_retries' => 5
);
if
(!isset(
$config
['memcache_deploy']) || !
is_callable
(
$config
['memcache_deploy'])) {
$config
['memcache_deploy'] =
array
(
__CLASS__
,
'default_memcache_deploy'
);
}
if
(!isset(
$config
['memcache_hdeploy']) || !
is_callable
(
$config
['memcache_hdeploy'])) {
$config
['memcache_hdeploy'] =
array
(
__CLASS__
,
'default_memcache_hdeploy'
);
}
if
(!isset(
$config
['failure_callback']) || !
is_callable
(
$config
['failure_callback'])) {
$config
['failure_callback'] =
array
(
__CLASS__
,
'default_error_hander'
);
}
$this
->config =
array_merge
(
$this
->config,
$config
);
$this
->memcache_obj =
new
\Memcache;
}
public
function
host(
$host_list
= '')
{
$this
->host_list =
$host_list
;
return
$this
;
}
static
public
function
default_error_hander(
$host
=
array
())
{
}
static
public
function
default_memcache_hdeploy(
$host
,
$hash
= '')
{
static
$node_object
;
if
(!(
$node_object
instanceof
yhash)) {
$config
=
array
(
'replicas' => 20
);
$node_object
=
new
yhash(
$config
);
$node_object
->addTargets(
array_keys
(
$host
));
}
return
$node_object
->lookup(
$hash
);
}
static
public
function
default_memcache_deploy(
$host
)
{
$keys
=
array_keys
(
$host
);
$key
=
array_rand
(
$keys
, 1);
return
$keys
[
$key
];
}
public
function
alink()
{
$default_host
=
array
(
'host' =>
$this
->config['host'],
'port' =>
$this
->config['port'],
'persistent' =>
$this
->config['persistent'],
'weight' =>
$this
->config['weight'],
'timeout' =>
$this
->config['timeout'],
'retry_interval' =>
$this
->config['retry_interval'],
'status' =>
$this
->config['status'],
'failure_callback' =>
$this
->config['failure_callback'],
'threshold' =>
$this
->config['threshold'],
'min_savings' =>
$this
->config['min_savings']
);
$failure_callback
= '';
if
(!
empty
(
$this
->host_list)) {
foreach
(
$this
->host_list
as
$_host
) {
$_host
=
array_merge
(
$default_host
,
$_host
);
$failure_callback
=
function
(
$host
= '',
$port
= 11211)
use
(
$_host
)
{
return
call_user_func_array(
$_host
['failure_callback'],
array
(
$_host
));
};
$this
->memcache_obj->addServer(
$_host
['host'],
$_host
['port'],
$_host
['persistent'],
$_host
['weight'],
$_host
['timeout'],
$_host
['retry_interval'],
$_host
['status'],
$failure_callback
);
if
(
$_host
['threshold']) {
$this
->memcache_obj->setCompressThreshold(
$_host
['threshold'],
$_host
['min_savings']);
}
}
}
else
{
$failure_callback
=
function
(
$host
= '',
$port
= 11211)
use
(
$default_host
)
{
return
call_user_func_array(
$default_host
['failure_callback'],
array
(
$default_host
));
};
$this
->memcache_obj->addServer(
$default_host
['host'],
$default_host
['port'],
$default_host
['persistent'],
$default_host
['weight'],
$default_host
['timeout'],
$default_host
['retry_interval'],
$default_host
['status'],
$failure_callback
);
if
(
$default_host
['threshold']) {
$this
->memcache_obj->setCompressThreshold(
$default_host
['threshold'],
$default_host
['min_savings']);
}
}
}
public
function
link()
{
$default_host
= '';
$host_no
= -1;
$return
= false;
$_host
=
array
();
if
(!
empty
(
$this
->host_list)) {
$host_no
= call_user_func_array(
$this
->config['memcache_deploy'],
array
(
$this
->host_list
));
$_host
=
$this
->host_list[
$host_no
];
}
$default_host
=
array
(
'host' =>
$this
->config['host'],
'port' =>
$this
->config['port'],
'persistent' =>
$this
->config['persistent'],
'timeout' =>
$this
->config['timeout'],
'retry_interval' =>
$this
->config['retry_interval'],
'failure_callback' =>
$this
->config['failure_callback'],
'threshold' =>
$this
->config['threshold'],
'min_savings' =>
$this
->config['min_savings']
);
$_host
=
array_merge
(
$default_host
,
$_host
);
if
(
$_host
['persistent']) {
$return
= @
$this
->memcache_obj->pconnect(
$_host
['host'],
$_host
['port'],
$_host
['timeout']);
}
else
{
$return
= @
$this
->memcache_obj->connect(
$_host
['host'],
$_host
['port'],
$_host
['timeout']);
}
if
(!
$return
) {
$this
->connectFailures++;
if
(
$this
->connectFailures <=
$this
->config['max_connect_retries']) {
if
(
count
(
$this
->host_list) > 1) {
call_user_func_array(
$_host
['failure_callback'],
array
(
$_host
));
usleep(
$_host
['interval_time']);
unset(
$this
->host_list[
$host_no
]);
$this
->link();
}
else
{
$this
->connectFailures = 0;
call_user_func_array(
$_host
['failure_callback'],
array
(
$_host
));
}
}
else
{
$this
->connectFailures = 0;
call_user_func_array(
$_host
['failure_callback'],
array
(
$_host
));
}
}
else
{
$this
->connectFailures = 0;
if
(
$_host
['threshold']) {
$this
->memcache_obj->setCompressThreshold(
$_host
['threshold'],
$_host
['min_savings']);
}
}
}
public
function
hlink(
$hash
= '')
{
$default_host
= '';
$host_no
= -1;
$return
= false;
$_host
=
array
();
if
(!
empty
(
$this
->host_list)) {
$host_no
= call_user_func_array(
$this
->config['memcache_hdeploy'],
array
(
$this
->host_list,
$hash
));
$_host
=
$this
->host_list[
$host_no
];
}
$default_host
=
array
(
'host' =>
$this
->config['host'],
'port' =>
$this
->config['port'],
'persistent' =>
$this
->config['persistent'],
'timeout' =>
$this
->config['timeout'],
'retry_interval' =>
$this
->config['retry_interval'],
'failure_callback' =>
$this
->config['failure_callback'],
'threshold' =>
$this
->config['threshold'],
'min_savings' =>
$this
->config['min_savings'],
'hash' =>
$hash
);
$_host
=
array_merge
(
$default_host
,
$_host
);
if
(
$_host
['persistent']) {
$return
=
$this
->memcache_obj->pconnect(
$_host
['host'],
$_host
['port'],
$_host
['timeout']);
}
else
{
$return
=
$this
->memcache_obj->connect(
$_host
['host'],
$_host
['port'],
$_host
['timeout']);
}
if
(!
$return
) {
$this
->connectFailures = 0;
call_user_func_array(
$_host
['failure_callback'],
array
(
$_host
));
}
else
{
if
(
$_host
['threshold']) {
$this
->memcache_obj->setCompressThreshold(
$_host
['threshold'],
$_host
['min_savings']);
}
}
}
public
function
_server(
$type
= '',
$slabid
= 1,
$limit
= 1000000)
{
if
(
$type
=== '')
return
$this
->memcache_obj->getStats();
if
(
$type
!= 'cachedump')
return
$this
->memcache_obj->getStats(
$type
);
return
$this
->memcache_obj->getStats(
$type
,
$slabid
,
$limit
);
}
public
function
_allserver(
$type
= '',
$slabid
= 1,
$limit
= 1000000)
{
if
(
$type
=== '')
return
$this
->memcache_obj->getExtendedStats();
if
(
$type
!= 'cachedump')
return
$this
->memcache_obj->getExtendedStats(
$type
);
return
$this
->memcache_obj->getExtendedStats(
$type
,
$slabid
,
$limit
);
}
public
function
close()
{
return
$this
->memcache_obj->close();
}
public
function
slabid()
{
$ids
=
$id
=
array
();
$ids
=
$this
->memcache_obj->getStats('slabs');
foreach
(
$ids
as
$k
=>
$v
) {
if
(
is_int
(
$k
)) {
$id
[] =
$k
;
}
}
return
$id
;
}
public
function
getallkey(
$limit
= 1000000)
{
$ids
=
$this
->slabid();
$arr
=
array
();
foreach
(
$ids
as
$id
) {
$arr
=
array_merge
(
array_keys
(
$this
->memcache_obj->getStats('cachedump',
$id
,
$limit
)),
$arr
);
}
return
$arr
;
}
public
function
set(
$key
,
$var
,
$expire
= 0,
$flag
= MEMCACHE_COMPRESSED)
{
return
$this
->memcache_obj->set(
$key
,
$var
,
$flag
,
$expire
);
}
public
function
add(
$key
,
$var
,
$expire
= 0,
$flag
= MEMCACHE_COMPRESSED)
{
return
$this
->memcache_obj->add(
$key
,
$var
,
$flag
,
$expire
);
}
public
function
get(
$key
, &
$flag
= 0)
{
$gflag
= 0;
$return
=
$this
->memcache_obj->get(
$key
,
$gflag
);
$flag
=
$gflag
;
return
$return
;
}
public
function
delete
(
$key
,
$timeout
= 0)
{
if
(
$key
=== 'all') {
return
$this
->memcache_obj->
flush
();
}
return
$this
->memcache_obj->
delete
(
$key
,
$timeout
);
}
public
function
replace(
$key
,
$var
,
$expire
= 0,
$flag
= MEMCACHE_COMPRESSED)
{
return
$this
->memcache_obj->replace(
$key
,
$var
,
$flag
,
$expire
);
}
public
function
increment(
$key
,
$var
)
{
return
$this
->memcache_obj->increment(
$key
, (int)
$var
);
}
public
function
decrement(
$key
,
$var
)
{
return
$this
->memcache_obj->decrement(
$key
, (int)
$var
);
}
public
function
append(
$key
,
$var
,
$expire
= 0,
$flag
= MEMCACHE_COMPRESSED)
{
$return
=
$this
->memcache_obj->get(
$key
);
if
(
empty
(
$return
))
return
false;
return
$this
->memcache_obj->replace(
$key
,
$return
.
$var
,
$flag
,
$expire
);
}
public
function
prepend(
$key
,
$var
,
$expire
= 0,
$flag
= MEMCACHE_COMPRESSED)
{
$return
=
$this
->memcache_obj->get(
$key
);
if
(
empty
(
$return
))
return
false;
return
$this
->memcache_obj->replace(
$key
,
$var
.
$return
,
$flag
,
$expire
);
}
public
function
cas(
$key
,
$key2
)
{
$return1
=
$this
->memcache_obj->get(
$key
);
if
(
empty
(
$return1
))
return
false;
$return2
=
$this
->memcache_obj->get(
$key2
);
if
(
empty
(
$return2
))
return
false;
return
(
$this
->memcache_obj->replace(
$key
,
$return2
)) && (
$this
->memcache_obj->replace(
$key2
,
$return
));
}
}