iron_cache_php icon indicating copy to clipboard operation
iron_cache_php copied to clipboard

Allow seamless storage of arrays and objects within IronCache.

Open Sam152 opened this issue 12 years ago • 0 comments

I'm not sure if this is out of scope for this project or not, but it might be handy to allow storage and retrieval of arrays and objects. The following code snippet extends IronCache to add this functionality, however there are a few limitations:

  • Storing strings that are valid serial objects/arrays would come out as an array or object.
  • The detection for a serialised variable is a bit dirty.

Perhaps this should only exist as a subclass that can be used as necessary or maybe the methods should be refactored into "putSerialized" and "getSerialized".

class FlexIronCache extends IronCache {
  public function put($key, $item){
    if (is_array($item) || is_object($item)) {
      $item = serialize($item);
    }
    return $this->putItem($this->cache_name, $key, $item);
  }
  public function get($key){
    $val = $this->getItem($this->cache_name, $key);
    $unserialised = @unserialize($val->value);
    if ($unserialised !== FALSE) {
      $val->value = $unserialised;
    }
    return $val;
  }
}

Sam152 avatar Sep 01 '13 06:09 Sam152