apc_exist() does not exist?
Asked Answered
T

2

5

I'm having some hard time getting PHP APC to work. Here's my test code:

<form>
    <input type="text" name="apc">
    <input type="submit">
</form>
<?php
    apc_store('foo','FOO');
    if (isset($_GET['apc'])) {
        apc_store($_GET['apc'],$_GET['apc']);
    }
?>
<pre>CACHE INFO (USER): <?php print_r(apc_cache_info("user",false)); ?></pre>
<pre>CACHE INFO: <?php print_r(apc_cache_info()); ?></pre>
<pre>FOO: <?php print_r(apc_fetch("foo")); ?></pre>
<pre>BAR: <?php print_r(apc_fetch("bar")); ?></pre>
<pre><?php if (apc_exists("bar")) { ?>bar exists!<?php } else { ?>bar does not exist!<?php } ?></pre>
<?php apc_clear_cache(); ?>

In short: you fill the form and the inserted value gets stored in APC. The key "foo" is always stored. You can try storing "bar" to see apc_fetch() working with a newly added key.

What works OK:

  • apc_store()
  • apc_fetch()

What does not:

  • apc_cache_info() (regardless of which parametres I pass to the function) always prints an empty array, despite apc_fetch() retrieving data successfully
  • apc_clear_cache() never clears the cache ("bar" is always displayed once input). This is true both if I provide a "user" parametre or leave the function with no parametres.
  • calling apc_exists() yields a fatal error: call to undefined function apc_exists()

In case it's helpful: I'm running Zend Server CE 5.6.0 (fresh install, finished half an hour ago), with PHP 5.3.9. Same happened with a more antique version of Zend Server CE yesterday (running PHP 5.3.5). I do not know which version of APC ships with Zend Server, phpinfo() only shows APC is enabled. I am on a Windows machine (Windows 7 Professional, 32 bit).

So. What's wrong here? Issues with my code? Maybe Zend Server ships with an older version of APC that just is buggy and/or does not support the functions I'm trying to use? Any clues?

[EDIT]

Inspired by clues provided by @Hannes, I modified the code, adding:

<?php
    if (!function_exists('apc_exists') {
        function apc_exists($key) { return (boolean)apc_fetch($key); }
    }
?>

Since no error is raised, the code passes to the next line and the cache is cleared OK. This must have been why it wasn't cleared in the first place.

Still, apc_cache_info() doesn't return anything...

Theatricals answered 3/2, 2012 at 8:48 Comment(0)
M
11
  1. apc_exists is available for PECL apc >= 3.1.4 http://www.php.net/manual/en/function.apc-exists.php so your APC Version is probbaly lower, but its basically just a boolean wraper anyhow, a simple function shoud basically do the same:

    function user_apc_exists($key){ return (bool) apc_fetch($key); }

  2. in both cases your didint provide information for which cache to use, your probaby want user:

    apc_clear_cache('user');
    
    apc_cache_info('user);
    

http://www.php.net/manual/en/function.apc-clear-cache.php

http://www.php.net/manual/en/function.apc-cache-info.php

Malta answered 3/2, 2012 at 9:0 Comment(5)
I have provided such information. Please re-read the question ;). It won't work regardless whether I pass "user" as parametre or leave it to its default (which is both files opcode + user data, as far as I understand).Theatricals
I fixed the apc_exists issue using the info you provided. Any clues about apc_cache_info though?Theatricals
@Theatricals well thats rather odd - but i never used APC on a Windows machine so the fault could ly there please run this file pastebin.com/hGUc2fBF - its an APC Backend / Cache Overwiew (login is apc/apc)Malta
The output is: "No cache info available. APC does not appear to be running.". This is odd, since apc_store() and apc_fetch() are working fine o_O EDIT: after a look at the source code, I can see thet the message is generated precisely if apc_cache_info() doesn't return anything... Precisely what my question is about...Theatricals
@Theatricals okay so i guess it is related to the installation of apc/php under windows - not my field of expertise - sorryMalta
M
0

I ran across the same problem and after some debugging found out that the function in Hannes' answer works unless the stored data is a boolean false or an empty array.

This works also in those cases:

if (!function_exists('apc_exists')) {
  function apc_exists($key) {
        $success = false;
        apc_fetch($key, $success);
        return  $success;
 }
}
Margarettamargarette answered 30/11, 2018 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.