public function insert($key, $value)
    {
        $this->file->flock(LOCK_SH);
        $this->file->fseek(0, SEEK_END);

        $position = $this->file->ftell();

        if (is_array($value) || is_object($value)) {
            $value = json_encode($value);
        }

        $this->file->flock(LOCK_EX);
        $this->file->fwrite(
            pack('N*', strlen($key)) .
            pack('N*', strlen($value)) .
            $key .
            $value
        );
        $this->file->flock(LOCK_UN);

        return $position;
    }
public function insert($key, $value)
    {
        $this->file->flock(LOCK_SH);
        $this->file->fseek(0, SEEK_END);

        $position = $this->file->ftell();

        if (is_array($value) || is_object($value)) {
            $value = json_encode($value);
        }

        $this->file->flock(LOCK_EX);
        $this->file->fwrite(
            pack('N*', strlen($key)) .
            pack('N*', strlen($value)) .
            $key .
            $value
        );
        $this->file->flock(LOCK_UN);

        return $position;
    }
public function insert ( $ key , $ value ) { $ this -> file -> flock ( LOCK_SH ) ; $ this -> file -> fseek ( 0 , SEEK_END ) ; $ position = $ this -> file -> ftell ( ) ; if ( is_array ( $ value ) || is_object ( $ value ) ) { $ value = json_encode ( $ value ) ; } $ this -> file -> flock ( LOCK_EX ) ; $ this -> file -> fwrite ( pack ( 'N*' , strlen ( $ key ) ) . pack ( 'N*' , strlen ( $ value ) ) . $ key . $ value ) ; $ this -> file -> flock ( LOCK_UN ) ; return $ position ; }
Write the new record to the database file

We store metadata (lengths) in the first 8 bytes of each row
allowing us to use binary searches and indexing key/value positions

@param  string  $key   The Key to be stored
@param  mixed   $value The Value to be stored

@return integer        Returns the position of the data
Write the new record to the database file