Skip to content

Commit

Permalink
Add caching to exists()
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Apr 12, 2020
1 parent 814022c commit f4fe11d
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Storage/DBFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ class DBFile extends DBComposite implements AssetContainer, Thumbnail
'image/vnd.adobe.photoshop',
);

private const EXISTS_CACHED_UNTESTED = 'EXISTS_CACHED_UNTESTED';
private const EXISTS_CACHED_TRUE = 'EXISTS_CACHED_TRUE';
private const EXISTS_CACHED_FALSE = 'EXISTS_CACHED_FALSE';

/**
* Uses to cache calls to ::exists() for the duration of the request
*/
private $existsCached = self::EXISTS_CACHED_UNTESTED;

/**
* Create a new image manipulation
*
Expand Down Expand Up @@ -359,12 +368,22 @@ public function getVisibility()

public function exists()
{
if ($this->existsCached !== self::EXISTS_CACHED_UNTESTED) {
return $this->existsCached === self::EXISTS_CACHED_TRUE;
}
if (empty($this->Filename)) {
return false;
}
return $this
$exists = $this
->getStore()
->exists($this->Filename, $this->Hash, $this->Variant);
$this->existsCached = $exists ? self::EXISTS_CACHED_TRUE : self::EXISTS_CACHED_FALSE;
return $exists;
}

private function resetExistsCached(): void
{
$this->existsCached = self::EXISTS_CACHED_UNTESTED;
}

public function getFilename()
Expand Down Expand Up @@ -545,7 +564,7 @@ public function deleteFile()
if (!$this->Filename) {
return false;
}

$this->resetExistsCached();
return $this
->getStore()
->delete($this->Filename, $this->Hash);
Expand All @@ -554,6 +573,7 @@ public function deleteFile()
public function publishFile()
{
if ($this->Filename) {
$this->resetExistsCached();
$this
->getStore()
->publish($this->Filename, $this->Hash);
Expand All @@ -563,6 +583,7 @@ public function publishFile()
public function protectFile()
{
if ($this->Filename) {
$this->resetExistsCached();
$this
->getStore()
->protect($this->Filename, $this->Hash);
Expand Down Expand Up @@ -600,6 +621,7 @@ public function renameFile($newName)
if (!$this->Filename) {
return null;
}
$this->resetExistsCached();
$newName = $this
->getStore()
->rename($this->Filename, $this->Hash, $newName);
Expand All @@ -614,6 +636,7 @@ public function copyFile($newName)
if (!$this->Filename) {
return null;
}
$this->resetExistsCached();
return $this
->getStore()
->copy($this->Filename, $this->Hash, $newName);
Expand Down

0 comments on commit f4fe11d

Please sign in to comment.