2121 */
2222class FilesystemCachePool extends AbstractCachePool
2323{
24+ const CACHE_PATH = 'cache ' ;
2425 /**
2526 * @var Filesystem
2627 */
@@ -32,33 +33,53 @@ class FilesystemCachePool extends AbstractCachePool
3233 public function __construct (Filesystem $ filesystem )
3334 {
3435 $ this ->filesystem = $ filesystem ;
35- $ this ->filesystem ->createDir (' cache ' );
36+ $ this ->filesystem ->createDir (self :: CACHE_PATH );
3637 }
3738
3839 protected function fetchObjectFromCache ($ key )
3940 {
40- return $ this ->filesystem ->read ($ key );
41+ $ file = $ this ->getFilePath ($ key );
42+ if (!$ this ->filesystem ->has ($ file )) {
43+ return ;
44+ }
45+
46+ return unserialize ($ this ->filesystem ->read ($ file ));
4147 }
4248
4349 protected function clearAllObjectsFromCache ()
4450 {
45- $ this ->filesystem ->deleteDir (' cache ' );
46- $ this ->filesystem ->createDir (' cache ' );
51+ $ this ->filesystem ->deleteDir (self :: CACHE_PATH );
52+ $ this ->filesystem ->createDir (self :: CACHE_PATH );
4753
4854 return true ;
4955 }
5056
5157 protected function clearOneObjectFromCache ($ key )
5258 {
5359 try {
54- return $ this ->filesystem ->delete ($ key );
60+ return $ this ->filesystem ->delete ($ this -> getFilePath ( $ key) );
5561 } catch (FileNotFoundException $ e ) {
5662 return true ;
5763 }
5864 }
5965
6066 protected function storeItemInCache ($ key , CacheItemInterface $ item , $ ttl )
6167 {
62- return $ this ->filesystem ->write ($ key , $ item );
68+ $ file = $ this ->getFilePath ($ key );
69+ if ($ this ->filesystem ->has ($ file )) {
70+ $ this ->filesystem ->delete ($ file );
71+ }
72+
73+ return $ this ->filesystem ->write ($ file , serialize ($ item ));
74+ }
75+
76+ /**
77+ * @param $key
78+ *
79+ * @return mixed
80+ */
81+ private function getFilePath ($ key )
82+ {
83+ return sprintf ('%s/%s ' , self ::CACHE_PATH , urlencode (base64_encode ($ key )));
6384 }
6485}
0 commit comments