> The main reason I ask is, I was wondering if there is any smarter way
> to check if a key exists, because, I'm just thinking that it would be
> advantageous to be able to differentiate a "get_miss" for a reason
> different than the key wasn't cached yet, like application logic
> error, versus everything working correctly but my key isn't cached yet
> or it expired. In my mind, it seems like it would be "cleaner" from
> client-side code, to check if a key exists, then issue a get only if
> the key exists, and be able to see 100% get_hits, but this may not be
> possible.

Not sure how you'd expect memcached to know the difference. If your
application logic fails, you wouldn't have stored anything in memcached,
so there's no way to tell the difference.

The only way you *could* tell the difference is if you issue a get and the
key existed but was expired for some reason, but it doesn't return that
info.

If you're worried about the application never setting the keys, you could
set a canary key at the very top of your app logic.

ie:

if ($cache = $mc->get("$page_blah")) {
    # use $cache
} else {
    $canary = $mc->get("$page_canary");
    if ($canary) {
        die("Something *might* have gone wrong");
    }
    $mc->set("$page_canary", "chirp");
    # Do real logic, fetch from database, render page.
    $mc->set("$page_blah", $page_data);
    $mc->delete("$page_canary");
}

So if your page successfully works, the canary should't exist, but the
cache entry will. I don't see that as being necessary though... if your
app errors that should go to log somewhere, which you should really be
watching anyway.

-Dormando

Reply via email to