Skip to content

Commit

Permalink
feat: add method getAllKeys, getKeysWithPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
ouqiang committed Jun 21, 2017
1 parent 62228ff commit a0faf88
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ vendor

# project
index.php
package.json
node_modules
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ $client->put('redis', '127.0.0.1:6579', ['lease' => 7587822882194199413]);
// get key value
$client->get('redis');

// get all keys
$client->getAllKeys();

// get keys with prefix
$client->getKeysWithPrefix('/v3/service/user/');

// delete key
$client->del('redis');

Expand Down
31 changes: 31 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,37 @@ public function get($key, array $options = [])
return $body;
}

/**
* get all keys
*
* @return array|\GuzzleHttp\Exception\BadResponseException
*/
public function getAllKeys()
{
return $this->get("\0", ['range_end' => "\0"]);
}

/**
* get all keys with prefix
*
* @param string $prefix
* @return array|\GuzzleHttp\Exception\BadResponseException
*/
public function getKeysWithPrefix($prefix)
{
$prefix = trim($prefix);
if (!$prefix) {
return [];
}
$lastIndex = strlen($prefix) - 1;
$lastChar = $prefix[$lastIndex];
$nextAsciiCode = ord($lastChar) + 1;
$rangeEnd = $prefix;
$rangeEnd[$lastIndex] = chr($nextAsciiCode);

return $this->get($prefix, ['range_end' => $rangeEnd]);
}

/**
* Removes the specified key or range of keys
*
Expand Down
12 changes: 12 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ public function testPutAndRange()
$this->assertEquals($value, $body[$this->key]);
}

public function testGetAllKeys()
{
$body = $this->client->getAllKeys();
$this->assertNotEmpty($body);
}

public function testGetKeysWithPrefix()
{
$body = $this->client->getKeysWithPrefix('/');
$this->assertNotEmpty($body);
}

public function testDeleteRange()
{
$this->client->del($this->key);
Expand Down

0 comments on commit a0faf88

Please sign in to comment.