/** * Get memory limit * * If the used memory of PHP exceeds this limit an OutOfCapacityException * will be thrown. * * @return int */ public function getMemoryLimit() { if ($this->memoryLimit === null) { $memoryLimit = Utils::bytesFromString(ini_get('memory_limit')); if ($memoryLimit >= 0) { $this->memoryLimit = floor($memoryLimit / 2); } else { // use a hard memory limit of 32M if php memory limit is disabled $this->memoryLimit = 33554432; } } return $this->memoryLimit; }
/** * Get memory limit * * If the used memory of PHP exceeds this limit an OutOfCapacityException * will be thrown. * * @return int */ public function getMemoryLimit() { if ($this->memoryLimit === null) { $memoryLimit = Utils::bytesFromString(ini_get('memory_limit')); if ($memoryLimit >= 0) { $this->memoryLimit = floor($memoryLimit / 2); } else { // disable memory limit $this->memoryLimit = 0; } } return $this->memoryLimit; }
/** * Get storage capacity. * * @param array $options * @return array|boolean Capacity as array or false on failure * * @triggers getCapacity.pre(PreEvent) * @triggers getCapacity.post(PostEvent) * @triggers getCapacity.exception(ExceptionEvent) */ public function getCapacity(array $options = array()) { $args = new ArrayObject(array('options' => &$options)); try { $eventRs = $this->triggerPre(__FUNCTION__, $args); if ($eventRs->stopped()) { return $eventRs->last(); } $result = Utils::getDiskCapacity(ini_get('zend_datacache.disk.save_path')); return $this->triggerPost(__FUNCTION__, $args, $result); } catch (\Exception $e) { return $this->triggerException(__FUNCTION__, $args, $e); } }
/** * Get memory limit * * If the used memory of PHP exceeds this limit an OutOfSpaceException * will be thrown. * * @return int */ public function getMemoryLimit() { if ($this->memoryLimit === null) { // By default use half of PHP's memory limit if possible $memoryLimit = Utils::bytesFromString(ini_get('memory_limit')); if ($memoryLimit >= 0) { $this->memoryLimit = floor($memoryLimit / 2); } else { // disable memory limit $this->memoryLimit = 0; } } return $this->memoryLimit; }
/** * Set real control algo * * @param string $algo * @return FilesystemOptions * @throws Exception\InvalidArgumentException */ public function setReadControlAlgo($algo) { $algo = strtolower($algo); if (!in_array($algo, Utils::getHashAlgos())) { throw new Exception\InvalidArgumentException("Unsupported hash algorithm '{$algo}"); } $this->triggerOptionEvent('read_control_algo', $algo); $this->readControlAlgo = $algo; return $this; }
/** * Internal method to get storage capacity. * * @param array $normalizedOptions * @return array|boolean Associative array of capacity, false on failure * @throws Exception\ExceptionInterface */ protected function internalGetCapacity(array & $normalizedOptions) { return Utils::getDiskCapacity(ini_get('zend_datacache.disk.save_path')); }
/** * Get by key * * @param $key * @param array $options * @return bool|string * @throws Exception\ItemNotFoundException|Exception\UnexpectedValueException */ protected function internalGetItem($key, array &$options) { if (!$this->internalHasItem($key, $options) || !($keyInfo = $this->getKeyInfo($key, $options['namespace']))) { if ($options['ignore_missing_items']) { return false; } else { throw new Exception\ItemNotFoundException("Key '{$key}' not found within namespace '{$options['namespace']}'"); } } $baseOptions = $this->getOptions(); try { $data = $this->getFileContent($keyInfo['filespec'] . '.dat'); if ($baseOptions->getReadControl()) { if (($info = $this->readInfoFile($keyInfo['filespec'] . '.ifo')) && isset($info['hash'], $info['algo']) && Utils::generateHash($info['algo'], $data, true) != $info['hash']) { throw new Exception\UnexpectedValueException("ReadControl: Stored hash and computed hash don't match"); } } return $data; } catch (Exception $e) { try { // remove cache file on exception $this->internalRemoveItem($key, $options); } catch (Exception $tmp) { } // do not throw remove exception on this point throw $e; } }
/** * Internal method to get storage capacity. * * @param array $normalizedOptions * @return array|boolean Capacity as array or false on failure * @throws Exception\ExceptionInterface */ protected function internalGetCapacity(array &$normalizedOptions) { return Utils::getDiskCapacity($this->getOptions()->getCacheDir()); }
public function testGenerateHashStrlenRaw() { $this->assertEquals(pack('l', strlen('test')), Utils::generateHash('strlen', 'test', true)); }
/** * Internal method to store multiple items. * * @param array $normalizedKeyValuePairs * @return array Array of not stored keys * @throws Exception\ExceptionInterface */ protected function internalSetItems(array &$normalizedKeyValuePairs) { $baseOptions = $this->getOptions(); $oldUmask = null; // create an associated array of files and contents to write $contents = array(); foreach ($normalizedKeyValuePairs as $key => &$value) { $filespec = $this->getFileSpec($key); $this->prepareDirectoryStructure($filespec); // *.dat file $contents[$filespec . '.dat'] =& $value; // *.ifo file $info = null; if ($baseOptions->getReadControl()) { $info['hash'] = Utils::generateHash($baseOptions->getReadControlAlgo(), $value, true); $info['algo'] = $baseOptions->getReadControlAlgo(); } if ($info) { $contents[$filespec . '.ifo'] = serialize($info); } else { $this->unlink($filespec . '.ifo'); $this->unlink($filespec . '.tag'); } } // write to disk try { // set umask for files $oldUmask = umask($baseOptions->getFileUmask()); while ($contents) { $nonBlocking = count($contents) > 1; $wouldblock = null; foreach ($contents as $file => &$content) { $this->putFileContent($file, $content, $nonBlocking, $wouldblock); if (!$nonBlocking || !$wouldblock) { unset($contents[$file]); } } } // reset umask umask($oldUmask); // return OK return array(); } catch (BaseException $e) { // reset umask on exception umask($oldUmask); throw $e; } }
/** * Get storage capacity. * * @param array $options * @return array|boolean Capacity as array or false on failure * @throws Exception * * @triggers getCapacity.pre(PreEvent) * @triggers getCapacity.post(PostEvent) * @triggers getCapacity.exception(ExceptionEvent) */ public function getCapacity(array $options = array()) { $args = new ArrayObject(array('options' => &$options)); $eventRs = $this->triggerPre(__FUNCTION__, $args); if ($eventRs->stopped()) { return $eventRs->last(); } $result = Utils::getPhpMemoryCapacity(); return $this->triggerPost(__FUNCTION__, $args, $result); }