@@ -58,6 +58,7 @@ public function __construct($path)
5858 */
5959 public function read (string $ key )
6060 {
61+ $ key = self ::sanitize ($ key );
6162 $ stmt = $ this ->pdo ->prepare ('SELECT data, slide FROM cache WHERE key=? AND (expire IS NULL OR expire >= ?) ' );
6263 $ stmt ->execute ([$ key , time ()]);
6364 if ($ row = $ stmt ->fetch (\PDO ::FETCH_ASSOC )) {
@@ -75,6 +76,7 @@ public function read(string $key)
7576 */
7677 public function bulkRead (array $ keys ): array
7778 {
79+ $ keys = array_map ([self ::class, 'sanitize ' ], $ keys );
7880 $ stmt = $ this ->pdo ->prepare ('SELECT key, data, slide FROM cache WHERE key IN (? ' . str_repeat (',? ' , count ($ keys ) - 1 ) . ') AND (expire IS NULL OR expire >= ?) ' );
7981 $ stmt ->execute (array_merge ($ keys , [time ()]));
8082 $ result = [];
@@ -83,7 +85,7 @@ public function bulkRead(array $keys): array
8385 if ($ row ['slide ' ] !== null ) {
8486 $ updateSlide [] = $ row ['key ' ];
8587 }
86- $ result [$ row ['key ' ]] = unserialize ($ row ['data ' ]);
88+ $ result [str_replace ( "\x01" , Cache:: NAMESPACE_SEPARATOR , $ row ['key ' ]) ] = unserialize ($ row ['data ' ]);
8789 }
8890 if (!empty ($ updateSlide )) {
8991 $ stmt = $ this ->pdo ->prepare ('UPDATE cache SET expire = ? + slide WHERE key IN(? ' . str_repeat (',? ' , count ($ updateSlide ) - 1 ) . ') ' );
@@ -106,6 +108,7 @@ public function lock(string $key): void
106108 */
107109 public function write (string $ key , $ data , array $ dependencies ): void
108110 {
111+ $ key = self ::sanitize ($ key );
109112 $ expire = isset ($ dependencies [Cache::EXPIRATION ]) ? $ dependencies [Cache::EXPIRATION ] + time () : null ;
110113 $ slide = isset ($ dependencies [Cache::SLIDING ]) ? $ dependencies [Cache::EXPIRATION ] : null ;
111114
@@ -131,7 +134,7 @@ public function write(string $key, $data, array $dependencies): void
131134 public function remove (string $ key ): void
132135 {
133136 $ this ->pdo ->prepare ('DELETE FROM cache WHERE key=? ' )
134- ->execute ([$ key ]);
137+ ->execute ([self :: sanitize ( $ key) ]);
135138 }
136139
137140
@@ -156,4 +159,10 @@ public function clean(array $conditions): void
156159 $ this ->pdo ->prepare ($ sql )->execute ($ args );
157160 }
158161 }
162+
163+
164+ private function sanitize ($ key )
165+ {
166+ return str_replace (Cache::NAMESPACE_SEPARATOR , "\x01" , $ key );
167+ }
159168}
0 commit comments