Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'marc-mabe/cache_rmUtils'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 573 deletions.
79 changes: 6 additions & 73 deletions src/Storage/Adapter/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
Zend\Cache\Storage\OptimizableInterface,
Zend\Cache\Storage\TaggableInterface,
Zend\Cache\Storage\TotalSpaceCapableInterface,
Zend\Cache\Utils,
Zend\Stdlib\ErrorHandler;

/**
Expand All @@ -48,7 +47,7 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Filesystem extends AbstractAdapter implements
class Filesystem extends AbstractAdapter implements
AvailableSpaceCapableInterface,
ClearByNamespaceInterface,
ClearByPrefixInterface,
Expand Down Expand Up @@ -177,11 +176,6 @@ public function clearExpired()
if (file_exists($tagPathname)) {
unlink($tagPathname);
}

$ifoPathname = substr($pathname, 0, -4) . '.ifo';
if (file_exists($ifoPathname)) {
unlink($ifoPathname);
}
}
}
$error = ErrorHandler::stop();
Expand Down Expand Up @@ -353,11 +347,6 @@ public function clearByTags(array $tags, $disjunction = false)
if (file_exists($datPathname)) {
unlink($datPathname);
}

$ifoPathname = substr($pathname, 0, -4) . '.ifo';
if (file_exists($ifoPathname)) {
unlink($ifoPathname);
}
}
}

Expand Down Expand Up @@ -531,17 +520,6 @@ protected function internalGetItem(& $normalizedKey, & $success = null, & $casTo
$filespec = $this->getFileSpec($normalizedKey);
$data = $this->getFileContent($filespec . '.dat');

if ($this->getOptions()->getReadControl()) {
if ( ($info = $this->readInfoFile($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"
);
}
}

// use filemtime + filesize as CAS token
if (func_num_args() > 2) {
$casToken = filemtime($filespec . '.dat') . filesize($filespec . '.dat');
Expand Down Expand Up @@ -588,17 +566,6 @@ protected function internalGetItems(array & $normalizedKeys)
unset($keys[$i]);
}

if ($options->getReadControl()) {
$info = $this->readInfoFile($filespec . '.ifo');
if (isset($info['hash'], $info['algo'])
&& Utils::generateHash($info['algo'], $data, true) != $info['hash']
) {
throw new Exception\UnexpectedValueException(
"ReadControl: Stored hash and computed hash doesn't match"
);
}
}

$result[$key] = $data;
}

Expand Down Expand Up @@ -928,36 +895,13 @@ protected function internalSetItem(& $normalizedKey, & $value)
$filespec = $this->getFileSpec($normalizedKey);
$this->prepareDirectoryStructure($filespec);

$info = null;
if ($options->getReadControl()) {
$info['hash'] = Utils::generateHash($options->getReadControlAlgo(), $value, true);
$info['algo'] = $options->getReadControlAlgo();
}

// write files
try {
// set umask for files
$oldUmask = umask($options->getFileUmask());

$contents = array($filespec . '.dat' => & $value);
if ($info) {
$contents[$filespec . '.ifo'] = serialize($info);
} else {
$this->unlink($filespec . '.ifo');
$this->unlink($filespec . '.tag');
}

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]);
}
}
}
$this->putFileContent($filespec . '.dat', $value);
$this->unlink($filespec . '.tag');

// reset file_umask
umask($oldUmask);
Expand Down Expand Up @@ -992,18 +936,8 @@ protected function internalSetItems(array & $normalizedKeyValuePairs)
// *.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');
}
// *.tag file
$this->unlink($filespec . '.tag');
}

// write to disk
Expand Down Expand Up @@ -1213,7 +1147,6 @@ protected function internalRemoveItem(& $normalizedKey)
} else {
$this->unlink($filespec . '.dat');
$this->unlink($filespec . '.tag');
$this->unlink($filespec . '.ifo');
}
return true;
}
Expand Down Expand Up @@ -1259,7 +1192,7 @@ protected function internalGetCapabilities()
'staticTtl' => false,
'ttlPrecision' => 1,
'expiredRead' => true,
'maxKeyLength' => 251, // 255 - strlen(.dat | .ifo)
'maxKeyLength' => 251, // 255 - strlen(.dat | .tag)
'namespaceIsPrefix' => true,
'namespaceSeparator' => $options->getNamespaceSeparator(),
)
Expand Down
73 changes: 1 addition & 72 deletions src/Storage/Adapter/FilesystemOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

namespace Zend\Cache\Storage\Adapter;

use Zend\Cache\Exception,
Zend\Cache\Utils;
use Zend\Cache\Exception;

/**
* These are options specific to the Filesystem adapter
Expand Down Expand Up @@ -109,22 +108,6 @@ class FilesystemOptions extends AdapterOptions
*/
protected $noCtime = true;

/**
* Read control enabled ?
*
* If enabled a hash (readControlAlgo) will be saved and check on read.
*
* @var boolean
*/
protected $readControl = false;

/**
* The used hash algorithm if read control is enabled
*
* @var string
*/
protected $readControlAlgo = 'crc32';

/**
* Set cache dir
*
Expand Down Expand Up @@ -442,60 +425,6 @@ public function getNoCtime()
return $this->noCtime;
}

/**
* Set read control
*
* @param bool $flag
* @return FilesystemOptions
*/
public function setReadControl($flag)
{
$flag = (bool) $flag;
$this->triggerOptionEvent('read_control', $flag);
$this->readControl = $flag;
return $this;
}

/**
* Get read control
*
* @return bool
*/
public function getReadControl()
{
return $this->readControl;
}

/**
* 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;
}

/**
* Get read control algo
*
* @return string
*/
public function getReadControlAlgo()
{
return $this->readControlAlgo;
}

/**
* Normalize a umask and optionally apply a callback to it
*
Expand Down
5 changes: 2 additions & 3 deletions src/Storage/Adapter/Memory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
Zend\Cache\Storage\IterableInterface,
Zend\Cache\Storage\TaggableInterface,
Zend\Cache\Storage\AvailableSpaceCapableInterface,
Zend\Cache\Storage\TotalSpaceCapableInterface,
Zend\Cache\Utils;
Zend\Cache\Storage\TotalSpaceCapableInterface;

/**
* @category Zend
Expand All @@ -41,7 +40,7 @@
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Memory extends AbstractAdapter implements
class Memory extends AbstractAdapter implements
AvailableSpaceCapableInterface,
ClearByPrefixInterface,
ClearExpiredInterface,
Expand Down
67 changes: 51 additions & 16 deletions src/Storage/Adapter/MemoryOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

namespace Zend\Cache\Storage\Adapter;

use Zend\Cache\Utils,
Zend\Cache\Exception;
use Zend\Cache\Exception;

/**
* These are options specific to the APC adapter
Expand All @@ -45,27 +44,24 @@ class MemoryOptions extends AdapterOptions
/**
* Set memory limit
*
* - Bytes of less or equal 0 will disable the memory limit
* - A number less or equal 0 will disable the memory limit
* - When a number is used, the value is measured in bytes. Shorthand notation may also be used.
* - If the used memory of PHP exceeds this limit an OutOfSpaceException
* will be thrown.
*
* @param int $bytes
* @link http://php.net/manual/faq.using.php#faq.using.shorthandbytes
* @param string|int $memoryLimit
* @return MemoryOptions
*/
public function setMemoryLimit($bytes)
public function setMemoryLimit($memoryLimit)
{
$bytes = (int) $bytes;
$memoryLimit = $this->normalizeMemoryLimit($memoryLimit);

// use PHP's memory limit if possible
if ($bytes <= 0) {
$bytes = Utils::bytesFromString(ini_get('memory_limit'));
if ($bytes <= 0) {
$bytes = 0;
}
if ($this->memoryLimit != $memoryLimit) {
$this->triggerOptionEvent('memory_limit', $memoryLimit);
$this->memoryLimit = $memoryLimit;
}

$this->triggerOptionEvent('memory_limit', $bytes);
$this->memoryLimit = $bytes;
return $this;
}

Expand All @@ -81,9 +77,9 @@ 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'));
$memoryLimit = $this->normalizeMemoryLimit(ini_get('memory_limit'));
if ($memoryLimit >= 0) {
$this->memoryLimit = floor($memoryLimit / 2);
$this->memoryLimit = (int)($memoryLimit / 2);
} else {
// disable memory limit
$this->memoryLimit = 0;
Expand All @@ -92,4 +88,43 @@ public function getMemoryLimit()

return $this->memoryLimit;
}

/**
* Normalized a given value of memory limit into the number of bytes
*
* @param string|int $value
* @return int
*/
protected function normalizeMemoryLimit($value)
{
if (is_numeric($value)) {
return (int)$value;
}

if (!preg_match('/(\-?\d+)\s*(\w*)/', ini_get('memory_limit'), $matches)) {
throw new Exception\InvalidArgumentException("Invalid memory limit '{$value}'");
}

$value = (int)$matches[1];
if ($value <= 0) {
return 0;
}

switch (strtoupper($matches[2])) {
case 'G':
$value*= 1024;
// Break intentionally omitted

case 'M':
$value*= 1024;
// Break intentionally omitted

case 'K':
$value*= 1024;
// Break intentionally omitted
}

return $value;
}

}
Loading

0 comments on commit ab2b436

Please sign in to comment.