[Cache] A method for checking if key exists

436 views
Skip to first unread message

Alexander Makarov

unread,
Sep 12, 2013, 6:24:52 PM9/12/13
to php...@googlegroups.com
Hello everyone,

What do you think about adding a method to check for key existence without getting a value? Sometimes it's actually used to speed things up in case many values are checked per request and values are complex enough in order for unserialize taking significantly more time than just checking if key exists.

Here's a real usecase that was presented as an argument to implemnt it in Yii2: https://github.com/yiisoft/yii2/pull/811

btw., Yii2 will probably switch to PSR-compatible cache implementation before 2.0. At least currently we have no serious reasons not to.

Robert Hafner

unread,
Sep 12, 2013, 6:27:52 PM9/12/13
to php...@googlegroups.com

The "isHit" function in the Item interface does exactly that. 

Robert



--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/70cff624-95bb-4af3-8230-cc22fc3262e1%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Alexander Makarov

unread,
Sep 12, 2013, 6:58:58 PM9/12/13
to php...@googlegroups.com
OK.

I think "exists" as a method name fits better:

- APC, Redis, WinCache (what's bundled with IIS), XCache are all using "exists" as a method name so it will be familiar.
- In computer science cache hit is commonly refers to the situation when there was a check that item is in the cache and cached value was used instead of computation. So there cound be confusuin about if just checking a data w/o actually using it be considered a hit or not.

Jeremy Lindblom

unread,
Sep 12, 2013, 7:00:16 PM9/12/13
to php...@googlegroups.com
Mmm... maybe, I'm missing something, but that sounds like isHit() would introduce a potential race condition if implemented that way. I thought one of the advantages of use an item object is to help prevent that.

Example:

$item = $pool->getItem('foo'); // create item object, but no cache operations performed yet
if ($item->isHit()) { //apc_exists or equivalent operation is performed
    $value = $item->get(); // apc_fetch or equivalent operation performed, however, the item may no longer exist at this point
}

However, if both isHit() and get() do a fetch, then there is no race condition.

I'm not a caching guru, so let me know if I am wrong in my assumptions here. I do agree that there should be a way to do what Alexander is asking for though.


--
Jeremy Lindblom
PHP Software Engineer at Amazon Web Services
Co-author of the AWS SDK for PHP
Co-organizer of the Seattle PHP User Group
Keys/Vocals for Gigawatt Band


Robert Hafner

unread,
Sep 12, 2013, 7:01:08 PM9/12/13
to php...@googlegroups.com
Actually you guys are completely right, isHit and exists should be considered separate things.

Robert


Paul Dragoonis

unread,
Sep 13, 2013, 6:25:26 AM9/13/13
to php...@googlegroups.com
isHit() is to be used when you want to pull the data down from the cache, unerialized and check if it existed
exists() is to be used when you want to just check existance but not pull the data over the wire.

we used to have exists() on the original merger PR but it wasn't carried over to the latest one.


Paul Dragoonis

unread,
Sep 13, 2013, 6:34:28 AM9/13/13
to php...@googlegroups.com

Robert Hafner

unread,
Sep 13, 2013, 12:35:59 PM9/13/13
to php...@googlegroups.com
It was removed because people wanted it removed- that being said, I'd prefer that it was there, we just need to make sure there's consensus.

Robert



Larry Garfield

unread,
Sep 13, 2013, 1:03:36 PM9/13/13
to php...@googlegroups.com
I don't see how it would work without a race condition.

1) If exists() doesn't actually fetch data, then the race condition previously noted is present:

if ($pool->exists('foo')) {
  // Something else happens
  $pool->getItem('foo'); // This is no longer consistent with the exists() call
}

2) If exists() does fetch data, then there's really no benefit to it over $pool->getItem('foo')->isHit(); It has the same cost and same result.

So I don't really see what the benefit is to exists(), other than one less method call (which is not a worthwhile benefit for the confusion it could cause).

--Larry Garfield

Alexander Makarov

unread,
Sep 13, 2013, 1:44:30 PM9/13/13
to php...@googlegroups.com
1) Correct. So it should be explicitly stated that isHit should fetch data in the PSR or meta.

2) Cost isn't the same if data fetched is quite large. It can take significant time to transfer data and unserialize it.
Sometimes you don't care about race condition and want just to check if something is cached w/o doing anything with the value.

Paul Dragoonis

unread,
Sep 13, 2013, 2:13:03 PM9/13/13
to php...@googlegroups.com
I hope the explanations in my PR are enough to educate people when they should use exists() vs isHit() for different circumstances.

If you're dealing with high-traffic websites that depend strongly on things being there, you're a isHit() guy.
If it's more of a relaxed and casual project you're better going with exists().

The exists() stuff has been around for years now and will continue to be there for years to come. We have identified a problem and come up with the isHit() strategy to fix it, but the caching PSR should not be limiting in any way and keep existing practises going.

What's required now to reach consensus?

 


--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.

Phil Sturgeon

unread,
Sep 13, 2013, 2:37:06 PM9/13/13
to php...@googlegroups.com
+1 on having the two methods.

Pádraic Brady

unread,
Sep 16, 2013, 3:38:05 PM9/16/13
to php...@googlegroups.com
I also see no reason not to have both methods so long as the implications are clear to implementors to carry forward into their own docs.

Paddy


On 13 September 2013 19:37, Phil Sturgeon <em...@philsturgeon.co.uk> wrote:
+1 on having the two methods.

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--

--
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
Zend Framework Community Review Team
Zend Framework PHP-FIG Representative

Larry Garfield

unread,
Sep 20, 2013, 12:15:10 AM9/20/13
to php...@googlegroups.com
I've merged Paul's PR to bring back exists(), and further refined the documentation to clarify that exists() MAY have a race condition but isHit() is forbidden from having one.  (More as a note to implementers about what they have to do.)

--Larry Garfield

Paul Dragoonis

unread,
Sep 27, 2013, 9:55:59 AM9/27/13
to php...@googlegroups.com
Thanks for merging my PR Larry, the more docs there are, the more times we can tell people to RTFM and link them to the github docs section.


Reply all
Reply to author
Forward
0 new messages