Skip to content

Instantly share code, notes, and snippets.

@kawa-
Created May 19, 2013 04:46
Show Gist options
  • Save kawa-/5606720 to your computer and use it in GitHub Desktop.
Save kawa-/5606720 to your computer and use it in GitHub Desktop.
Get all the key and values of Redis.
<?php
$redis = new Redis();
$redis->connect("localhost", 6379, 2.5); // 2.5 sec timeout.
$redis->flushAll();
$redis->set(uniqid("key_"), uniqid("key_value_1_"));
$redis->set(uniqid("key_"), uniqid("key_value_2_"));
$redis->set(uniqid("key_"), uniqid("key_value_3_"));
$redis->hMset(uniqid("hash_"), array('name' => 'yukkuri_1', 'salary' => 1000));
$redis->hMset(uniqid("hash_"), array('name' => 'yukkuri_2', 'salary' => 2000));
$redis->hMset(uniqid("hash_"), array('name' => 'yukkuri_3', 'salary' => 3000));
$redis->rPush(uniqid("list_"), uniqid("list_value_01_"), uniqid("list_value_02_"), uniqid("list_value_03_"));
$redis->rPush(uniqid("list_"), uniqid("list_value_11_"), uniqid("list_value_12_"), uniqid("list_value_13_"));
$redis->rPush(uniqid("list_"), uniqid("list_value_21_"), uniqid("list_value_22_"), uniqid("list_value_23_"));
$redis->sAdd(uniqid("set_"), uniqid("set_value_01_"), uniqid("set_value_02_"));
$redis->sAdd(uniqid("set_"), uniqid("set_value_11_"), uniqid("set_value_12_"));
$redis->sAdd(uniqid("set_"), uniqid("set_value_21_"), uniqid("set_value_22_"));
$key_zset_01 = uniqid("zset_");
$redis->zAdd($key_zset_01, time(), uniqid("zset_value_01_"));
$redis->zAdd($key_zset_01, time(), uniqid("zset_value_02_"));
$redis->zAdd($key_zset_01, time(), uniqid("zset_value_03_"));
$key_zset_02 = uniqid("zset_");
$redis->zAdd($key_zset_02, time(), uniqid("zset_value_11_"));
$redis->zAdd($key_zset_02, time(), uniqid("zset_value_12_"));
$redis->zAdd($key_zset_02, time(), uniqid("zset_value_13_"));
$key_zset_03 = uniqid("zset_");
$redis->zAdd($key_zset_03, time(), uniqid("zset_value_21_"));
$redis->zAdd($key_zset_03, time(), uniqid("zset_value_22_"));
$redis->zAdd($key_zset_03, time(), uniqid("zset_value_23_"));
// prepare arrays for each types
$key_arr = array();
$hash_arr = array();
$list_arr = array();
$set_arr = array();
$zset_arr = array();
$all_keys = $redis->keys('*');
foreach ($all_keys as $key) {
$type = $redis->type($key);
switch ($type) {
case 1: // key
$key_arr[] = $key;
break;
case 2: // set
$set_arr[] = $key;
break;
case 3: // list
$list_arr[] = $key;
break;
case 4: // zset
$zset_arr[] = $key;
break;
case 5: // hash
$hash_arr[] = $key;
break;
default:
die("Unknown type of redis has generated. Terminated.");
break;
}
}
if (count($key_arr) !== 0) {
echo "\nkey-value\n";
foreach ($key_arr as $key) {
echo $key . " | " . $redis->get($key) . "\n";
}
}
if (count($set_arr) !== 0) {
echo "\nset\n";
foreach ($set_arr as $key) {
echo $key . "\n";
foreach ($redis->sMembers($key) as $set_value) {
echo "\t" . $set_value . "\n";
}
}
}
if (count($list_arr) !== 0) {
echo "\nlist\n";
foreach ($list_arr as $key) {
echo $key . "\n";
foreach ($redis->lRange($key, 0, -1) as $list_value) {
echo "\t" . $list_value . "\n";
}
}
}
if (count($zset_arr) !== 0) {
echo "\nzset\n";
foreach ($zset_arr as $key) {
echo $key . " (score | value)\n";
foreach ($redis->zRange($key, 0, -1, true) as $zset_value => $zset_score) {
echo "\t" . $zset_score . " | " . $zset_value . "\n";
}
}
}
if (count($hash_arr) !== 0) {
echo "\nhash\n";
foreach ($hash_arr as $key) {
echo $key . "\n";
foreach ($redis->hGetAll($key) as $key1 => $value1) {
echo "\t" . $key1 . " | " . $value1 . "\n";
}
}
}
/*
Output:
key-value
key_519858a42bbb4 | key_value_2_519858a42bbc0
key_519858a42bafe | key_value_1_519858a42bb0a
key_519858a42bce6 | key_value_3_519858a42bcf2
set
set_519858a42c233
set_value_21_519858a42c23d
set_value_22_519858a42c246
set_519858a42c18d
set_value_12_519858a42c19f
set_value_11_519858a42c196
set_519858a42c0fe
set_value_02_519858a42c110
set_value_01_519858a42c108
list
list_519858a42bfce
list_value_11_519858a42bfd7
list_value_12_519858a42bfdf
list_value_13_519858a42bfe8
list_519858a42c069
list_value_21_519858a42c073
list_value_22_519858a42c07b
list_value_23_519858a42c083
list_519858a42bf1d
list_value_01_519858a42bf28
list_value_02_519858a42bf30
list_value_03_519858a42bf39
zset
zset_519858a42c5f9 (score | value)
1368938660 | zset_value_21_519858a42c606
1368938660 | zset_value_22_519858a42c7ba
1368938660 | zset_value_23_519858a42c853
zset_519858a42c2bd (score | value)
1368938660 | zset_value_01_519858a42c2d2
1368938660 | zset_value_02_519858a42c375
1368938660 | zset_value_03_519858a42c3f1
zset_519858a42c481 (score | value)
1368938660 | zset_value_11_519858a42c48d
1368938660 | zset_value_12_519858a42c508
1368938660 | zset_value_13_519858a42c581
hash
hash_519858a42be22
name | yukkuri_2
salary | 2000
hash_519858a42bd6e
name | yukkuri_1
salary | 1000
hash_519858a42bea1
name | yukkuri_3
salary | 3000
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment