<?php

$redisList = cfg('redis');
if (I('cfgKey')) {
    $type = I('type');
    $cfgKey = I('cfgKey');
    $key = I('key');
    $options = $redisList[$cfgKey];
    $redis = new \Redis();
    try {
        $redis->connect($options['host'], $options['port']);
        if (isset($options['password']) && $options['password'] != '') {
            $redis->auth($options['password']);
        }
    } catch (\Exception $e) {
        error('Redis ' . $e->getMessage() . ' ' . $options['host'] . ':' . $options['port'], null, [__FILE__]);
    }
    if ($type == 'view' && $key != '') {
        dd(apcu_fetch($key));
    } elseif ($type == 'delete' && $key != '') {
        $redis->del($key);
        header('Location: ' . url('xmvc/core/cmd-redis', ['cfgKey' => $cfgKey]));
    } elseif ($type == 'clearAll') {
        $redis->flushAll();
        header('Location: ' . url('xmvc/core/cmd-redis', ['cfgKey' => $cfgKey]));
    }
    $redisKeys = $redis->keys('*');
    $redisTopValues = [];
    $regI = 0;
    foreach ($redisKeys as $key) {
        if ($regI > 200) {
            break;
        }
        $redisType = $redis->type($key);
        $redisTypeString = 'Unknown Type';
        switch ($redisType) {
            case Redis::REDIS_STRING:
                $value = $redis->get($key);
                $redisTypeString = 'STRING';
                break;
            case Redis::REDIS_LIST:
                $value = $redis->lRange($key, 0, -1);
                $redisTypeString = 'LIST';
                break;
            case Redis::REDIS_SET:
                $value = $redis->sMembers($key);
                $redisTypeString = 'SET';
                break;
            case Redis::REDIS_ZSET:
                $value = $redis->zRange($key, 0, -1);
                $redisTypeString = 'ZSET';
                break;
            case Redis::REDIS_HASH:
                $value = $redis->hGetAll($key);
                $redisTypeString = 'HASH';
                break;
            default:
                $value = 'Unknown';
                $redisTypeString = 'Unknown Type';
        }
        $redisTopValues[] = [
            '删除' => '<a href="' . url('xmvc/core/cmd-redis', ['type' => 'delete', 'cfgKey' => $cfgKey, 'key' => $key]) . '">删除</a>',
            'Key' => $key,
            'Type' => $redisTypeString,
            'Value' => $value,
        ];
        $regI++;
    }
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charSet="utf-8" />
    <title>Redis UI</title>
</head>

<body>
    <div>
        <ul>
            <?php foreach ($redisList as $cfgKey => $item) : ?>
                <li><a href="<?= url('xmvc/core/cmd-redis', ['cfgKey' => $cfgKey]) ?>"><?= $cfgKey ?> (<?= $item['host'] ?>:<?= $item['port'] ?>)</a></li>
            <?php endforeach; ?>
        </ul>
    </div>
    <div>
        <?php if (I('cfgKey')) : ?>
            <h1><?= I('cfgKey') ?></h1>
            <a href="<?= url('xmvc/core/cmd-redis', ['type' => 'clearAll', 'cfgKey' => $cfgKey]) ?>" onclick="return confirm('确认清空<?= I('cfgKey') ?>所有缓存?')">
                清空<?= I('cfgKey') ?>所有缓存
            </a>
            <h3>Top 200</h3>
            <?php dumpTable($redisTopValues, ['删除' => 'width:30px', 'Key' => 'width:350px', 'Type' => 'width:50px']); ?>
        <?php endif; //cfgKey
        ?>
    </div>
</body>

</html>