Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apc_exist() does not exist?

Tags:

php

caching

apc

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...

like image 422
mingos Avatar asked Feb 03 '12 08:02

mingos


1 Answers

  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

like image 192
Hannes Avatar answered Nov 20 '22 18:11

Hannes