tedivm/fig-standards — pull request #11 by simensen (2013-03-07 03:08:37+00:00)
Title: 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 (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
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
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? :)