@@ -66,6 +66,15 @@ class DBFile extends DBComposite implements AssetContainer, Thumbnail
6666 'image/vnd.adobe.photoshop ' ,
6767 );
6868
69+ private const EXISTS_CACHED_UNTESTED = 'EXISTS_CACHED_UNTESTED ' ;
70+ private const EXISTS_CACHED_TRUE = 'EXISTS_CACHED_TRUE ' ;
71+ private const EXISTS_CACHED_FALSE = 'EXISTS_CACHED_FALSE ' ;
72+
73+ /**
74+ * Uses to cache calls to ::exists() for the duration of the request
75+ */
76+ private $ existsCached = self ::EXISTS_CACHED_UNTESTED ;
77+
6978 /**
7079 * Create a new image manipulation
7180 *
@@ -359,12 +368,22 @@ public function getVisibility()
359368
360369 public function exists ()
361370 {
371+ if ($ this ->existsCached !== self ::EXISTS_CACHED_UNTESTED ) {
372+ return $ this ->existsCached === self ::EXISTS_CACHED_TRUE ;
373+ }
362374 if (empty ($ this ->Filename )) {
363375 return false ;
364376 }
365- return $ this
377+ $ exists = $ this
366378 ->getStore ()
367379 ->exists ($ this ->Filename , $ this ->Hash , $ this ->Variant );
380+ $ this ->existsCached = $ exists ? self ::EXISTS_CACHED_TRUE : self ::EXISTS_CACHED_FALSE ;
381+ return $ exists ;
382+ }
383+
384+ private function resetExistsCached (): void
385+ {
386+ $ this ->existsCached = self ::EXISTS_CACHED_UNTESTED ;
368387 }
369388
370389 public function getFilename ()
@@ -545,7 +564,7 @@ public function deleteFile()
545564 if (!$ this ->Filename ) {
546565 return false ;
547566 }
548-
567+ $ this -> resetExistsCached ();
549568 return $ this
550569 ->getStore ()
551570 ->delete ($ this ->Filename , $ this ->Hash );
@@ -554,6 +573,7 @@ public function deleteFile()
554573 public function publishFile ()
555574 {
556575 if ($ this ->Filename ) {
576+ $ this ->resetExistsCached ();
557577 $ this
558578 ->getStore ()
559579 ->publish ($ this ->Filename , $ this ->Hash );
@@ -563,6 +583,7 @@ public function publishFile()
563583 public function protectFile ()
564584 {
565585 if ($ this ->Filename ) {
586+ $ this ->resetExistsCached ();
566587 $ this
567588 ->getStore ()
568589 ->protect ($ this ->Filename , $ this ->Hash );
@@ -600,6 +621,7 @@ public function renameFile($newName)
600621 if (!$ this ->Filename ) {
601622 return null ;
602623 }
624+ $ this ->resetExistsCached ();
603625 $ newName = $ this
604626 ->getStore ()
605627 ->rename ($ this ->Filename , $ this ->Hash , $ newName );
@@ -614,6 +636,7 @@ public function copyFile($newName)
614636 if (!$ this ->Filename ) {
615637 return null ;
616638 }
639+ $ this ->resetExistsCached ();
617640 return $ this
618641 ->getStore ()
619642 ->copy ($ this ->Filename , $ this ->Hash , $ newName );
0 commit comments