Saving Objects In Redis And Php
Join the DZone community and get the full member experience.
Join For Free// A fairly generic method to store arrays and also to add them to a pool to reverse lookup their ids based on values that they contain. This method extends my own redis client but will work for the better clients out there such as predis.
class storage extends redis {
public function save($key, array $object, $timestamp=true){
$timestamp && $object['timestamp'] = date('Ymdhis');
$id = $this->incr('id:'.$key);
foreach ($object as $k => $v) {
$this->sadd(sprintf("%s:%s:%s", $key, $k, $v), $id);
}
$key = sprintf("%s:%s", $key, $id);
$this->hmset($key, $object);
}
}
Redis (company)
Object (computer science)
Opinions expressed by DZone contributors are their own.
Comments