7

I was trying to use APC but it doesn't seem to work as I expected.

file1:

$bar = 'BAR';
apc_store('foo', $bar, 3600);
var_dump(apc_fetch('foo'));           // It works here. Displays BAR

file2:

var_dump(apc_fetch('foo'));

When I execute file2 within seconds, it returns false instead of 'BAR' which is the data stored in the cache.

Andry R.
  • 199
  • 1
  • 2
  • 5
  • Never ran into it, but is it still happening when [apc.localcache](http://www.php.net/manual/en/apc.configuration.php#ini.apc.localcache) is set to false? – Wrikken Nov 03 '10 at 22:35
  • Are you testing from a web page, or from the command line? – Mark Baker Nov 03 '10 at 22:39
  • 3
    What SAPI are you using? Apache with mod_php? SUEXEC (which is nothing more than a wrapper around CGI)? FastCGI? CGI? My guess is that you have more than one PHP process, so they are not sharing the memory space... – ircmaxell Nov 03 '10 at 22:58
  • ^ yup, I think ircmaxell is probably spot on, in which case alternative caches like memcache are the way to go. – Wrikken Nov 04 '10 at 16:22

2 Answers2

4

It works fine :) - as long as you remember that every php script executed from the command line uses it's own cache, so you won't be able to access data saved by script1 inside script2. (you can't access it in a later run of script1 either as it gets cleared when the script finishes)

These caches are also separate from the cache you most likely want to use, and that's the cache of php scripts executed via your web server.

So if you have those tests above saved in your webroot so you can access for example http://localhost/file1.php, then http://localhost/file2.php

It'll work as expected.

This also means that you can't clear out the webserver's APC cache from the command line. The cache (user cache or opcode cache) clearing code has to be executed via your web server. Would it be a wget from shell, or file_get_contents() from php cli - it's up to your taste and circumstances.

Rau
  • 76
  • 4
0

Install this ( http://svn.php.net/viewvc/pecl/apc/trunk/apc.php?view=markup ) in a protected area of the server, set it up as describe in the comments, and that should make diagnosing your APC caching issues a breeze... or atleast give you a better insight into why something isn't working as desired.

David
  • 17,673
  • 10
  • 68
  • 97