From b3ca6226ca1ab65ba8faa7a848bc8eae9c24c9df Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Mon, 22 Nov 2004 01:33:47 +0000 Subject: [PATCH] Use compression in the objectcache table if zlib support is available. Since we'll be using this for the parser cache when memcached is not set up, we'll want to actually put junk in there and saving space is nice. --- includes/ObjectCache.php | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/includes/ObjectCache.php b/includes/ObjectCache.php index 7e182d8d21..7689b9442b 100644 --- a/includes/ObjectCache.php +++ b/includes/ObjectCache.php @@ -232,7 +232,7 @@ class SqlBagOStuff extends BagOStuff { } if($row=$this->_fetchobject($res)) { $this->_debug("get: retrieved data; exp time is " . $row->exptime); - return unserialize($row->value); + return $this->_unserialize($row->value); } else { $this->_debug('get: no matching rows'); } @@ -252,7 +252,7 @@ class SqlBagOStuff extends BagOStuff { $this->delete( $key ); $this->_query( "INSERT INTO $0 (keyname,value,exptime) VALUES('$1','$2','$exp')", - $key, serialize($value)); + $key, $this->_serialize($value)); return true; /* ? */ } @@ -324,6 +324,38 @@ class SqlBagOStuff extends BagOStuff { /* Clear *all* items from cache table */ $this->_query( "DELETE FROM $0" ); } + + /** + * Serialize an object and, if possible, compress the representation. + * On typical message and page data, this can provide a 3X decrease + * in storage requirements. + * + * @param mixed $data + * @return string + */ + function _serialize( &$data ) { + $serial = serialize( $data ); + if( function_exists( 'gzdeflate' ) ) { + return gzdeflate( $serial ); + } else { + return $serial; + } + } + + /** + * Unserialize and, if necessary, decompress an object. + * @param string $serial + * @return mixed + */ + function &_unserialize( $serial ) { + if( function_exists( 'gzinflate' ) ) { + $decomp = @gzinflate( $serial ); + if( false !== $decomp ) { + $serial = $decomp; + } + } + return unserialize( $serial ); + } } /** -- 2.20.1