phpパッケージredis操作クラスについて

17404 ワード


namespace core;

/**
*    
*/
class Cache
{
	private $redis;
	private $index = 0;

	function __construct($index='')
	{
		//   Redis 
		$redis = new \Redis();
		$redis->connect('127.0.0.1', 6379);
		$this->redis = $redis;
		if($index){
			$this->index = $index;
		}
		$redis->select($this->index);
	}

	public function setCache($key,$value,$expire=7200){
		$redis = $this->redis;
		$set = $redis->set($key,$value);
		$redis->expire($key,$expire);
		return $set;
	}

	public function getCache($key){
		$redis = $this->redis;
		$val = $redis->get($key);
		return $val;
	}

	public function onlySetCache($key,$value){
		$redis = $this->redis;
		$set = $redis->set($key,$value);
		return $set;
	}

	public function getExpire($key){
		$redis = $this->redis;
		$time = $redis->ttl($key);
		return $time;
	}

	public function deleteCache($key){
		$redis = $this->redis;
		$res = $redis->delete($key);
		return $res;
	}

	//redis       

	/**
	*       hash  ,                key   
	*   key           
	*
	* @param string key  
	* @param string field   
	* @param mix value  
	*
	* @return boolen
	**/
	public function hset($key, $field, $value){
		$redis = $this->redis;
		$res = $redis->hSet($key, $field, $value);
		return $res;
	}

	/**
	*    key-field  hash   
	*
	* @param string key  
	* @param string field   
	*
	* @return mixed
	**/
	public function hget($key, $field){
		$redis = $this->redis;
		$res = $redis->hGet($key, $field);
		return $res;
	}

	/**
	*   hash      
	*
	* @param string key  
	*
	* @return mixed
	**/
	public function hlen($key){
		$redis = $this->redis;
		$len = $redis->hLen($key);
		return $len;
	}

	/**
	*    key-field  hash     
	*
	* @param string key  
	*
	* @return mixed
	**/
	public function hgetAll($key){
		$redis = $this->redis;
		$res = $redis->hGetAll($key);
		return $res;
	}

	/**
     *   hash       
     * @param string $key      
     * @param string $field     
     * @return bool
	 */
	public function hdel($key,$field)
	{
		$redis = $this->redis;
		$redis->hdel($key,$field);
		if($redis){
			return true;
		}else{
			return false;
		}
	}
}