Over a million developers have joined DZone.

Saving Objects In Redis And Php

// 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);

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}