Skip to content

Commit

Permalink
Merge pull request #13 from ocramleznem/urlMapCaching
Browse files Browse the repository at this point in the history
Avoid APC-cache collision
  • Loading branch information
jk committed May 23, 2018
2 parents bfee138 + 407954c commit 6399650
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/JK/RestServer/RestServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class RestServer
public $params;
public $format;
public $cacheDir = '.';
public $apcCacheSuffix;
public $realm;
/** @var Mode|string Operation mode, can be one of [debug, production] */
public $mode;
Expand Down Expand Up @@ -87,6 +88,7 @@ public function __construct($mode = Mode::PRODUCTION, $realm = 'Rest Server')
$this->mode = $mode;
$this->realm = $realm;
$this->header_manager = new HeaderManager();
$this->apcCacheSuffix = crc32(__DIR__);

if (php_sapi_name() !== 'cli') {
$this->root = ltrim(dirname($_SERVER['SCRIPT_NAME']).DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR);
Expand All @@ -108,7 +110,7 @@ public function __destruct()
{
if ($this->mode == Mode::PRODUCTION && !$this->cached) {
if (function_exists('apc_store')) {
apc_store('urlMap', $this->map);
apc_store('urlMap_'.$this->apcCacheSuffix, $this->map);
} else {
file_put_contents($this->cacheDir.DIRECTORY_SEPARATOR.'urlMap.cache', serialize($this->map));
}
Expand Down Expand Up @@ -340,7 +342,7 @@ protected function loadCache()

if ($this->mode == Mode::PRODUCTION) {
if (function_exists('apc_fetch')) {
$map = apc_fetch('urlMap');
$map = apc_fetch('urlMap_'.$this->apcCacheSuffix);
} elseif (file_exists($this->cacheDir.DIRECTORY_SEPARATOR.'urlMap.cache')) {
$map = unserialize(file_get_contents($this->cacheDir.DIRECTORY_SEPARATOR.'urlMap.cache'));
}
Expand All @@ -350,7 +352,7 @@ protected function loadCache()
}
} else {
if (function_exists('apc_delete')) {
apc_delete('urlMap');
apc_delete('urlMap_'.$this->apcCacheSuffix);
} else {
@unlink($this->cacheDir.DIRECTORY_SEPARATOR.'urlMap.cache');
}
Expand Down

0 comments on commit 6399650

Please sign in to comment.