3

I am setting an APC cached array with the command:

apc_add( 'ips', $ips );

Via the command line. This cached array can be retrieved with:

apc_fetch( 'ips' )

in the same script.

However, it cannot be accessed from either subsequent CLI scripts or via PHP called by Apache. While I can call variables across scripts ran by Apache.

I am running on: 5.4.6-1ubuntu1.8 and APC is enabled with:

apc.enable_cli=1

On my local PC, I am using APCU with PHP7 and this problem does not occur.

Settings:

apc.cache_by_default => On => On
apc.canonicalize => On => On
apc.coredump_unmap => Off => Off
apc.enable_cli => On => On
apc.enabled => On => On
apc.file_md5 => Off => Off
apc.file_update_protection => 2 => 2
apc.filters => no value => no value
apc.gc_ttl => 3600 => 3600
apc.include_once_override => Off => Off
apc.lazy_classes => Off => Off
apc.lazy_functions => Off => Off
apc.max_file_size => 1M => 1M
apc.mmap_file_mask => no value => no value
apc.num_files_hint => 1000 => 1000
apc.preload_path => no value => no value
apc.report_autofilter => Off => Off
apc.rfc1867 => Off => Off
apc.rfc1867_freq => 0 => 0
apc.rfc1867_name => APC_UPLOAD_PROGRESS => APC_UPLOAD_PROGRESS
apc.rfc1867_prefix => upload_ => upload_
apc.rfc1867_ttl => 3600 => 3600
apc.serializer => default => default
apc.shm_segments => 1 => 1
apc.shm_size => 32M => 32M
apc.shm_strings_buffer => 4M => 4M
apc.slam_defense => On => On
apc.stat => On => On
apc.stat_ctime => Off => Off
apc.ttl => 0 => 0
apc.use_request_time => On => On
apc.user_entries_hint => 4096 => 4096
apc.user_ttl => 0 => 0
apc.write_lock => On => On

Any ideas?

4
  • 2
    The docs imply that APC is not really meant to work reliably for the CLI. Commented Mar 11, 2017 at 19:32
  • Do you know of an alternative? Commented Mar 11, 2017 at 19:33
  • 1
    I use Redis and the Predis library. It's pretty straight forward to set up and use. Commented Mar 11, 2017 at 19:35
  • @apokryfos i do not see there "not really meant to work reliably" but "Mostly for testing and debugging" , there is nothing said like it may not work. Commented Jul 16, 2017 at 14:36

1 Answer 1

4

Yes this is a confusing aspect to the apc.enable_cli=1 parameter. It suggests that you could get behavior similar to the way it might work with an apache module.

However, with cli, there is one process and no shared state, so despite the fact that the parameter exists once the first cli execution is complete, the entire apc memory space is disposed of and ceases to exist when the cli script is complete.

You need some other mechanism (memcache, redis, queues, flat file, database) to make this work with multiple CLI executions (batch runs?) other than APC.

FWIW, APC uses shared memory for those operations. There is a seperate shared memory api for php, which might work. The use case for this is for something shared between processes that by definition are confined to one single server. There aren't a lot of those use cases I can think of. Using those routines with CLI might work, but I haven't tried them.

If you really need something shared, that would even work across multiple servers (for example a cluster of batch processing workers) then you are better off with queuing, memcache or redis.

Sign up to request clarification or add additional context in comments.

2 Comments

it does not work even while execution is not complete: stackoverflow.com/questions/45129579/…
No you misunderstand. Just because a cli process is running, doesn't mean you can run another cli instance and read the variables. A cli process shares no state or memory with other cli processes. So APC is of no value in CLI processes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.