<?php
class
MCache
extends
Object
implements
CacheFace
{
private
$mem
= null;
private
$sId
= 1;
public
function
__construct()
{
if
( !
class_exists
('Memcache') )
{
throw
new
QException('PHP extension does not exist: Memcache');
}
$this
->mem =
new
Memcache();
}
private
function
connect(
$sid
)
{
$file
=
$this
->CacheFile();
require
$file
;
if
(! isset(
$cache
) )
{
throw
new
QException('缓存配置文件不存在'.
$file
);
}
$server
=
$cache
[
$this
->cacheId];
$sid
= isset(
$sid
) == 0 ?
$this
->sId :
$sid
;
if
( !
$server
[
$sid
])
{
throw
new
QException('当前操作的缓存服务器配置文件不存在');
}
$host
=
$server
[
$sid
]['host'];
$port
=
$server
[
$sid
]['port'];
try
{
$this
->mem->connect(
$host
,
$port
);
}
catch
(Exception
$e
) {
exit
('memecache连接失败,错误信息:'.
$e
->getMessage());
}
}
public
function
set(
$key
,
$value
,
$sid
,
$expire
= 0)
{
$data
=
$this
->get(
$key
,
$sid
);
if
(
$data
)
{
return
$this
->mem->set(
$key
,
$value
,MEMCACHE_COMPRESSED ,
$expire
);
}
else
{
return
$this
->mem->add(
$key
,
$value
,MEMCACHE_COMPRESSED ,
$expire
);
}
}
public
function
get(
$key
,
$sid
)
{
$this
->connect(
$sid
);
return
$this
->mem->get(
$key
);
}
public
function
flush
()
{
$this
->connect();
return
$this
->mem->
flush
();
}
public
function
remove(
$key
,
$sid
)
{
$this
->connect();
return
$this
->mem->
delete
(
$key
);
}
public
function
__destruct()
{
}
}