Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6724635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:42:48+00:00 2026-05-26T09:42:48+00:00

I’m looking for a good cache key for APC that represents some complied information

  • 0

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:

function compile(Obj $obj)
{
    if ($this->cache)
    {
        $cachekey = serialize($obj);

        if ($data = $this->cache->get($obj))
        {
            return $data
        }
    }

    // compute result here

    if ($this->cache)
    {
        $this->cache->set($cachekey, $result);
    }

    return $result;
}

If it’s not obvious, $this->cache is an implementation of an interface with the methods get and set.

Is there a quicker alternative to creating a key that’s unique to some 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.

Serialize works, from a “correctness” position, but it seems wasteful (both in size of outputted key, and in computational complexity).

EDIT: I would also like to add, if it’s not obvious, that I will not be needing to unserialize this object. My verbatim code for the current cache key is actually:

$cachekey = 'compile.' . sha1(serialize($obj));.

EDIT 2: The object I’m working with has the following definition:

class Route
{
    protected $pattern;
    protected $defaults = array();
    protected $requirements = array();
}

Pattern and requirements 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.

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.

EDIT 3: I guess I’m not giving enough context. Here’s a link to the code so far:

https://github.com/efritz/minuet/blob/master/src/Minuet/Routing/Router.php#L160

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:42:48+00:00Added an answer on May 26, 2026 at 9:42 am

    One way to do it might be to generate a key based simply from the values you use to compute the result..

    Here is a rough example.

    function compile(Obj $obj)
    {
        if ($this->cache)
        {
            $cachekey = 'Obj-result-' . sha1($obj->pattern . '-' . serialize($obj->requirements));
            // You could even try print_r($obj->requirements, true)
            // or even json_encode($obj->requirements)
            // or implode('-', $obj->requirements)
            // Can't say for sure which is slowest, or fastest.
            if ($data = $this->cache->get($cachekey))
            {
                return $data
            }
        }
    
        // compute result here
        $result = $obj->x + $obj->y; // irrelevant, and from original answer.
    
        if ($this->cache)
        {
            $this->cache->set($cachekey, $result);
        }
    
        return $result;
    }
    

    Since you use an array of data, you’d still need to turn it into something that makes sense as a key.. However this way you’re now only serializing a part of the object, rather then the whole thing. See how it goes. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.