Skip to content

Commit 8ec1835

Browse files
author
Jordan Hall
committed
Allow storage of values that evaluate to false (empty array, numberic zero, boolean false, etc) - #6
1 parent 082e231 commit 8ec1835

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/RWFileCache.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,14 @@ public function get($key)
112112
if ($cacheObj->expiryTimestamp > time() || $unixLoad[0] >= $this->config['unixLoadUpperThreshold']) {
113113
// Cache item has not yet expired or system load is too high
114114
$content = $cacheObj->content;
115-
if ($unserializedContent = @unserialize($content)) {
115+
116+
if (($unserializedContent = @unserialize($content))!==false) {
117+
// Normal unserialization
116118
$content = $unserializedContent;
119+
120+
} elseif ($content==serialize(false)) {
121+
// Edge case to handle boolean false being stored
122+
$content = false;
117123
}
118124

119125
return $content;

src/test.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,32 @@
66

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

9-
$cache1->set('first.test.stringContent', 'Mary had a little lamb.', strtotime('+ 1 day'));
9+
// Test basic string storage and retrieval
10+
$key = 'first.test.stringContent';
11+
$cache1->set($key, 'Mary had a little lamb.', strtotime('+ 1 day'));
12+
$var = $cache1->get($key);
13+
var_dump($var);
14+
15+
// Test empty array storage and retrieval
16+
$key = 'second.test.emptyArray';
17+
$cache1->set($key, [], strtotime('+ 1 day'));
18+
$var = $cache1->get($key);
19+
var_dump($var);
1020

11-
$var = $cache1->get('first.test.stringContent');
21+
// Test numeric zero storage and retrieval
22+
$key = 'third.test.numericZero';
23+
$cache1->set($key, 0, strtotime('+ 1 day'));
24+
$var = $cache1->get($key);
25+
var_dump($var);
26+
27+
// Test boolean false storage and retrieval
28+
$key = 'fourth.test.booleanFalse';
29+
$cache1->set($key, false, strtotime('+ 1 day'));
30+
$var = $cache1->get($key);
31+
var_dump($var);
1232

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

0 commit comments

Comments
 (0)