@@ -71,6 +71,7 @@ public function set(string $key, mixed $value, int $expiration = 0): bool {
7171 return $ this ->memcached ->set ($ key , $ value , $ expiration );
7272 }
7373
74+ $ key = preg_replace ('/[\s\x00-\x1f]+/ ' , '' , $ key );
7475 $ value = is_scalar ($ value ) ? (string ) $ value : serialize ($ value );
7576 $ raw = $ this ->runCommand ('set ' .$ key .' 0 ' .$ expiration .' ' .strlen ($ value )."\r\n" .$ value );
7677
@@ -93,6 +94,7 @@ public function get(string $key): string|false {
9394 return is_scalar ($ value ) ? (string ) $ value : serialize ($ value );
9495 }
9596
97+ $ key = preg_replace ('/[\s\x00-\x1f]+/ ' , '' , $ key );
9698 $ raw = $ this ->runCommand ('get ' .$ key );
9799 $ data = explode ("\r\n" , $ raw );
98100
@@ -110,6 +112,8 @@ public function get(string $key): string|false {
110112 * @throws MemcachedException
111113 */
112114 public function delete (string $ key ): bool {
115+ $ key = preg_replace ('/[\s\x00-\x1f]+/ ' , '' , $ key );
116+
113117 return $ this ->runCommand ('delete ' .$ key ) === 'DELETED ' ;
114118 }
115119
@@ -297,6 +301,8 @@ public function parseLine(string $line): array {
297301 * @throws MemcachedException
298302 */
299303 public function getKeyMeta (string $ key ): array {
304+ $ key = preg_replace ('/[\s\x00-\x1f]+/ ' , '' , $ key );
305+
300306 if (version_compare ($ this ->version (), '1.5.19 ' , '>= ' )) {
301307 $ raw = $ this ->runCommand ('me ' .$ key );
302308
@@ -410,16 +416,15 @@ public function disconnect(): void {
410416 * verbosity <level>\r\n
411417 * quit\r\n
412418 *
413- * Note: \r\n is added automatically to the end,
414- * and \r\n (as a plain string) is converted to a real end of the line.
419+ * Note: \r\n is added automatically to the end.
415420 *
416421 * @link https://github.com/memcached/memcached/blob/master/doc/protocol.txt
417422 *
418423 * @throws MemcachedException
419424 */
420425 public function runCommand (string $ command ): string {
421426 $ command_name = strtolower (strtok ($ command , ' ' ));
422- $ command = strtr ( $ command , [ ' \r\n ' => "\r\n" ]). "\r\n" ;
427+ $ command .= "\r\n" ;
423428 $ data = $ this ->streamConnection ($ command , $ command_name );
424429
425430 return rtrim ($ data , "\r\n" );
0 commit comments