PHPスクリプトredisクラスのインスタンスソースを共有
9586 ワード
class
redisDB{
private
$redis
;
//redis
/**
* Redis
* $config = array(
* 'server' => '127.0.0.1'
* 'port' => '6379'
* )
* @param array $config
*/
function
__construct(
$config
=
array
()){
$this
->redis =
new
Redis();
$this
->redis->connect(REDIS_SERVER,REDIS_PORT);
return
$this
->redis;
}
/**
*
* @param string $key KEY
* @param string|array $value
* @param int $timeOut
*/
public
function
set(
$key
,
$value
,
$timeOut
= 0,
$type
=
'json'
) {
if
(
$type
==
'serialize'
)
{
$value
= serialize(
$value
);
}
else
{
$value
= json_encode(
$value
);
}
$retRes
=
$this
->redis->set(
$key
,
$value
);
if
(
$timeOut
> 0)
$this
->redis->setTimeout(
$key
,
$timeOut
);
return
$retRes
;
}
/**
* KEY
* @param string $key KEY
*/
public
function
get(
$key
,
$type
=
'json'
) {
$result
=
$this
->redis->get(
$key
);
if
(
$type
==
'serialize'
)
{
return
unserialize(
$result
);
}
else
{
return
json_decode(
$result
);
}
}
/**
*
* @param string $key KEY
*/
public
function
delete
(
$key
) {
return
$this
->redis->
delete
(
$key
);
}
/**
*
*/
public
function
flushAll() {
return
$this
->redis->flushAll();
}
/**
*
* @param string $key KEY
* @param string|array $value
* @param bool $right
*/
public
function
push(
$key
,
$value
,
$right
= true) {
$value
= json_encode(
$value
);
return
$right
?
$this
->redis->rPush(
$key
,
$value
) :
$this
->redis->lPush(
$key
,
$value
);
}
/**
*
* @param string $key KEY
* @param bool $left
*/
public
function
pop(
$key
,
$left
= true) {
$val
=
$left
?
$this
->redis->lPop(
$key
) :
$this
->redis->rPop(
$key
);
return
json_decode(
$val
);
}
/**
*
* @param string $key KEY
*/
public
function
increment(
$key
) {
return
$this
->redis->incr(
$key
);
}
/**
*
* @param string $key KEY
*/
public
function
decrement(
$key
) {
return
$this
->redis->decr(
$key
);
}
/**
* key , ture
* @param string $key KEY
*/
public
function
exists(
$key
) {
return
$this
->redis->exists(
$key
);
}
/**
* redis
* redis ,
* redis
*/
public
function
redis() {
return
$this
->redis;
}
}
include
'redis.php'
;
$redis
=
new
redisDB();
$key
=
'fields'
;
$value
=
' '
;
//value
$redis
->set(
$key
,
$value
);
// fields
$fvalue
=
$redis
->get(
'fields'
);
print_r(
$fvalue
);
使うならcopyで、簡単で、実用的です.へへへ
必要なものがある
memcacheのクラス
あ、これを参考にしてください.http://www.haojb.cn/php/thread-68.html