Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Add flag isProduction with value default is true - check write an…
Browse files Browse the repository at this point in the history
…d read the cache files if this flag is `true`.
  • Loading branch information
thang-d committed Feb 11, 2019
1 parent 5c0a2cd commit daa4f15
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/CacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ class CacheProvider {
private $times = 4;
private $saltAddToPath;
private $requestTarget;
// Just enable caching in production mode.
private $isProduction;

public function __construct($cacheRootDir)
public function __construct(string $cacheRootDir, bool $isProduction = true)
{
$this->cacheHelper = CacheHelper::getInstance($cacheRootDir);
$this->isProduction = $isProduction;
}

/**
* Cache any type to cache dir in server
*
* @param Request $request
* @param callable $callable
* @return mixed
Expand Down Expand Up @@ -71,7 +75,7 @@ public function cacheArray(Request $request, callable $callable) : array
throw new InvalidArgumentException('Callable must be return an array');
}

if ($dataArrayCache != '{}') {
if ($dataArrayCache !== '{}') {
$this->writeCache($request, $this->cacheHelper->array_2_json($dataArrayCache));
}
}
Expand Down Expand Up @@ -128,6 +132,10 @@ public function customRequestTarget(string $target) : self
*/
private function writeCache(Request $request, string $content)
{
if (!$this->isProduction) {
return false;
}

$requestTarget = $this->createTargetPath($request);

$this->cacheHelper->setContent($requestTarget, $content);
Expand All @@ -141,6 +149,10 @@ private function writeCache(Request $request, string $content)
*/
private function readCache(Request $request)
{
if (!$this->isProduction) {
return null;
}

$requestTarget = $this->createTargetPath($request);
return $this->cacheHelper->getContent($requestTarget);
}
Expand Down

0 comments on commit daa4f15

Please sign in to comment.