1414class FileCache implements CacheItemPoolInterface
1515{
1616 private string $ cacheDirectory ;
17- /** @var array<CacheItemInterface> */
17+
18+ /**
19+ * @var array<CacheItemInterface>
20+ */
1821 private array $ deferred = [];
1922
23+ /**
24+ * @throws CacheException
25+ */
2026 public function __construct (string $ cacheDirectory = './.phpcca.cache ' )
2127 {
2228 $ this ->cacheDirectory = rtrim ($ cacheDirectory , '/ ' );
2329 $ this ->ensureCacheDirectory ();
2430 }
2531
32+ /**
33+ * @throws CacheException
34+ */
2635 public function getItem (string $ key ): CacheItemInterface
2736 {
2837 $ filePath = $ this ->getCacheFilePath ($ key );
@@ -39,19 +48,27 @@ public function getItem(string $key): CacheItemInterface
3948 return new CacheItem ($ key , $ data , true );
4049 }
4150
42- /** @return array<string, CacheItemInterface> */
51+ /**
52+ * @return array<string, CacheItemInterface>
53+ * @throws CacheException
54+ */
4355 public function getItems (array $ keys = []): iterable
4456 {
4557 $ items = [];
4658 foreach ($ keys as $ key ) {
4759 $ items [$ key ] = $ this ->getItem ($ key );
4860 }
61+
4962 return $ items ;
5063 }
5164
65+ /**
66+ * @throws CacheException
67+ */
5268 public function hasItem (string $ key ): bool
5369 {
5470 $ filePath = $ this ->getCacheFilePath ($ key );
71+
5572 return file_exists ($ filePath ) && $ this ->loadCacheData ($ filePath ) !== null ;
5673 }
5774
@@ -66,6 +83,9 @@ public function clear(): bool
6683 }
6784 }
6885
86+ /**
87+ * @throws CacheException
88+ */
6989 public function deleteItem (string $ key ): bool
7090 {
7191 $ filePath = $ this ->getCacheFilePath ($ key );
@@ -77,6 +97,9 @@ public function deleteItem(string $key): bool
7797 return true ;
7898 }
7999
100+ /**
101+ * @throws CacheException
102+ */
80103 public function deleteItems (array $ keys ): bool
81104 {
82105 $ success = true ;
@@ -88,12 +111,11 @@ public function deleteItems(array $keys): bool
88111 return $ success ;
89112 }
90113
114+ /**
115+ * @throws CacheException
116+ */
91117 public function save (CacheItemInterface $ item ): bool
92118 {
93- if (!$ item instanceof CacheItem) {
94- return false ;
95- }
96-
97119 $ filePath = $ this ->getCacheFilePath ($ item ->getKey ());
98120 $ data = $ item ->get ();
99121
@@ -106,11 +128,8 @@ public function save(CacheItemInterface $item): bool
106128
107129 public function saveDeferred (CacheItemInterface $ item ): bool
108130 {
109- if (!$ item instanceof CacheItem) {
110- return false ;
111- }
112-
113131 $ this ->deferred [] = $ item ;
132+
114133 return true ;
115134 }
116135
@@ -126,26 +145,35 @@ public function commit(): bool
126145 return $ success ;
127146 }
128147
148+ /**
149+ * @throws CacheException
150+ */
129151 private function ensureCacheDirectory (): void
130152 {
131- if (!is_dir ($ this ->cacheDirectory )) {
132- if (!mkdir ($ this ->cacheDirectory , 0755 , true )) {
133- throw new CacheException ("Failed to create cache directory: {$ this ->cacheDirectory }" );
134- }
153+ if (
154+ !is_dir ($ this ->cacheDirectory )
155+ && !mkdir ($ this ->cacheDirectory , 0755 , true )
156+ ) {
157+ throw new CacheException ("Failed to create cache directory: {$ this ->cacheDirectory }" );
135158 }
136159 }
137160
161+ /**
162+ * Create subdirectories to avoid too many files in one directory
163+ *
164+ * @throws CacheException
165+ */
138166 private function getCacheFilePath (string $ key ): string
139167 {
140- // Create subdirectories to avoid too many files in one directory
141168 $ hash = md5 ($ key );
142169 $ subDir = substr ($ hash , 0 , 2 );
143170 $ dir = $ this ->cacheDirectory . '/ ' . $ subDir ;
144171
145- if (!is_dir ($ dir )) {
146- if (!mkdir ($ dir , 0755 , true )) {
147- throw new CacheException ("Failed to create cache subdirectory: {$ dir }" );
148- }
172+ if (
173+ !is_dir ($ dir )
174+ && !mkdir ($ dir , 0755 , true )
175+ ) {
176+ throw new CacheException ("Failed to create cache subdirectory: {$ dir }" );
149177 }
150178
151179 return $ dir . '/ ' . $ hash . '.cache ' ;
@@ -164,18 +192,18 @@ private function loadCacheData(string $filePath): ?array
164192 return null ;
165193 }
166194
167- // Data is stored without compression for now
168-
169195 return $ data ;
170196 }
171197
172- /** @param array<string, mixed> $data */
198+ /**
199+ * Store data
200+ *
201+ * Sanitize data to ensure valid UTF-8 encoding
202+ *
203+ * @param array<string, mixed> $data
204+ */
173205 private function saveCacheData (string $ filePath , array $ data ): bool
174206 {
175- // Store data without compression for now (compression can be added later)
176- // This ensures cache works reliably
177-
178- // Sanitize data to ensure valid UTF-8 encoding
179207 $ data = $ this ->sanitizeUtf8 ($ data );
180208
181209 $ json = json_encode ($ data , JSON_PRETTY_PRINT );
@@ -184,13 +212,15 @@ private function saveCacheData(string $filePath, array $data): bool
184212 }
185213
186214 $ dir = dirname ($ filePath );
187- if (!is_dir ($ dir )) {
188- if (!mkdir ($ dir , 0755 , true )) {
189- return false ;
190- }
215+ if (
216+ !is_dir ($ dir )
217+ && !mkdir ($ dir , 0755 , true )
218+ ) {
219+ return false ;
191220 }
192221
193222 $ result = file_put_contents ($ filePath , $ json );
223+
194224 return $ result !== false ;
195225 }
196226
0 commit comments