Update:
Per suggestion from the accepted answer below, I tested reading a negative number:
$negativeInt = -1;
Cache::write('my-test-count', $negativeInt, 'short');
$readVal = Cache::read('my-test-count', 'short');
debug($readVal);
exit;
The unseralize error reproduces consistently when trying to read any negative number. It's now an accepted bug, which I assume will be resolved by 2.8.1
Original Question:
I keep getting this error, but can't figure out why or even how to further troubleshoot.
The line that throws the error should only get hit if Cache::read()
returns false. But, that line would throw an unserialize error itself if I didn't put the @ before it.
Question:
How can I reliably use Redis for counts without getting unserialize Notices on a regular basis? If the data in the key is "bad", how can I know that without getting the Notice just from doing a ::read
. I've tried making sure my data is (int) (see below), but that doesn't seem to help.
Notice (8): unserialize(): Error at offset 0 of 2 bytes [APP/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Cache/Engine/RedisEngine.php, line 136]
Upon inspection of the error:
> unserialize - [internal], line ??
> RedisEngine::read() - APP/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Cache/Engine/RedisEngine.php, line 136
> Cache::read() - APP/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Cache/Cache.php, line 358
> Cache::remember() - APP/Vendor/pear-pear.cakephp.org/CakePHP/Cake/Cache/Cache.php, line 567
> Item::getCount() - APP/Model/Item.php, line 812
It appears to be coming from this function:
public function getCount($id) {
$model = $this;
// here is where the Cache::read() and debug are in the update below
return Cache::remember('item' . $id. '_count', function() use ($model, $id) {
$count = $model->find('count', array(
'conditions' => array(
$model->alias . '.status' => 1,
$model->alias . '.id' => $id
)
));
return ($count === false) ? 0 : (int)$count;
}, 'my_counts'); // THIS IS LINE 812
}
public function decrementCount($id, $offset = 1) {
if(empty($id)) return false;
$count = @Cache::read('item' . $id . '_count', 'my_counts');
if($count === false) {
$this->getCount($id);
} else {
Cache::decrement('item' . $id . '_count', $offset, 'my_counts');
}
}
public function incrementCount($id, $offset = 1) {
if(empty($id)) return false;
$count = @Cache::read('item' . $id. '_count', 'my_counts');
if($count === false) {
$this->getCount($id);
} else {
Cache::increment('item' . $id. '_count', $offset, 'my_counts');
}
}
UPDATE:
This function gets run in a loop (through 1-20 items). When I add the following before the Cache::remember(...
:
$toReturn = Cache::read('item' . $id. '_count', 'my_counts');
debug($toReturn);
It gives this:
- debug: (int) 0
- debug: (int) 0
- Notice (8): unserialize(): Error at... (from the Cache::read)
- debug: false
- Notice (8): unserialize(): Error at... (from the Cache::remember
- debug: (int) 0
- Notice (8): unserialize(): Error at... (from the Cache::read)
- debug: false
- Notice (8): unserialize(): Error at... (from the Cache::remember
- 0
- 3
- 1
- 1 ...