Skip to content

Commit

Permalink
Refactor load and dump to cache for router
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Jan 1, 2024
1 parent 5f473a7 commit 386b7ba
Showing 1 changed file with 10 additions and 36 deletions.
46 changes: 10 additions & 36 deletions src/Core/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
use Mezon\Router\Router as VendorRouter;
use CrazyPHP\Interface\CrazyRouterType;
use CrazyPHP\Exception\CrazyException;
use CrazyPHP\Library\Array\Arrays;
use CrazyPHP\Library\Cache\Cache;
use CrazyPHP\Library\File\Config;
use CrazyPHP\Library\File\File;
use CrazyPHP\Library\File\Json;
use CrazyPHP\Model\Context;

/**
Expand Down Expand Up @@ -249,31 +249,18 @@ public function dumpOnCache(string $key = ""):void {
]
);

# Create temp file
$folderCachePath = File::path(self::CACHE_PATH);
$fileCachePath = File::path(self::CACHE_PATH.time());

# Check folder
if(!File::exists($folderCachePath))

# Create folder
File::createDirectory($folderCachePath);
# Tmp file Path
$tmpFilePath = tempnam(sys_get_temp_dir(), 'routerDump');

# Create file cache
$this->dumpOnDisk($fileCachePath);
$this->dumpOnDisk($tmpFilePath);

# Get content of cache
$data = file_get_contents($fileCachePath);
$data = file_get_contents($tmpFilePath);

# Put on Cache
$this->cache->set($key, $data);

# Check if file exists
if(file_exists($fileCachePath))

# Remove cache
unlink($fileCachePath);

}

/**
Expand Down Expand Up @@ -313,27 +300,14 @@ public function loadFromCache(string $key = ""):void {
]
);

# Create temp file
$folderCachePath = File::path(self::CACHE_PATH);
$fileCachePath = File::path(self::CACHE_PATH.time());

# Check folder
if(!is_dir($folderCachePath))

# Create folder
mkdir($folderCachePath);
# Tmp file Path
$tmpFilePath = tempnam(sys_get_temp_dir(), 'routerDump');

# Get content of cache
$data = file_put_contents($fileCachePath, $data);
$data = file_put_contents($tmpFilePath, $data);

# Load cache
$this->loadFromDisk($fileCachePath);

# Check if file exists
if(file_exists($fileCachePath))

# Remove cache
unlink($fileCachePath);
$this->loadFromDisk($tmpFilePath);

}

Expand Down

0 comments on commit 386b7ba

Please sign in to comment.