티스토리 뷰
http://www.slideshare.net/knight1128/redis-8896084
http://www.slideshare.net/dvirsky/introduction-to-redis
레디스 PHP LIST, 수정 , 삭제 처리
host = $host; $this->port = $port; } /** * Ping server * * @param int $server_index Index of the server */ public function ping() { return $this->execute_command('PING'); } # === Scalar operations === /** * @param string $key * @param mixed $value */ public function set( $key, $value ) { $value = $this->pack_value($value); $cmd = array("SET {$key} " . strlen($value), $value); $response = $this->execute_command( $cmd ); return $this->get_error($response); } /** * @param string $key * @return mixed */ public function get( $key ) { $response = $this->execute_command( "GET {$key}" ); if ( $this->get_error($response) ) { return; } $length = (int)substr($response, 1); if ( $length > 0 ) { $value = $this->get_response(); return $this->unpack_value($value); } } /** * @param string $key */ public function delete( $key ) { return $this->execute_command( "DEL {$key}" ); } /** * @param string $key * @return boolean */ public function exists( $key ) { return $this->execute_command( "EXISTS {$key}" ) == ':1'; } /** * * @param string $key * @param int $by */ public function inc( $key, $by = 1 ) { $response = $this->execute_command( "INCRBY {$key} {$by}" ); return substr($response, 1); } /** * * @param string $key * @param int $by */ public function dec( $key, $by = 1 ) { $response = $this->execute_command( "DECRBY {$key} {$by}" ); return substr($response, 1); } # === List operations === public function prepend( $key, $value ) { $value = $this->pack_value($value); $cmd = array("LPUSH {$key} " . strlen($value), $value); $response = $this->execute_command( $cmd ); return $this->get_error($response); } public function append( $key, $value ) { $value = $this->pack_value($value); $cmd = array("RPUSH {$key} " . strlen($value), $value); $response = $this->execute_command( $cmd ); return $this->get_error($response); } public function get_list($key, $limit, $offset = 0) { $limit--; $start = $offset; $end = $start + $limit; $response = $this->execute_command( "LRANGE {$key} {$start} {$end}" ); if ( $this->get_error($response) ) { return; } $count = (int)substr($response, 1); $list = array(); for ( $i = 0; $i < $count; $i++ ) { $length = substr($this->get_response(), 1); $value = $this->get_response(); $list[] = $this->unpack_value($value); } return $list; } public function get_filtered_list($key, $filters, $limit = 0, $offset = 0) { $start = $added = 0; $end = $this->get_list_length($key); $response = $this->execute_command( "LRANGE {$key} {$start} {$end}" ); if ( $this->get_error($response) ) { return; } $limit = !$limit ? $end : $limit + $offset; $list = array(); for ( $i = 0; $i < $end; $i++ ) { $length = substr($this->get_response(), 1); $value = $this->get_response(); $value = $this->unpack_value( $value ); if ( ( $filters == array_intersect($value, $filters) ) && ( ++$added <= $limit ) ) { $list[] = $value; } } $list = array_slice($list, $offset); return $list; } public function get_list_length($key) { $response = $this->execute_command( "LLEN {$key}" ); if ( $this->get_error($response) ) { return; } return (int)substr($response, 1); } public function remove_from_list($key, $value, $count = 0) { $value = $this->pack_value($value); $response = $this->execute_command( array("LREM {$key} {$count} " . strlen($value), $value) ); if ( $this->get_error($response) ) { return; } return (int)substr($response, 1); } public function remove_by_filter($key, $filters) { $list = $this->get_filtered_list($key, $filters); foreach ( $list as $item ) { $this->remove_from_list($key, $item); } } public function truncate_list($key, $limit, $offset = 0) { $limit--; $start = $offset; $end = $start + $limit; $response = $this->execute_command( "LTRIM {$key} {$start} {$end}" ); if ( $this->get_error($response) ) { return; } return true; } public function set_list( $key , $index, $value ) { $value = $this->pack_value($value); $response = $this->execute_command( array("LSET {$key} {$index} " . strlen($value), $value) ); if ( $this->get_error($response) ) { return; } return true; } # === Set operations === public function add_member( $key, $value ) { $value = $this->pack_value($value); $cmd = array("SADD {$key} " . strlen($value), $value); $response = $this->execute_command( $cmd ); return $response == ':1'; } public function remove_member( $key, $value ) { $value = $this->pack_value($value); $cmd = array("SREM {$key} " . strlen($value), $value); $response = $this->execute_command( $cmd ); return $response == ':1'; } public function is_member( $key, $value ) { $value = $this->pack_value($value); $cmd = array("SISMEMBER {$key} " . strlen($value), $value); $response = $this->execute_command( $cmd ); return $response == ':1'; } public function get_members($key) { $response = $this->execute_command( "SMEMBERS {$key}" ); if ( $this->get_error($response) ) { return; } $count = (int)substr($response, 1); $list = array(); for ( $i = 0; $i < $count; $i++ ) { $length = substr($this->get_response(), 1); $value = $this->get_response(); $list[] = $this->unpack_value($value); } return $list; } public function get_members_count($key) { $response = $this->execute_command( "SCARD {$key}" ); if ( $this->get_error($response) ) { return; } return (int)substr($response, 1); } # === Middle tier === /** * Init connection */ private function get_connection() { if ( !$this->handle ) { if ( !$sock = fsockopen($this->host, $this->port, $errno, $errstr) ) { return false; } $this->handle = $sock; } return $this->handle; } private function pack_value( $value ) { if ( is_numeric($value) ) { return $value; } else { return serialize($value); } } private function unpack_value( $packed ) { if ( is_numeric($packed) ) { return $packed; } return unserialize($packed); } private function execute_command( $commands ) { $this->get_connection(); if ( !$this->handle ) return false; if ( is_array($commands) ) { $commands = implode("\r\n", $commands); } $command =""; $command .= $commands . "\r\n"; for ( $written = 0; $written < strlen($command); $written += $fwrite ) { if ( !$fwrite = fwrite($this->handle, substr($command, $written)) ) { return false; } } return $this->get_response(); } private function get_response() { if ( !$this->handle ) return false; return trim(fgets($this->handle), "\r\n "); } private function get_error( $response ) { if ( strpos($response, '-ERR') === 0 ) { return substr($response, 5); } return false; } } $startTime = microtime(true); $r = new php_redis("192.168.49.72"); $my = "myKeys"; //$aData = insertData(); //$aData['s_id'] = $r->inc($my . "pk"); // 중요. //$re = $r->append($my, $aData); //$result = $r->truncate( 3 ); //$result = $r->get_filtered_list($my , array( "s_id" => 9 ) ); // 검색 //$result = $r->remove_by_filter( $my, array( "s_seq" => "222") ); //삭제 //$result = $r->set_list($my, 9, updateData() ); //수정 //$result = $r->get_list($my, 10 ); // 목록 $key, limit, offset // 수정 echo ""; print_r($result); echo " "; echo microtime( true ) - $startTime; function updateData() { $aData['s_name'] = rand(0, 1000 ) . "_myname"; $aData['s_date'] = time(); $aData['s_seq'] = "222"; return $aData; } function insertData() { $aData['s_name'] = rand(0, 1000 ) . "_myname"; $aData['s_date'] = time(); $aData['s_seq'] = "1"; return $aData; }
'웹개발 > Php' 카테고리의 다른 글
php 긴 문자열 변수에 넣기. (0) | 2012.08.09 |
---|---|
array_unshift (0) | 2012.06.11 |
회원 가입 후 메일 확인 후 인증 처리하기. (2) | 2012.03.22 |
array empty check (0) | 2012.03.22 |
array_walk_recursive 재귀적으로 해당 배열의 자식들 찿을 수 있다. (0) | 2012.03.20 |
댓글