$item = array(
'id' => $itemId,
'slug' => 'lenovo-g470-core-i5-VGA-onboard',
'name' => 'Lenovo G470',
'price' => 550,
'buy_count' => 345,
'buyer' => array(
'total' => 7,
'data' => array(
array('id' => 'c134', 'name' => 'Sharone Stone'),
array('id' => 'c245', 'name' => 'Jane Ling'),
array('id' => 'c311', 'name' => 'John Tahee')
)
)
);
black color: static information
red color: dynamic information
How to cache product information with Redis?
command: hmset
$item = array(
'id' => $itemId,
'slug' => 'lenovo-g470-core-i5-VGA-onboard',
'name' => 'Lenovo G470',
'price' => 550,
'buy_count' => 345,
'buyer' => serialize(array(
'total' => 7,
'data' => array(
array('id' => 'c134', 'name' => 'Sharone Stone'),
array('id' => 'c245', 'name' => 'Jane Ling'),
array('id' => 'c311', 'name' => 'John Tahee')
)
))
);
$key = 'product:' . $item['id'];
$redis->hmset($key, $item);
$oneday = 24 * 60 * 60;
$redis->expire($key, $oneday);
$hashsetValue = $redis->hgetall($key); //
if (!empty($hashsetValue)) {
$hashsetValue['buyer'] = unserialize($hashsetValue['buyer']);
}
# when product is bought by customers
$arrUpdateInfo = array(
'buy_count' => 349,
'buyer' => serialize(array(
'total' => 12,
'data' => array(
array('id' => 'c134', 'name' => 'Leak Cody'),
array('id' => 'c245', 'name' => 'Line Changun'),
array('id' => 'c311', 'name' => 'Sim Sukzi')
)
))
);
$redis->hmset($key, $arrUpdateInfo);
Title:
Use Redis to cache product information
Description:
$item = array( 'id' => $itemId, 'slug' => 'lenovo-g470-core-i5-VGA-onboard', 'name' => ...
...
Rating:
4