Skip to content

Commit

Permalink
- multi server support
Browse files Browse the repository at this point in the history
- fixed some bug
- code cleanup
  • Loading branch information
erikdubbelboer committed Jul 14, 2011
1 parent 1c994bd commit b6617ab
Show file tree
Hide file tree
Showing 21 changed files with 724 additions and 515 deletions.
3 changes: 2 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ TODO

* Javascript sorting of tables
* Make delete a POST request
* Better error handling
* Move or Copy key to different server
* Importing of databases (json and redis commands)
* Support multiple servers
* JSON export with seperate objects based on your seperator


Expand Down
127 changes: 32 additions & 95 deletions common.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,9 @@
}


require 'config.inc.php';



if (isset($config['login'])) {
require 'login.inc.php';
}


$redis = new Redis();

try {
$redis->connect($config['host'], $config['port']);
} catch (Exception $e) {
die('ERROR: Could not connect to Redis');
}


if (isset($config['auth'])) {
if (!$redis->auth($config['auth'])) {
die('ERROR: Authentication failed.');
}
}


$page = array(
'css' => array('common'),
'js' => array()
);



$types = array(
Redis::REDIS_STRING => 'string',
Redis::REDIS_SET => 'set',
Redis::REDIS_LIST => 'list',
Redis::REDIS_ZSET => 'zset',
Redis::REDIS_HASH => 'hash',
Redis::REDIS_NOT_FOUND => 'other'
);



// Undo magic quotes
// Undo magic quotes (both in keys and values)
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
$process = array(&$_GET, &$_POST);

Expand All @@ -71,76 +30,54 @@



function is_ie() {
if (isset($_SERVER['HTTP_USER_AGENT']) &&
(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)) {
return true;
} else {
return false;
}
}
// These includes are needed by each script.
require_once 'config.inc.php';
require_once 'functions.inc.php';
require_once 'page.inc.php';


function format_html($str) {
return htmlentities($str, ENT_COMPAT, 'UTF-8');
if (isset($config['login'])) {
require_once 'login.inc.php';
}


function format_ago($time, $ago = false) {
$minute = 60;
$hour = $minute * 60;
$day = $hour * 24;

$when = $time;

if ($when >= 0)
$suffix = 'ago';
else {
$when = -$when;
$suffix = 'in the future';
}
// phpredis types to string conversion array.
$redistypes = array(
Redis::REDIS_STRING => 'string',
Redis::REDIS_SET => 'set',
Redis::REDIS_LIST => 'list',
Redis::REDIS_ZSET => 'zset',
Redis::REDIS_HASH => 'hash',
);

if ($when > $day) {
$when = round($when / $day);
$what = 'day';
} else if ($when > $hour) {
$when = round($when / $hour);
$what = 'hour';
} else if ($when > $minute) {
$when = round($when / $minute);
$what = 'minute';
} else {
$what = 'second';
}

if ($when != 1) $what .= 's';

if ($ago) {
return "$when $what $suffix";
} else {
return "$when $what";
}
}


function format_size($size) {
$sizes = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$i = 0;

if ($size == 0) {
return '0 B';
} else {
return round($size / pow(1024, ($i = floor(log($size, 1024)))), 1).' '.$sizes[$i];
}
if (isset($_GET['s']) && is_numeric($_GET['s']) && ($_GET['s'] < count($config['servers']))) {
$i = $_GET['s'];
}

$server = $config['servers'][$i];
$server['id'] = $i;

// Setup a connection to Redis.
$redis = new Redis();

try {
$redis->connect($server['host'], $server['port']);
} catch (Exception $e) {
die('ERROR: Could not connect to Redis ('.$server['host'].':'.$server['port'].')');
}

function str_rand($length) {
$r = '';

for (; $length > 0; --$length) {
$r .= chr(rand(32, 126));
if (isset($server['auth'])) {
if (!$redis->auth($server['auth'])) {
die('ERROR: Authentication failed ('.$server['host'].':'.$server['port'].')');
}

return $r;
}

29 changes: 25 additions & 4 deletions config.inc.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
<?

$config = array(
'host' => '127.0.0.1',
'port' => 6379,
'servers' => array(
array(
'name' => 'server 1', // Optional name.
'host' => '127.0.0.1',
'port' => 6379,

// Optional authentication.
//'auth' => 'redispasswordhere' // Warning: The password is send in plain-text to the redis server.
),

/*array(
'host' => 'localhost',
'port' => 6380
)*/
),

//'auth' => 'redispasswordhere', // Warning: The password is sent in plain-text to the redis server.

'seperator' => ':',


// Uncomment to show less information and make phpRedisAdmin fire less commands to the Redis server. Recommended for a really busy Redis server.
//'faster' => true,


// Uncomment to enable HTTP authentication
/*'login' => array(
// Username => Password
// Multiple combinations can be used
'username' => 'password'
)*/
),*/




// You can ignore settings below this point.

'maxkeylen' => 30
);

5 changes: 5 additions & 0 deletions css/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ border: 0;
}


.clear {
clear: left;
}


.info {
color: #aaa;
font-weight: normal;
Expand Down
80 changes: 7 additions & 73 deletions css/frame.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,79 +15,7 @@ margin-left: -8em;
}

form .button {
margin-left: -8em;
}

.button {
background: #3963b7;
background: -moz-linear-gradient(top, #4787d7 0%, #3963b7 100%);
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#4787d7), to(#3963b7));
border-top: 1px solid #426a98;
border-right: 1px solid #32548c;
border-bottom: 1px solid #254283;
border-left: 1px solid #32548c;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
-moz-box-shadow: inset 0 1px 0 0 #629bdc;
-webkit-box-shadow: inset 0 1px 0 0 #629bdc;
color: #fff;
font-size: 95%;
font-weight: normal;
line-height: 1;
padding: 3px 0 5px 0;
text-align: center;
text-shadow: 0 -1px 1px #5680cc;
padding: 3px 10px;
-webkit-background-clip: padding-box;
}

.button:hover {
background: #206bcb;
background: -moz-linear-gradient(top, #3e9ee5 0%, #206bcb 100%);
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#3e9ee5), to(#206bcb));
border-top: 1px solid #2a73a6;
border-right: 1px solid #165899;
border-bottom: 1px solid #07428f;
border-left: 1px solid #165899;
-moz-box-shadow: inset 0 1px 0 0 #62b1e9;
-webkit-box-shadow: inset 0 1px 0 0 #62b1e9;
cursor: pointer;
text-shadow: 0 -1px 1px #1d62ab;
-webkit-background-clip: padding-box;
}

.button:active {
background: #3282d3;
border: 1px solid #154c8c;
border-bottom: 1px solid #0e408e;
-moz-box-shadow: inset 0 0 6px 3px #1657b5, 0 1px 0 0 #fff;
-webkit-box-shadow: inset 0 0 6px 3px #1657b5, 0 1px 0 0 #fff;
text-shadow: 0 -1px 1px #2361a4;
-webkit-background-clip: padding-box;
}

.button[disabled],
.button[disabled]:hover,
.button[disabled]:active {
background: #dadada;
background: -moz-linear-gradient(top, #f3f3f3 0%, #dadada 100%);
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#dadada), to(#f3f3f3));
border-top: 1px solid #c5c5c5;
border-right: 1px solid #cecece;
border-bottom: 1px solid #d9d9d9;
border-left: 1px solid #cecece;
color: #8f8f8f;
box-shadow: none;
-moz-box-shadow: none;
-webkit-box-shadow: none;
cursor: not-allowed;
text-shadow: 0 -1px 1px #ebebeb;
}

.button::-moz-focus-inner {
border: 0;
padding: 0;
margin-left: -7em;
}


Expand Down Expand Up @@ -120,3 +48,9 @@ vertical-align: middle;
margin-top: -4px;
}


.server {
float: left;
margin: 1em;
}

2 changes: 1 addition & 1 deletion css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ text-decoration: underline;

#keys {
position: fixed;
top: 10em;
top: 12.5em;
bottom: 0;
width: 18em;
padding-bottom: 1em;
Expand Down
42 changes: 33 additions & 9 deletions delete.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,56 @@
<?

require 'common.inc.php';
require_once 'common.inc.php';



if (isset($_GET['key'])) {
// String
if (!isset($_GET['type']) || ($_GET['type'] == 'string')) {
// Delete the whole key.
$redis->delete($_GET['key']);
} else if (($_GET['type'] == 'hash') && isset($_GET['hkey'])) {
}

// Hash
else if (($_GET['type'] == 'hash') && isset($_GET['hkey'])) {
// Delete only the field in the hash.
$redis->hDel($_GET['key'], $_GET['hkey']);
} else if (($_GET['type'] == 'list') && isset($_GET['index'])) {
$value = str_rand(64);
}

// This code assumes $value is not present in the list. To make sure of this we would need to check the whole list and place a Watch on it to make the list isn't modified in between.
// List
else if (($_GET['type'] == 'list') && isset($_GET['index'])) {
// Lists don't have simple delete operations.
// You can only remove something based on a value so we set the value at the index to some random value we hope doesn't occur elsewhere in the list.
$value = str_rand(69);

// This code assumes $value is not present in the list. To make sure of this we would need to check the whole list and place a Watch on it to make sure the list isn't modified in between.
$redis->lSet($_GET['key'], $_GET['index'], $value);
$redis->lRem($_GET['key'], $value, 0);
} else if (($_GET['type'] == 'set') && isset($_GET['value'])) {
$redis->lRem($_GET['key'], $value, 1);
}

// Set
else if (($_GET['type'] == 'set') && isset($_GET['value'])) {
// Removing members from a set can only be done by supplying the member.
$redis->sRem($_GET['key'], $_GET['value']);
} else if (($_GET['type'] == 'zset') && isset($_GET['value'])) {
}

// ZSet
else if (($_GET['type'] == 'zset') && isset($_GET['value'])) {
// Removing members from a zset can only be done by supplying the value.
$redis->zDelete($_GET['key'], $_GET['value']);
}



// Refresh the top so the key tree is updated.
require 'header.inc.php';

?>
<script>
top.location.href = top.location.pathname+'?view&key=<?=urlencode($_GET['key'])?>';
top.location.href = top.location.pathname+'?view&s=<?=$server['id']?>&key=<?=urlencode($_GET['key'])?>';
</script>
<?

require 'footer.inc.php';
die;
}
Expand Down
Loading

0 comments on commit b6617ab

Please sign in to comment.