RedisApp is an attempt to use Redis as a primary database to improve database read/write performance. The application built with this feature is a blog with a score/ranking functionality. Redis data types like Hash, Set, Sorted Set, and Lists are used to store different categories of data used by the application.
You can download or clone this repository. To run the application on your system, run the following command:
composer install
php -S localhost:8000 -t public/Please note that you should have PHP and Composer installed on your local system.
The App structure uses the dotenv package to handle environment configs, Laminas\Diactoros for PSR implementations, League\Route for routing, and Laminas\HttpHandlerRunner.
// index.php
$dotenv = Dotenv\Dotenv::createImmutable(BASEPATH);
$dotenv->load();
$request = Laminas\Diactoros\ServerRequestFactory::fromGlobals();
$router = new League\Route\Router;
$router->get('/', [RedisApp\Controllers\PageController::class, 'home']);
try{
$response = $router->dispatch($request);
$emitter = new Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
$emitter->emit($response);
} catch(NotFoundException $exception) {
header('Location: /'); // or: make a 404 page
}For templating, the Twig engine was used. Predis, the Redis client for PHP was used to access Redis through the application.
//...
class PageController
{
protected $client;
public function __construct()
{
$this->client = redis();
}
//...
// check a user's profile
public function displayProfile(ServerRequestInterface $request, array $args): Response
{
$auth = authCheck();
$username = $args['username'];
if($auth['username'] == $username) return redirect('/profile');
$id = $this->client->hget("users", $username);
$userDetails = $this->client->hgetall("user:$id");
$user = [
'fullname' => $userDetails['fullname'],
'email' => $userDetails['email'],
'username' => $userDetails['username'],
'id' => $id,
'bio' => $userDetails['bio'],
];
$posts = PostHandler::getUserPosts($username);
// return jsonResponse($user);
return view('user/profile.html', ['title' => $username.' - Profile', 'user' => $user, 'posts' => $posts, 'auth' => $auth]);
}
}//...
function redis()
{
$parameters = null;
if($_ENV['APP_ENV'] == "production"){
$parameters = array(
'host' => parse_url($_ENV['REDISCLOUD_URL'], PHP_URL_HOST),
'port' => parse_url($_ENV['REDISCLOUD_URL'], PHP_URL_PORT),
'password' => parse_url($_ENV['REDISCLOUD_URL'], PHP_URL_PASS),
);
}
return new \Predis\Client($parameters, ['prefix' => 'pred:']);
}Please note the prefix used "pred:", to ensure all keys produced by the application are unique.
This App is Hosted online at https://joshredisapp.herokuapp.com/.
This was a demonstration of Redis as a primary database. The discussion is on my Medium profile. Having used this application and tested it for persistence, I believe Redis can efficiently replace a conventional database. Although one bottleneck is the size available being limited to a part of your RAM, the tradeoff is justified in cases where speed is important.
Sidenote: A blog, I believe, isn't the most popular justification for using Redis as a primary database. This is for testing purposes and should not be used in production environment.
Feedback and further advice would be appreciated. Please direct them to etimjoshua4@gmail.com
Thanks in advance!