Skip to content

Commit

Permalink
Merge pull request #6 from oat-sa/feature/cache-has-directory
Browse files Browse the repository at this point in the history
Cache has directory feature
  • Loading branch information
antoinerobin authored Mar 21, 2018
2 parents 0f858f0 + 4f38451 commit 18e2330
Showing 1 changed file with 69 additions and 6 deletions.
75 changes: 69 additions & 6 deletions src/LocalCacheAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class LocalCacheAdapter extends AbstractAdapter
*/
protected $cacheListContents = false;

/**
* Whether or not caching results of method has when directories are involved.
* @var bool
*/
protected $cacheHasDirectory = false;


/**
* array to store local file to write on destructor
Expand Down Expand Up @@ -141,6 +147,24 @@ public function setCacheListContents($cacheListContents)
$this->cacheListContents = $cacheListContents;
}

/**
* change chacheHasDirectory value
* @param $cacheHasDirectory
*/
public function setCacheHasDirectory($cacheHasDirectory)
{
$this->cacheHasDirectory = $cacheHasDirectory;
}

/**
* return cacheHasDirectory value
* @return bool
*/
public function getCacheHasDirectory()
{
return $this->cacheHasDirectory;
}

/**
* Check whether a file exists.
*
Expand All @@ -150,7 +174,32 @@ public function setCacheListContents($cacheListContents)
*/
public function has($path)
{
return $this->callWithFallback('has' , [$path]);
if ($this->getCacheHasDirectory() === true && $this->isPathDir($path)) {
$cachePath = $this->getHasDirectoryCacheExpectedPath($path);

if ($this->localStorage->has($cachePath) && ($data = $this->localStorage->read($cachePath)) !== false) {
// In cache, let's decode data.
return json_decode($data['contents']);
} else {
// Not in cache, cache it!
$hasVal = $this->callWithFallback('has' , [$path]);

$this->localStorage->write(
$cachePath,
json_encode($hasVal),
new Config()
);

return $hasVal;
}
} else {
return $this->callWithFallback('has' , [$path]);
}
}

private function isPathDir($path)
{
return preg_match('/.+\..+$/', $path) === 0;
}

/**
Expand Down Expand Up @@ -254,16 +303,30 @@ public function listContents($directory = '', $recursive = false)
*/
protected function getListContentsCacheExpectedPath($directory, $recursive)
{
$key = md5($this->localStorage->getPathPrefix() . $directory . strval($recursive));
$key = $this->buildCacheKey($this->localStorage->getPathPrefix() . $directory . strval($recursive));
$expectedPath = ".oat-lib-flysystem-cache/list-contents-cache/${key}.json";

return $expectedPath;
}

protected function getHasDirectoryCacheExpectedPath($path)
{
$key = $this->buildCacheKey($this->localStorage->getPathPrefix() . $path);
$expectedPath = ".oat-lib-flysystem-cache/has-directory-cache/${key}.json";

return $expectedPath;
}

private function buildCacheKey($origin)
{
$key = md5($origin);

// Add some directory levels to not overload a single filesystem level.
for ($i = 1; $i < 6; $i += 2) {
$key = substr_replace($key, '/', $i, 0);
}

$expectedPath = ".oat-lib-flysystem-cache/list-contents-cache/${key}.json";

return $expectedPath;

return $key;
}

/**
Expand Down

0 comments on commit 18e2330

Please sign in to comment.