Don't double-serialize values for APC
authorOri Livneh <ori@wikimedia.org>
Wed, 22 Jul 2015 22:26:02 +0000 (15:26 -0700)
committerOri Livneh <ori@wikimedia.org>
Wed, 22 Jul 2015 23:20:52 +0000 (16:20 -0700)
The last time we had encountered APC errors related to serialization /
unserialization was 2011. PHP's implementation has had many bugfixes since
then, and HHVM's implementation is a complete rewrite. So let's stop working
around alleged bugs.

To prevent errors resulting from HHVM code receiving serialize()d values when
it isn't expecting them, add a key suffix.

Change-Id: I4b2cf1715538aa3d9163787f43eb31984a380d35

includes/libs/objectcache/APCBagOStuff.php

index eaf1155..cc54fe7 100644 (file)
  * @ingroup Cache
  */
 class APCBagOStuff extends BagOStuff {
+
+       /**
+        * @var string String to append to each APC key. This may be changed
+        *  whenever the handling of values is changed, to prevent existing code
+        *  from encountering older values which it cannot handle.
+        **/
+       const KEY_SUFFIX = ':1';
+
        public function get( $key, &$casToken = null ) {
-               $val = apc_fetch( $key );
+               $val = apc_fetch( $key . self::KEY_SUFFIX );
 
                $casToken = $val;
 
-               if ( is_string( $val ) ) {
-                       if ( $this->isInteger( $val ) ) {
-                               $val = intval( $val );
-                       } else {
-                               $val = unserialize( $val );
-                       }
-               }
-
                return $val;
        }
 
        public function set( $key, $value, $exptime = 0 ) {
-               if ( !$this->isInteger( $value ) ) {
-                       $value = serialize( $value );
-               }
-
-               apc_store( $key, $value, $exptime );
+               apc_store( $key . self::KEY_SUFFIX, $value, $exptime );
 
                return true;
        }
 
        public function delete( $key ) {
-               apc_delete( $key );
+               apc_delete( $key . self::KEY_SUFFIX );
 
                return true;
        }
 
        public function incr( $key, $value = 1 ) {
-               return apc_inc( $key, $value );
+               return apc_inc( $key . self::KEY_SUFFIX, $value );
        }
 
        public function decr( $key, $value = 1 ) {
-               return apc_dec( $key, $value );
+               return apc_dec( $key . self::KEY_SUFFIX, $value );
        }
 }