Skip to content

Add isHit and isMiss to CacheItem in place of isValid #11

Open
wants to merge 2 commits into from

1 participant

@simensen
simensen commented Mar 7, 2013

Renamed isValid to isHit as discussed on IRC. The concept of cache validity is a separate concern and we should not overload the meaning here.

Added isMiss as syntactic sugar for !$item->isHit().

There were apparently some mailing list concerns about isMiss looking like a double negative (or something; I didn't quite follow at the time and can't find the argument now). However, there are two states represented by this cache system, a "cache hit" and a "cache miss." People will build workflows around both concepts. Forcing one camp over the other to negate with ! seems unfriendly when we can add this sugar.

I'm open to removing isMiss if it is going to be a problem, I just thought I'd save some time and add it here now as I was already in the process of editing.

Use case for isMiss workflow:

This is more for inline operations where you are getting something from cache locally and then trying to do something with it locally rather than returning the cached result. In this case, you care if there is a cache miss because in that case you need to get and then set the value before moving on to the rest of the operation.

<?php
function do_something_with_foo()
{
    $item = $pool->getItem('foo');

    $foo = $item->get();

    if ($item->isMiss()) {
        $foo = expensive_operation();
        $item->set($foo, 300);
    }

    // do something with a valid $foo
}

Use case for isHit workflow:

This is useful for a "return early" workflow where you want to just return right away if something was found in the cache.

<?php
function get_foo()
{
    $item = $pool->getItem('foo');

    if ($item->isHit()) {
        return $item->get();
    }

    $foo = expensive_operation();
    $item->set($foo, 300);

    return $foo;
}

Negate

Sure, we can use ! but either one camp or the other is going to be unhappy. Why choose when we can easily do both? :)

simensen added some commits Mar 6, 2013
@simensen simensen Add isHit and isMiss to CacheItem in place of isValid
Renamed `isValid` to `isHit` as discussed on IRC. The concept
of cache validity is a separate concern and we should not
overload the meaning here.

Added `isMiss` as syntactic sugar for `!$item->isHit()`. There
were apparently some mailing list concerns about `isMiss`
looking like a double negative. However, there are two states,
a "cache hit" and a "cache miss" and people will build workflows
around both concepts. Forcing one over the other to need to
negate with `!` seems like overkill.
ca6188d
@simensen simensen Fixed typo. f6dda48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.