Our web site platform is highly efficient, primarily due to the aggressive caching we have implemented using APC. APC has performance advantages over other systems like Redis and Memcache because it avoids any kind of protocol overhead. It is tightly integrated and allows PHP to access the key-value pairs in memory. However, it comes with the drawback that any command-line PHP does not have access to the same cache because it is running in a different process. This becomes a major issue when there are intensive background processes that are run, and it can become a major source of database load in configuration-heavy systems like Drupal. So how do we leverage the online cache that has been built when working with console applications?
The common response is that it is not possible: reference, reference. Some people have had some success using dumpfiles for opcode caching, which more closely resembles the solution I am considering for accessing the user cache.
Performing system administration via a web-based PHP script requires extra attention to security. To minimize the overhead from security layers, the following techniques are employed/recommended:
- The configuration is in a PHP file rather than a YAML/JSON file that could be viewable online.
- The console and web parts include a secret key in their handshake. You are responsible for generating the key, but uuidgen is a simple solution.
- Most importantly, this script should be installed on a private port so that it cannot be accessed by the public.
Basic Usage: Using User Variable Cache from Console
More features are planned, but the basic usage is simply to access the user variable cache while running a console (command line) PHP script. This is straightforward:
- Create a VirtualHost listening on a private port (i.e., NOT 80!)
- Clone the ApcConsole repository from github.
- Copy settings.php.dist to settings.php and make any customizations - minimally, change the secret.
Include the default handler near the top of your console application:
Tip: Usage with Drush
Add the require_once statement to your module's hook_drush_init() function.