DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Saving Objects In Redis And Php

12.07.2011
| 3961 views |
  • submit to reddit
        // 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);
  }
}