/** * Write the item_id => total quantity pairing into the cache. * @param int item id * @param int total count, after db write * if the total is less than zero, write it in as undefined in the cache so it doesn't hit the * db again. With tally, the total is a number, but the serial total will be a quantity object. * no matter. we can get it's total value easily enough by just getting strval of it. * if there is a transaction attached, set up a callback to delete this key if the transaction doesn't work. * can't delete the cache key for history since variations are endless, but we can bust the key * using last touch. if we aren't in a transaction, just bust last touch right away. * check the id index so we can update it in the cache. * if nothing left of a given item, remove it from the index. * if the item id is in the index, no more work needed. * make sure the index is sorted the way it is supposed to be. * always sorted in numeric order of item id. */ protected function writeToCache($item_id, $total) { $cache = $this->cacher; $timeout = $this->cacheTimeout(); $cache->set($item_id, Base::quantify($total) > 0 ? $total : Store\Callback::UNDEF, $timeout); if ($this->inTran()) { Transaction::onRollback(array($cache, 'delete'), array($item_id)); Transaction::onRollback(array($this, 'lastTouch'), array(TRUE)); Transaction::onCommit(array($this, 'lastTouch'), array(TRUE)); } else { $this->lastTouch(TRUE); } $index = $cache->get(self::INDEX_CACHEKEY); if (!is_array($index)) { return; } if (Base::quantify($total) > 0) { if (in_array($item_id, $index)) { return; } $index[] = $item_id; sort($index, SORT_NUMERIC); } else { $found = array_keys($index, $item_id); if (count($found) < 1) { return; } foreach ($found as $k) { unset($index[$k]); } } $cache->set(self::INDEX_CACHEKEY, $index, $timeout); }