Exemple #1
0
function test_space($testname, $data)
{
    $phplen = strlen(serialize($data));
    $igbinarylen = strlen(igbinary_serialize($data));
    printf("  php     : %5u\n", $phplen);
    printf("  igbinary: %5u (%.02f%%)\n", $igbinarylen, $igbinarylen * 100 / $phplen);
}
 /**
  * @param mixed $data Data to serialize
  * @param bool $useIgBinary Use igBinary extension if supported
  *
  * @return string
  */
 protected function phpSerialize($data, $useIgBinary = true)
 {
     if ($this->isIgBinarySupported() && $useIgBinary) {
         return igbinary_serialize($data);
     }
     return serialize($data);
 }
 /**
  * Freezes this cache backend.
  *
  * All data in a frozen backend remains unchanged and methods which try to add
  * or modify data result in an exception thrown. Possible expiry times of
  * individual cache entries are ignored.
  *
  * On the positive side, a frozen cache backend is much faster on read access.
  * A frozen backend can only be thawed by calling the flush() method.
  *
  * @return void
  * @throws \RuntimeException
  */
 public function freeze()
 {
     if ($this->frozen === true) {
         throw new \RuntimeException(sprintf('The cache "%s" is already frozen.', $this->cacheIdentifier), 1323353176);
     }
     $cacheEntryFileExtensionLength = strlen($this->cacheEntryFileExtension);
     for ($directoryIterator = new \DirectoryIterator($this->cacheDirectory); $directoryIterator->valid(); $directoryIterator->next()) {
         if ($directoryIterator->isDot()) {
             continue;
         }
         if ($cacheEntryFileExtensionLength > 0) {
             $entryIdentifier = substr($directoryIterator->getFilename(), 0, -$cacheEntryFileExtensionLength);
         } else {
             $entryIdentifier = $directoryIterator->getFilename();
         }
         $this->cacheEntryIdentifiers[$entryIdentifier] = true;
         $cacheEntryPathAndFilename = $this->cacheDirectory . $entryIdentifier . $this->cacheEntryFileExtension;
         $lock = new Lock($cacheEntryPathAndFilename);
         file_put_contents($cacheEntryPathAndFilename, $this->internalGet($entryIdentifier, false));
         $lock->release();
     }
     $cachePathAndFileName = $this->cacheDirectory . 'FrozenCache.data';
     $lock = new Lock($cachePathAndFileName);
     if ($this->useIgBinary === true) {
         file_put_contents($cachePathAndFileName, igbinary_serialize($this->cacheEntryIdentifiers));
     } else {
         file_put_contents($cachePathAndFileName, serialize($this->cacheEntryIdentifiers));
     }
     $lock->release();
     $this->frozen = true;
 }
Exemple #4
0
	protected function serialize ($value) {
		if ($this->igbinary) {
			return igbinary_serialize($value);
		}

		return serialize($value);
	}
 public function serialize($data)
 {
     if ($this->igbinary) {
         return igbinary_serialize($data);
     }
     return serialize($data);
 }
Exemple #6
0
function bench($value, $n = 1000000)
{
    $benchmark = new Benchmark();
    $benchmark->add('serialize', function () use(&$value) {
        serialize($value);
    });
    $benchmark->add('json_encode', function () use(&$value) {
        json_encode($value);
    });
    if (function_exists('bin_encode')) {
        $benchmark->add('bin_encode', function () use(&$value) {
            bin_encode($value);
        });
    }
    if (function_exists('bson_encode')) {
        $benchmark->add('bson_encode', function () use(&$value) {
            bson_encode($value);
        });
    }
    if (function_exists('msgpack_pack')) {
        $benchmark->add('msgpack_pack', function () use(&$value) {
            msgpack_pack($value);
        });
    }
    if (function_exists('igbinary_serialize')) {
        $benchmark->add('igbinary_serialize', function () use(&$value) {
            igbinary_serialize($value);
        });
    }
    $benchmark->add('var_export', function () use(&$value) {
        var_export($value, true);
    });
    $benchmark->setCount($n);
    $benchmark->run();
}
Exemple #7
0
 /**
  * Freezes this cache backend.
  *
  * All data in a frozen backend remains unchanged and methods which try to add
  * or modify data result in an exception thrown. Possible expiry times of
  * individual cache entries are ignored.
  *
  * On the positive side, a frozen cache backend is much faster on read access.
  * A frozen backend can only be thawed by calling the flush() method.
  *
  * @return void
  * @throws \RuntimeException
  */
 public function freeze()
 {
     if ($this->frozen === TRUE) {
         throw new \RuntimeException(sprintf('The cache "%s" is already frozen.', $this->cacheIdentifier), 1323353176);
     }
     $cacheEntryFileExtensionLength = strlen($this->cacheEntryFileExtension);
     for ($directoryIterator = new \DirectoryIterator($this->cacheDirectory); $directoryIterator->valid(); $directoryIterator->next()) {
         if ($directoryIterator->isDot()) {
             continue;
         }
         if ($cacheEntryFileExtensionLength > 0) {
             $entryIdentifier = substr($directoryIterator->getFilename(), 0, -$cacheEntryFileExtensionLength);
         } else {
             $entryIdentifier = $directoryIterator->getFilename();
         }
         $this->cacheEntryIdentifiers[$entryIdentifier] = TRUE;
         file_put_contents($this->cacheDirectory . $entryIdentifier . $this->cacheEntryFileExtension, $this->get($entryIdentifier));
     }
     if ($this->useIgBinary === TRUE) {
         file_put_contents($this->cacheDirectory . 'FrozenCache.data', igbinary_serialize($this->cacheEntryIdentifiers));
     } else {
         file_put_contents($this->cacheDirectory . 'FrozenCache.data', serialize($this->cacheEntryIdentifiers));
     }
     $this->frozen = TRUE;
 }
Exemple #8
0
 /**
  * Store data on the cache server.
  *
  * @param string       $key        Key we can use to retrieve the data.
  * @param string|array $data       Data to store on the cache server.
  * @param int          $expiration Time before the data expires on the cache server.
  *
  * @return bool Success/Failure.
  * @access public
  */
 public function set($key, $data, $expiration)
 {
     if ($this->connected === true && $this->ping() === true) {
         return $this->server->set($key, $this->isRedis ? $this->IgBinarySupport ? igbinary_serialize($data) : serialize($data) : $data, $expiration);
     }
     return false;
 }
 /**
  * @dataProvider getTestData
  */
 public function testSerialize($value, $serialized, $expected = null)
 {
     if (!extension_loaded('igbinary')) {
         $this->markTestSkipped('The igbinary extension must be loaded');
     }
     $serialized = igbinary_serialize($value);
     parent::testSerialize($value, $serialized, $expected);
 }
Exemple #10
0
 private function _tempData_serialize($data)
 {
     if (true === $this->_has_igbinary_status) {
         return igbinary_serialize($data);
     } else {
         return serialize($data);
     }
 }
 private static function serialize($o)
 {
     $igbinary = extension_loaded('igbinary');
     if ($igbinary) {
         return @igbinary_serialize($o);
     }
     return @serialize($o);
 }
/**
 * @param mixed $val
 * @return string
 */
function serialize($val)
{
    if (function_exists('igbinary_serialize')) {
        return \igbinary_serialize($val);
    } else {
        return \serialize($val);
    }
}
 private function _serialize($data)
 {
     if ($this->_has_igbinary) {
         return igbinary_serialize($data);
     } else {
         return serialize($data);
     }
 }
Exemple #14
0
 /**
  */
 public function unpack($data)
 {
     $out = igbinary_unserialize($data);
     if (!is_null($out) || $data == igbinary_serialize(null)) {
         return $out;
     }
     throw new Horde_Pack_Exception('Error when unpacking serialized data.');
 }
 /**
  * @TODO DESCR
  * @param $p
  */
 public function sendPacket($p)
 {
     if ($p === null) {
         return;
     }
     $data = \igbinary_serialize($p);
     $this->write(pack('N', mb_orig_strlen($data)) . $data);
 }
Exemple #16
0
 /**
  * 加密
  * @param $data 支持数据和字符串;
  * @return string 返回加密后的字符串
  */
 public function encrypt($data)
 {
     if (empty($data)) {
         return '';
     }
     $data = igbinary_serialize($data);
     $data = Yii::$app->getSecurity()->encryptByKey($data, self::$_key);
     return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
 }
Exemple #17
0
 /**
  * serialize
  *
  * @param mixed $value
  *
  * @return string
  */
 public function serialize($value)
 {
     if (function_exists('igbinary_serialize')) {
         return igbinary_serialize($value);
     } else {
         // fallback
         return serialize($value);
     }
 }
Exemple #18
0
 public static function pack($data)
 {
     if (function_exists("igbinary_serialize")) {
         $data = igbinary_serialize($data);
     } else {
         $data = serialize($data);
     }
     return self::PACKED_MARK . rtrim(base64_encode($data), "=");
 }
Exemple #19
0
 /**
  * Store cell data in cache for the current cell object if it's "dirty",
  *     and the 'nullify' the current cell object
  *
  * @return	void
  * @throws	PHPExcel_Exception
  */
 protected function _storeData()
 {
     if ($this->_currentCellIsDirty && !empty($this->_currentObjectID)) {
         $this->_currentObject->detach();
         $this->_cellCache[$this->_currentObjectID] = igbinary_serialize($this->_currentObject);
         $this->_currentCellIsDirty = false;
     }
     $this->_currentObjectID = $this->_currentObject = null;
 }
 /**
  * Serialize PHP value to igbinary
  *
  * @param  mixed $value
  * @param  array $opts
  * @return string
  * @throws Zend_Serializer_Exception on igbinary error
  */
 public function serialize($value, array $opts = array())
 {
     $ret = igbinary_serialize($value);
     if ($ret === false) {
         $lastErr = error_get_last();
         throw new Zend_Serializer_Exception($lastErr['message']);
     }
     return $ret;
 }
 public function testBasicSerializeUnserializeIgbinary()
 {
     $serializer = Serializer::create(Serializer::TYPE_IGBINARY);
     $dataOriginal = ['a', 'b', 'c'];
     $dataSerialized = $serializer->serialize($dataOriginal);
     $this->assertSame(igbinary_serialize($dataOriginal), $dataSerialized);
     $this->assertSame($dataOriginal, $serializer->unserialize($dataSerialized));
     $this->assertInstanceOf('SR\\Serializer\\Type\\SerializerTypeIgbinary', $serializer->getSerializer());
 }
Exemple #22
0
 /**
  * Serialize PHP value to igbinary
  *
  * @param  mixed $value
  * @param  array $opts
  * @return string
  * @throws Zend_Serializer_Exception on igbinary error
  */
 public function serialize($value, array $opts = array())
 {
     $ret = igbinary_serialize($value);
     if ($ret === false) {
         $lastErr = error_get_last();
         require_once LIB_DIR . '/Zend/Serializer/Exception.php';
         throw new Zend_Serializer_Exception($lastErr['message']);
     }
     return $ret;
 }
 /**
  * Serialize PHP value to igbinary
  *
  * @param  mixed $value
  * @return string
  * @throws Exception\RuntimeException on igbinary error
  */
 public function serialize($value)
 {
     ErrorHandler::start();
     $ret = igbinary_serialize($value);
     $err = ErrorHandler::stop();
     if ($ret === false) {
         throw new Exception\RuntimeException('Serialization failed', 0, $err);
     }
     return $ret;
 }
Exemple #24
0
 public function testGetParametersWithUnpacking()
 {
     if (!function_exists('igbinary_serialize')) {
         $this->markTestSkipped('This test requires igbinary PHP extension');
     }
     $checkString = 'packed code';
     $signatures = array('wonderfulClass' => igbinary_serialize($checkString));
     $definitions = array('wonderful' => 'wonderfulClass');
     $model = new \Magento\Framework\ObjectManager\Definition\Compiled\Binary(array($signatures, $definitions));
     $this->assertEquals($checkString, $model->getParameters('wonderful'));
 }
 /**
  * Sets a cache item to the given data, tags, and lifetime.
  *
  * @param   string   cache id to set
  * @param   string   data in the cache
  * @param   array    cache tags
  * @param   integer  lifetime
  * @return  bool
  */
 public function set($id, $data, array $tags = NULL, $lifetime)
 {
     // Remove old cache files
     $this->delete($id);
     // Cache File driver expects unix timestamp
     if ($lifetime !== 0) {
         $lifetime += time();
     }
     if (!empty($tags)) {
         // Convert the tags into a string list
         $tags = implode('+', $tags);
     }
     // Write out a serialized cache
     return (bool) file_put_contents($this->directory . $id . '~' . $tags . '~' . $lifetime . '.igbinary', igbinary_serialize($data));
 }
 /**
  * Implements DrupalCacheInterface::set().
  */
 function set($cid, $data, $expire = CACHE_PERMANENT)
 {
     $fields = array('serialized' => 0, 'created' => REQUEST_TIME, 'expire' => $expire);
     if (!is_string($data)) {
         $fields['data'] = igbinary_serialize($data);
         $fields['serialized'] = 1;
     } else {
         $fields['data'] = $data;
         $fields['serialized'] = 0;
     }
     try {
         db_merge($this->bin)->key(array('cid' => $cid))->fields($fields)->execute();
     } catch (Exception $e) {
         // The database may not be available, so we'll ignore cache_set requests.
     }
 }
 /**
  * Saves the value of a PHP variable in the cache. Note that the variable
  * will be serialized if necessary.
  *
  * @param string $entryIdentifier An identifier used for this cache entry
  * @param mixed $variable The variable to cache
  * @param array $tags Tags to associate with this cache entry
  * @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, the default lifetime is used. "0" means unlimited lifetime.
  * @return void
  * @throws \InvalidArgumentException
  * @api
  */
 public function set($entryIdentifier, $variable, array $tags = [], $lifetime = null)
 {
     if (!$this->isValidEntryIdentifier($entryIdentifier)) {
         throw new \InvalidArgumentException('"' . $entryIdentifier . '" is not a valid cache entry identifier.', 1233058264);
     }
     foreach ($tags as $tag) {
         if (!$this->isValidTag($tag)) {
             throw new \InvalidArgumentException('"' . $tag . '" is not a valid tag for a cache entry.', 1233058269);
         }
     }
     if ($this->useIgBinary === true) {
         $this->backend->set($entryIdentifier, igbinary_serialize($variable), $tags, $lifetime);
     } else {
         $this->backend->set($entryIdentifier, serialize($variable), $tags, $lifetime);
     }
 }
Exemple #28
0
 /**
  * 数据编码。
  * 
  * @param array $data  指定数据对象。
  * @param int $format  指定编码格式。(默认值: 1 | JSON, 其它可用值: Encoder::MSGPACK, Encoder::XML)
  * @param int $options 指定参数选项。(注: 此参数仅对 JSON 编码有效.)
  * @return string
  * @throws NotImplementedException
  */
 static function encode($data, $format = self::JSON, $options = 320)
 {
     $s = false;
     switch ($format) {
         case self::JSON:
             $s = json_encode($data, $options);
             break;
         case self::MSGPACK:
             $s = msgpack_serialize($data);
             break;
         case self::IGBINARY:
             $s = igbinary_serialize($data);
             break;
         case self::XML:
             throw new NotImplementedException('尚未实现此接口。', -1);
             break;
     }
     return $s;
 }
Exemple #29
0
 public static function initForServiceIntf($path, $encoding = 'gbk')
 {
     /*{{{*/
     ob_start();
     mb_internal_encoding($encoding);
     $configFile = $path . '/config/config.php';
     include $configFile;
     if (isset($configs['includePath'])) {
         set_include_path($configs['includePath']);
     }
     DBC::requireNotEmptyString($configs['system'], '未知系统code,请检查config中是否配置了system项');
     DAL::get()->setUp($configs['system']);
     $serviceChannel = new ServiceChannel(true);
     $result = $serviceChannel->run($configs);
     ob_clean();
     //var_dump($result);exit;
     echo igbinary_serialize($result);
     ob_end_flush();
 }
Exemple #30
0
 public function serialize($data)
 {
     $serialize = strtolower($this->get('serialize'));
     if ('igbinary' == $serialize) {
         return igbinary_serialize($data);
     }
     if ('json' == $serialize) {
         return json_encode($data);
     }
     if ('php' == $serialize) {
         return serialize($data);
     }
     if (extension_loaded('igbinary')) {
         return igbinary_serialize($data);
     }
     if (extension_loaded('json')) {
         return json_encode($data);
     }
     return serialize($data);
 }