Add Redis dependency

This commit is contained in:
Repflez 2021-09-10 03:35:12 -06:00
commit 734e29ac93
4 changed files with 159 additions and 1 deletions

View file

@ -10,6 +10,7 @@
"illuminate/translation": "^8.0",
"nesbot/carbon": "^2.31",
"phroute/phroute": "^2.1",
"predis/predis": "^1.1",
"symfony/console": "^5.3",
"twig/twig": "^3.0",
"wohali/oauth2-discord-new": "^1.0"

68
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "2c0f387d0670a0c885674c4f2c9fa338",
"content-hash": "20501082c4ed98a338ac13bf64ae6fc1",
"packages": [
{
"name": "cloudinary/cloudinary_php",
@ -1522,6 +1522,72 @@
},
"time": "2015-07-22T20:46:43+00:00"
},
{
"name": "predis/predis",
"version": "v1.1.7",
"source": {
"type": "git",
"url": "https://github.com/predis/predis.git",
"reference": "b240daa106d4e02f0c5b7079b41e31ddf66fddf8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/predis/predis/zipball/b240daa106d4e02f0c5b7079b41e31ddf66fddf8",
"reference": "b240daa106d4e02f0c5b7079b41e31ddf66fddf8",
"shasum": ""
},
"require": {
"php": ">=5.3.9"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
},
"suggest": {
"ext-curl": "Allows access to Webdis when paired with phpiredis",
"ext-phpiredis": "Allows faster serialization and deserialization of the Redis protocol"
},
"type": "library",
"autoload": {
"psr-4": {
"Predis\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Daniele Alessandri",
"email": "suppakilla@gmail.com",
"homepage": "http://clorophilla.net",
"role": "Creator & Maintainer"
},
{
"name": "Till Krüss",
"homepage": "https://till.im",
"role": "Maintainer"
}
],
"description": "Flexible and feature-complete Redis client for PHP and HHVM",
"homepage": "http://github.com/predis/predis",
"keywords": [
"nosql",
"predis",
"redis"
],
"support": {
"issues": "https://github.com/predis/predis/issues",
"source": "https://github.com/predis/predis/tree/v1.1.7"
},
"funding": [
{
"url": "https://github.com/sponsors/tillkruss",
"type": "github"
}
],
"time": "2021-04-04T19:34:46+00:00"
},
{
"name": "psr/container",
"version": "1.1.1",

View file

@ -16,6 +16,7 @@ setlocale(LC_TIME, 'English');
ExceptionHandler::register();
Config::load();
DB::connect(config('database'));
Cache::init();
Hashid::init(config('general.link_salt'));
Upload::init();

90
src/Cache.php Normal file
View file

@ -0,0 +1,90 @@
<?php
/**
* Holds the Redis engine.
*/
namespace Miiverse;
use Predis\Client as Redis;
/**
* Handles the Redis cache system.
*
* @author Repflez
*/
class Cache
{
/**
* The object of Predis.
*
* @var Predis\Client
*/
private static $redis = null;
/**
* Initialise Redis engine.
*/
public static function init() : void {
self::$redis = new Redis;
}
/**
* Get a value.
*
* @param string $key The key to be fetched
* @param int $ttl The TTL of the object. If the object has a larger TTL, it's considered stale and returns null.
*/
public static function get(string $key, int $ttl = 120) {
$key = 'ffe00475fc9ccd10bb002534512d2749-' . strtr($key, ':/', '-_');
// Fetch cache data from Redis.
$data = self::$redis->get($key);
if ($data) {
list($created, $contents) = unserialize($data);
if (($created + $ttl) < time()) {
return null;
} else {
return $contents;
}
} else {
return null;
}
}
/**
* Store a value.
*
* @param string $key The key name it's being stored
* @param mixed $value The value to be stored
*/
public static function store(string $key, $value) : void {
$key = 'ffe00475fc9ccd10bb002534512d2749-' . strtr($key, ':/', '-_');
$value = $value ?? null;
// Storing this in Redis
$store = serialize([time(), $value]);
self::$redis->set($key, $store);
}
/**
* Delete value
*
* @param string $key The key to delete
*/
public static function delete(string $key) : void {
$key = 'ffe00475fc9ccd10bb002534512d2749-' . strtr($key, ':/', '-_');
self::$redis->del($key);
}
/**
* Checks if redis is available.
*
* @return bool Redis availability
*/
public static function available() : bool {
return self::$redis !== null;
}
}