Skip to content

Allow seamless storage of arrays and objects within IronCache. #7

@Sam152

Description

@Sam152

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;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions