PHP redis操作類個人まとめ
5823 ワード
<pre name="code" class="php"><span style="font-size:18px;">class MyRedis {
private $redis;
/**
* @param string $host
* @param int $post
*/
public function __construct($host = '10.102.1.8', $port = 6379) {
$this->redis = new Redis();
$this->redis->connect($host, $port);
return $this->redis;
}
/**
*
* @param string $key KEY
* @param string $value
* @param int $timeOut 0
*/
public function set($key, $value, $timeOut=0) {
$retRes = $this->redis->set($key, $value);
if ($timeOut > 0)
$redis->expire('$key', $timeOut);
return $retRes;
}
/*
* ( )
* @param string $key Y
* @param string|array $value
*/
public function sadd($key,$value){
return $this->redis->sadd($key,$value);
}
/*
* ( )
* @param string $key
* @param string|array $value
*/
public function zadd($key,$value){
return $this->redis->zadd($key,$value);
}
/**
*
* @param string $setName
*/
public function smembers($setName){
return $this->redis->smembers($setName);
}
/**
* ( , )
* @param sting $key KEY
* @param string $value
*/
public function lpush($key,$value){
echo "$key - $value
";
return $this->redis->LPUSH($key,$value);
}
/**
* ( , )
* @param sting $key KEY
* @param string $value
*/
public function rpush($key,$value){
echo "$key - $value
";
return $this->redis->rpush($key,$value);
}
/**
* ( )
* @param sting $key KEY
* @param int $head
* @param int $tail
*/
public function lranges($key,$head,$tail){
return $this->redis->lrange($key,$head,$tail);
}
/**
* HASH
* @param string $tableName key
* @param string $key
* @param sting $value
*/
public function hset($tableName,$field,$value){
return $this->redis->hset($tableName,$field,$value);
}
public function hget($tableName,$field){
return $this->redis->hget($tableName,$field);
}
/**
*
* @param array $keyArray KEY
* @param string|array $value
* @param int $timeOut
*/
public function sets($keyArray, $timeout) {
if (is_array($keyArray)) {
$retRes = $this->redis->mset($keyArray);
if ($timeout > 0) {
foreach ($keyArray as $key => $value) {
$this->redis->expire($key, $timeout);
}
}
return $retRes;
} else {
return "Call " . __FUNCTION__ . " method parameter Error !";
}
}
/**
* key
* @param string $key KEY
*/
public function get($key) {
$result = $this->redis->get($key);
return $result;
}
/**
*
* @param ayyay $keyArray key
*/
public function gets($keyArray) {
if (is_array($keyArray)) {
return $this->redis->mget($keyArray);
} else {
return "Call " . __FUNCTION__ . " method parameter Error !";
}
}
/**
* key ,
*/
public function keyAll() {
return $this->redis->keys('*');
}
/**
* key
* @param string $key KEY
*/
public function del($key) {
return $this->redis->delete($key);
}
/**
* key
* @param array $keyArray KEY
*/
public function dels($keyArray) {
if (is_array($keyArray)) {
return $this->redis->del($keyArray);
} else {
return "Call " . __FUNCTION__ . " method parameter Error !";
}
}
/**
*
* @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
* @param string $key KEY
*/
public function isExists($key){
return $this->redis->exists($key);
}
/**
* - newkey , key newkey , newkey RENAME
* rename , ( )
* @param string $Key KEY
* @param string $newKey key
*/
public function updateName($key,$newKey){
return $this->redis->RENAMENX($key,$newKey);
}
/**
* KEY
* none(key ) int(0) string( ) int(1) list( ) int(3) set( ) int(2) zset( ) int(4) hash( ) int(5)
* @param string $key KEY
*/
public function dataType($key){
return $this->redis->type($key);
}
/**
*
*/
public function flushAll() {
return $this->redis->flushAll();
}
/**
* redis
* redis ,
* redis
* eg:$redis->redisOtherMethods()->keys('*a*') keys
*/
public function redisOtherMethods() {
return $this->redis;
}
}</span>