Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Serialization Alternative
    text
    copied!<p>I'm looking for a good cache key for APC that represents some complied information about an object, using the "object" as the key. I have a compilation method that does something like this:</p> <pre><code>function compile(Obj $obj) { if ($this-&gt;cache) { $cachekey = serialize($obj); if ($data = $this-&gt;cache-&gt;get($obj)) { return $data } } // compute result here if ($this-&gt;cache) { $this-&gt;cache-&gt;set($cachekey, $result); } return $result; } </code></pre> <p>If it's not obvious, <code>$this-&gt;cache</code> is an implementation of an interface with the methods <code>get</code> and <code>set</code>. </p> <p>Is there a quicker alternative to creating a key that's unique to <em>some</em> of the properties of this object? I can extract the relevant bits out, but then they are still arrays, which would have the same problem with serialization that I had with the objects in the first place. </p> <p>Serialize works, from a "correctness" position, but it seems wasteful (both in size of outputted key, and in computational complexity). </p> <p>EDIT: I would also like to add, if it's not obvious, that I will <em>not</em> be needing to unserialize this object. My verbatim code for the current cache key is actually: </p> <p><code>$cachekey = 'compile.' . sha1(serialize($obj));</code>. </p> <p>EDIT 2: The object I'm working with has the following definition:</p> <pre><code>class Route { protected $pattern; protected $defaults = array(); protected $requirements = array(); } </code></pre> <p><code>Pattern</code> and <code>requirements</code> are the values of the object that will change the output of this method, therefore a hash of these values must be present in the cache key. </p> <p>Also, someone suggested uniqid(), which would defeat the purpose of a general cache lookup key, as you could not reliably regenerate the same ID from the same information.</p> <p>EDIT 3: I guess I'm not giving enough context. Here's a link to the code so far: </p> <p><a href="https://github.com/efritz/minuet/blob/master/src/Minuet/Routing/Router.php#L160" rel="nofollow">https://github.com/efritz/minuet/blob/master/src/Minuet/Routing/Router.php#L160</a></p> <p>I guess I'm really only trying to avoid expensive calls to serialize (and I guess sha1, which is also a bit expensive). It's possible that the best I can do is try to reduce the size of what I'm serializing...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload