feat(cache): Checking if extensions are installed (#1759)

* feat(cache): Checking if extensions are installed

* Updated

* Update APCu.php

* Update Sqlite.php

* Update Sqlite.php

* Update APCu.php

* Update Sqlite.php

* Update APCu.php

* Updated

* Update APCu.php

* Update Redis.php
This commit is contained in:
Roman Kelesidis 2025-01-15 14:25:16 +03:00 committed by GitHub
commit 7f31022cfc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 140 additions and 6 deletions

View file

@ -13,6 +13,8 @@ use TorrentPier\Dev;
use MatthiasMullie\Scrapbook\Adapters\Apc;
use Exception;
/**
* Class APCu
* @package TorrentPier\Legacy\Cache
@ -54,6 +56,9 @@ class APCu extends Common
*/
public function __construct(string $prefix)
{
if (!$this->isInstalled()) {
throw new Exception('ext-apcu not installed. Check out php.ini file');
}
$this->apcu = new Apc();
$this->prefix = $prefix;
$this->dbg_enabled = Dev::sqlDebugAllowed();
@ -127,4 +132,14 @@ class APCu extends Common
return $result;
}
/**
* Checking if APCu is installed
*
* @return bool
*/
private function isInstalled(): bool
{
return extension_loaded('apcu') && function_exists('apcu_fetch');
}
}

View file

@ -14,6 +14,8 @@ use TorrentPier\Dev;
use Memcached as MemcachedClient;
use MatthiasMullie\Scrapbook\Adapters\Memcached as MemcachedCache;
use Exception;
/**
* Class Memcached
* @package TorrentPier\Legacy\Cache
@ -77,6 +79,9 @@ class Memcached extends Common
*/
public function __construct(array $cfg, string $prefix)
{
if (!$this->isInstalled()) {
throw new Exception('ext-memcached not installed. Check out php.ini file');
}
$this->client = new MemcachedClient();
$this->cfg = $cfg;
$this->prefix = $prefix;
@ -98,7 +103,7 @@ class Memcached extends Common
}
if (!$this->connected) {
die("Could not connect to $this->engine server");
throw new Exception("Could not connect to $this->engine server");
}
$this->memcached = new MemcachedCache($this->client);
@ -187,4 +192,14 @@ class Memcached extends Common
return $result;
}
/**
* Checking if Memcached is installed
*
* @return bool
*/
private function isInstalled(): bool
{
return extension_loaded('memcached') && class_exists('Memcached');
}
}

View file

@ -14,6 +14,8 @@ use TorrentPier\Dev;
use Redis as RedisClient;
use MatthiasMullie\Scrapbook\Adapters\Redis as RedisCache;
use Exception;
/**
* Class Redis
* @package TorrentPier\Legacy\Cache
@ -77,6 +79,9 @@ class Redis extends Common
*/
public function __construct(array $cfg, string $prefix)
{
if (!$this->isInstalled()) {
throw new Exception('ext-redis not installed. Check out php.ini file');
}
$this->client = new RedisClient();
$this->cfg = $cfg;
$this->prefix = $prefix;
@ -100,7 +105,7 @@ class Redis extends Common
}
if (!$this->connected) {
die("Could not connect to $this->engine server");
throw new Exception("Could not connect to $this->engine server");
}
$this->redis = new RedisCache($this->client);
@ -189,4 +194,14 @@ class Redis extends Common
return $result;
}
/**
* Checking if Redis is installed
*
* @return bool
*/
private function isInstalled(): bool
{
return extension_loaded('redis') && class_exists('Redis');
}
}

View file

@ -14,6 +14,8 @@ use TorrentPier\Dev;
use MatthiasMullie\Scrapbook\Adapters\SQLite as SQLiteCache;
use PDO;
use Exception;
/**
* Class Sqlite
* @package TorrentPier\Legacy\Cache
@ -34,6 +36,13 @@ class Sqlite extends Common
*/
public string $engine = 'SQLite';
/**
* SQLite DB file extension
*
* @var string
*/
public string $dbExtension = '.db';
/**
* Cache prefix
*
@ -56,7 +65,10 @@ class Sqlite extends Common
*/
public function __construct(string $dir, string $prefix)
{
$client = new PDO("sqlite:$dir.db");
if (!$this->isInstalled()) {
throw new Exception('ext-pdo_sqlite not installed. Check out php.ini file');
}
$client = new PDO('sqlite:' . $dir . $this->dbExtension);
$this->sqlite = new SQLiteCache($client);
$this->prefix = $prefix;
$this->dbg_enabled = Dev::sqlDebugAllowed();
@ -130,4 +142,14 @@ class Sqlite extends Common
return $result;
}
/**
* Checking if PDO SQLite is installed
*
* @return bool
*/
private function isInstalled(): bool
{
return extension_loaded('pdo_sqlite') && class_exists('PDO');
}
}

View file

@ -13,6 +13,8 @@ use TorrentPier\Dev;
use MatthiasMullie\Scrapbook\Adapters\Apc;
use Exception;
/**
* Class APCu
* @package TorrentPier\Legacy\Datastore
@ -47,6 +49,9 @@ class APCu extends Common
*/
public function __construct(string $prefix)
{
if (!$this->isInstalled()) {
throw new Exception('ext-apcu not installed. Check out php.ini file');
}
$this->apcu = new Apc();
$this->prefix = $prefix;
$this->dbg_enabled = Dev::sqlDebugAllowed();
@ -121,4 +126,14 @@ class APCu extends Common
$this->num_queries++;
}
}
/**
* Checking if APCu is installed
*
* @return bool
*/
private function isInstalled(): bool
{
return extension_loaded('apcu') && function_exists('apcu_fetch');
}
}

View file

@ -14,6 +14,8 @@ use TorrentPier\Dev;
use Memcached as MemcachedClient;
use MatthiasMullie\Scrapbook\Adapters\Memcached as MemcachedCache;
use Exception;
/**
* Class Memcached
* @package TorrentPier\Legacy\Datastore
@ -70,6 +72,9 @@ class Memcached extends Common
*/
public function __construct(array $cfg, string $prefix)
{
if (!$this->isInstalled()) {
throw new Exception('ext-memcached not installed. Check out php.ini file');
}
$this->client = new MemcachedClient();
$this->cfg = $cfg;
$this->prefix = $prefix;
@ -91,7 +96,7 @@ class Memcached extends Common
}
if (!$this->connected) {
die("Could not connect to $this->engine server");
throw new Exception("Could not connect to $this->engine server");
}
$this->memcached = new MemcachedCache($this->client);
@ -181,4 +186,14 @@ class Memcached extends Common
$this->num_queries++;
}
}
/**
* Checking if Memcached is installed
*
* @return bool
*/
private function isInstalled(): bool
{
return extension_loaded('memcached') && class_exists('Memcached');
}
}

View file

@ -14,6 +14,8 @@ use TorrentPier\Dev;
use Redis as RedisClient;
use MatthiasMullie\Scrapbook\Adapters\Redis as RedisCache;
use Exception;
/**
* Class Redis
* @package TorrentPier\Legacy\Datastore
@ -70,6 +72,9 @@ class Redis extends Common
*/
public function __construct(array $cfg, string $prefix)
{
if (!$this->isInstalled()) {
throw new Exception('ext-redis not installed. Check out php.ini file');
}
$this->client = new RedisClient();
$this->cfg = $cfg;
$this->prefix = $prefix;
@ -93,7 +98,7 @@ class Redis extends Common
}
if (!$this->connected) {
die("Could not connect to $this->engine server");
throw new Exception("Could not connect to $this->engine server");
}
$this->redis = new RedisCache($this->client);
@ -183,4 +188,14 @@ class Redis extends Common
$this->num_queries++;
}
}
/**
* Checking if Redis is installed
*
* @return bool
*/
private function isInstalled(): bool
{
return extension_loaded('redis') && class_exists('Redis');
}
}

View file

@ -14,6 +14,8 @@ use TorrentPier\Dev;
use MatthiasMullie\Scrapbook\Adapters\SQLite as SQLiteCache;
use PDO;
use Exception;
/**
* Class Sqlite
* @package TorrentPier\Legacy\Datastore
@ -27,6 +29,13 @@ class Sqlite extends Common
*/
public string $engine = 'SQLite';
/**
* SQLite DB file extension
*
* @var string
*/
public string $dbExtension = '.db';
/**
* Cache prefix
*
@ -49,7 +58,10 @@ class Sqlite extends Common
*/
public function __construct(string $dir, string $prefix)
{
$client = new PDO("sqlite:$dir.db");
if (!$this->isInstalled()) {
throw new Exception('ext-pdo_sqlite not installed. Check out php.ini file');
}
$client = new PDO('sqlite:' . $dir . $this->dbExtension);
$this->sqlite = new SQLiteCache($client);
$this->prefix = $prefix;
$this->dbg_enabled = Dev::sqlDebugAllowed();
@ -124,4 +136,14 @@ class Sqlite extends Common
$this->num_queries++;
}
}
/**
* Checking if PDO SQLite is installed
*
* @return bool
*/
private function isInstalled(): bool
{
return extension_loaded('pdo_sqlite') && class_exists('PDO');
}
}