Skip to content

Commit

Permalink
Fixed the issue where calling CacheManager too may times raises an ex…
Browse files Browse the repository at this point in the history
…ception.
  • Loading branch information
Zarkawy committed Aug 4, 2021
1 parent ded1a0e commit 976f565
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions src/Core/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@
namespace Miqu\Core;

use Exception;
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
use Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException;
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;

class CacheManager
{
private static $cacheInstance;

/**
* @return ExtendedCacheItemPoolInterface
*/
private static function getInstance(): ExtendedCacheItemPoolInterface
{
if (self::$cacheInstance === null)
self::$cacheInstance = \Phpfastcache\CacheManager::getInstance( \Miqu\Helpers\env('cache.driver') );

return self::$cacheInstance;
}

/**
* @param string $key
* @param int $expires
Expand All @@ -24,7 +34,7 @@ public static function remember(string $key, int $expires, callable $callback)
if ( ! \Miqu\Helpers\env( 'cache.enabled' ) )
return call_user_func( $callback );

$instance = \Phpfastcache\CacheManager::getInstance( \Miqu\Helpers\env('cache.driver') );
$instance = self::getInstance();
$cachedObject = $instance->getItem($key);
if ( ! $cachedObject->isHit() )
{
Expand All @@ -42,19 +52,14 @@ public static function remember(string $key, int $expires, callable $callback)
/**
* @param string $key
* @return mixed|null
* @throws PhpfastcacheDriverCheckException
* @throws PhpfastcacheDriverException
* @throws PhpfastcacheDriverNotFoundException
* @throws PhpfastcacheInvalidArgumentException
* @throws PhpfastcacheInvalidConfigurationException
*/
public static function get(string $key)
{
if ( ! \Miqu\Helpers\env( 'cache.enabled' ) )
return null;

$instance = \Phpfastcache\CacheManager::getInstance('files');
$cachedObject = $instance->getItem($key);
$cachedObject = self::getInstance()->getItem($key);
if ( $cachedObject->isHit() )
return $cachedObject->get();
return null;
Expand All @@ -63,35 +68,24 @@ public static function get(string $key)
/**
* @param string $key
* @return bool
* @throws PhpfastcacheDriverCheckException
* @throws PhpfastcacheDriverException
* @throws PhpfastcacheDriverNotFoundException
* @throws PhpfastcacheInvalidArgumentException
* @throws PhpfastcacheInvalidConfigurationException
* @throws Exception
*/
public static function delete(string $key): bool
{
if ( ! \Miqu\Helpers\env( 'cache.enabled' ) )
return false;

$instance = \Phpfastcache\CacheManager::getInstance('files');
return $instance->deleteItem($key);
return self::getInstance()->deleteItem($key);
}

/**
* @return bool
* @throws PhpfastcacheDriverCheckException
* @throws PhpfastcacheDriverException
* @throws PhpfastcacheDriverNotFoundException
* @throws PhpfastcacheInvalidArgumentException
* @throws PhpfastcacheInvalidConfigurationException
*/
public static function clear(): bool
{
if ( ! \Miqu\Helpers\env( 'cache.enabled' ) )
return false;

$instance = \Phpfastcache\CacheManager::getInstance('files');
return $instance->clear();
return self::getInstance()->clear();
}
}

0 comments on commit 976f565

Please sign in to comment.