1313
1414class Transformation extends AbstractModel
1515{
16+ /**
17+ * @var string
18+ */
19+ private $ imageNameCacheKey ;
20+
1621 private $ configuration ;
1722
1823 /**
@@ -74,6 +79,27 @@ public function getFreeTransformation()
7479 return $ this ->getData ('free_transformation ' );
7580 }
7681
82+ /**
83+ * @method cacheResult
84+ * @param bool $result
85+ * @return mixed
86+ */
87+ private function cacheResult ($ result )
88+ {
89+ $ this ->_registry ->unregister ($ this ->imageNameCacheKey );
90+ $ this ->_registry ->register ($ this ->imageNameCacheKey , $ result );
91+ return $ result ;
92+ }
93+
94+ /**
95+ * @method cacheResult
96+ * @return mixed
97+ */
98+ private function getFromCache ()
99+ {
100+ return $ this ->_registry ->registry ($ this ->imageNameCacheKey );
101+ }
102+
77103 /**
78104 * @param string $imageFile
79105 * @return ImageTransformation
@@ -89,13 +115,21 @@ public function transformationForImage($imageFile)
89115 /**
90116 * @param ImageTransformation $transformation
91117 * @param string $imageFile
118+ * @param bool $refresh
92119 * @return ImageTransformation
93120 */
94- public function addFreeformTransformationForImage (ImageTransformation $ transformation , $ imageFile )
121+ public function addFreeformTransformationForImage (ImageTransformation $ transformation , $ imageFile, $ refresh = false )
95122 {
96- $ this ->load ($ imageFile );
97- if (($ this ->getImageName () === $ imageFile ) && $ this ->hasFreeTransformation ()) {
98- $ transformation ->withFreeform (Freeform::fromString ($ this ->getFreeTransformation ()));
123+ $ this ->imageNameCacheKey = 'cldtransformcachekey_ ' . (string ) $ imageFile ;
124+ if (!$ refresh && ($ cacheResult = $ this ->getFromCache ()) !== null ) {
125+ if (($ cacheResult ->getImageName () === $ imageFile ) && $ cacheResult ->hasFreeTransformation ()) {
126+ $ transformation ->withFreeform (Freeform::fromString ($ cacheResult ->getFreeTransformation ()));
127+ }
128+ } else {
129+ $ this ->load ($ imageFile );
130+ if (($ this ->getImageName () === $ imageFile ) && $ this ->hasFreeTransformation ()) {
131+ $ transformation ->withFreeform (Freeform::fromString ($ this ->getFreeTransformation ()));
132+ }
99133 }
100134
101135 return $ transformation ;
0 commit comments