php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #58997 apc_exist / apc_info
Submitted: 2009-12-17 17:50 UTC Modified: 2010-08-02 04:40 UTC
From: marc dot bennewitz at giata dot de Assigned:
Status: Closed Package: APC (PECL)
PHP Version: 5.3.0 OS: Linux
Private report: No CVE-ID: None
 [2009-12-17 17:50 UTC] marc dot bennewitz at giata dot de
Description:
------------
apc_exist:
There is no function to only check if an key is available within the cache. To test the existence I have to use apc_fetch('key', $success) and check the $seccess variable but it isn't as high-performance as apc_exist because it haven't to return cached data.

apc_info:
to get more information about only one cached key I have to instantiate a new APCIterator with a search string for the key like this:
$it = new APCIterator('user', '/^'.preg_quote($key).'$/', APC_ITER_ALL, 1, APC_LIST_ACTIVE);
if ($it->valid()) {
    $info = $it->current();
    echo $info['mtime'];
}
but thats not the high-performance to get additional information about only one cached key. It is better to use something like this:
$info = apc_info($key, APC_ITER_ALL);
if ($info !== false) {
   echo $info['mtime'];
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-12-18 00:37 UTC] shire@php.net
My only argument against the apc_exists() functionality would be that it could add more kludge to the API, but if there's a need for it it's simple enough to add.  Any objections by any1 else?


APCIterator will also accept an array of keys to find (rather than a regex).  ie:

$it = new APCIterator('user', array($key), APC_ITER_ALL, 1, APC_LIST_ACTIVE);

Does this solve your apc_info use case?
 [2009-12-18 17:38 UTC] marc dot bennewitz at giata dot de
hi shire,

Ok, I didn't know before that the second parameter also accept an array, thats better. It isn't perfect anyway to get additional information about only one item.
There is a lot overhead:
- no need to instantiate an object
- no need to iterate over one item
- no need to search a list if items (regexp or array)
- no need to call "valid()" if the requested item was found
  (a simple "$info === false" is enough)

I know that this functionality is rarely used but in my opinion it is really important.
 [2009-12-23 19:51 UTC] rasmus@php.net
There is a non-locking implementation of apc_exists() in 
trunk now.  Give it a try.
 [2009-12-24 04:02 UTC] marc dot bennewitz at giata dot de
Thanks for the quick implementation.

- With a single key it seems to work fine :)
- With an array of keys it returns false ever :(
- With APCIterator a fatal error is occured
  Is it hard to implement APCIterator as argument like apc_delete, too ?


Test Script:
apc_store('id1', 'data1');
apc_store('id2', 'data2');

var_dump( apc_exists('id1') );
var_dump( apc_exists('id2') );

var_dump( apc_exists(array('id1', 'id2')) );

var_dump( apc_exists(new APCIterator('/^id[12]$/', APC_ITER_NONE, 1, APC_LIST_ACTIVE)) );

Output:
bool(true)
bool(true)
bool(false)
PHP Catchable fatal error:  Object of class APCIterator could not be converted to string ...
 [2009-12-24 04:06 UTC] marc dot bennewitz at giata dot de
Ops sorry here is a fix of my last test script (The result is the same):

- var_dump( apc_exists(new APCIterator('/^id[12]$/', APC_ITER_NONE, 1,
APC_LIST_ACTIVE)) );
+ var_dump( apc_exists(new APCIterator('user', '/^id[12]$/', APC_ITER_NONE, 1,
APC_LIST_ACTIVE)) );
 [2009-12-24 04:29 UTC] shire@php.net
I don't see why you would pass an APCIterator class to apc_exists() here.  APCIterator will allow you to iterate over all the keys that existed matching that regex.
 [2009-12-24 04:35 UTC] marc dot bennewitz at giata dot de
Yes you are right. It was a stupid idea.
 [2009-12-24 11:21 UTC] rasmus@php.net
Try it again, the array case should be working now.
 [2010-01-04 03:42 UTC] marc dot bennewitz at giata dot de
It's working now. Thanks
 [2010-01-08 05:14 UTC] marc dot bennewitz at giata dot de
I noticed that, if the key doesn't exist, apc_exists returns NULL instead of false.

Is this a bug or intended ?
 [2010-07-02 09:26 UTC] kalle@php.net
This bug has been fixed in SVN.

In case this was a documentation problem, the fix will show up at the
end of next Sunday (CET) on pecl.php.net.

In case this was a pecl.php.net website problem, the change will show
up on the website in short time.
 
Thank you for the report, and for helping us make PECL better.

I have changed apc_exists() to return false if it should fail
 [2010-07-20 17:03 UTC] technovicebd at gmail dot com
apc_exists('foo'), this type of command is not working me. It says: 
Fatal error: Call to undefined function apc_exists() in ******  on line 46
 [2010-08-02 04:40 UTC] marc dot bennewitz at giata dot de
it's on svn trunk: http://svn.php.net/repository/pecl/apc/trunk
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 21:01:31 2024 UTC