Skip to content

Commit

Permalink
Allow storage of values that evaluate to false (empty array, numberic…
Browse files Browse the repository at this point in the history
… zero, boolean false, etc) - #6
  • Loading branch information
Jordan Hall committed Mar 13, 2017
1 parent 082e231 commit 8ec1835
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/RWFileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,14 @@ public function get($key)
if ($cacheObj->expiryTimestamp > time() || $unixLoad[0] >= $this->config['unixLoadUpperThreshold']) {
// Cache item has not yet expired or system load is too high
$content = $cacheObj->content;
if ($unserializedContent = @unserialize($content)) {

if (($unserializedContent = @unserialize($content))!==false) {
// Normal unserialization
$content = $unserializedContent;

} elseif ($content==serialize(false)) {
// Edge case to handle boolean false being stored
$content = false;
}

return $content;
Expand Down
28 changes: 26 additions & 2 deletions src/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,32 @@

$cache1->changeConfig(['cacheDirectory' => '/tmp/rwFileCacheStorage/']);

$cache1->set('first.test.stringContent', 'Mary had a little lamb.', strtotime('+ 1 day'));
// Test basic string storage and retrieval
$key = 'first.test.stringContent';
$cache1->set($key, 'Mary had a little lamb.', strtotime('+ 1 day'));
$var = $cache1->get($key);
var_dump($var);

// Test empty array storage and retrieval
$key = 'second.test.emptyArray';
$cache1->set($key, [], strtotime('+ 1 day'));
$var = $cache1->get($key);
var_dump($var);

$var = $cache1->get('first.test.stringContent');
// Test numeric zero storage and retrieval
$key = 'third.test.numericZero';
$cache1->set($key, 0, strtotime('+ 1 day'));
$var = $cache1->get($key);
var_dump($var);

// Test boolean false storage and retrieval
$key = 'fourth.test.booleanFalse';
$cache1->set($key, false, strtotime('+ 1 day'));
$var = $cache1->get($key);
var_dump($var);

// Test boolean true storage and retrieval
$key = 'fifth.test.booleanTrue';
$cache1->set($key, true, strtotime('+ 1 day'));
$var = $cache1->get($key);
var_dump($var);

0 comments on commit 8ec1835

Please sign in to comment.