Storing user variables in memory. XCache functionality with PHP7/OPCache
Save vars to disk and include as PHP to use OPCache.
- First, turn on OPCache in php.ini, if not already
- Second, create directory on disk, put it to variable (global $xdir or directly to functions)
- Third, include xcache.inc to your project
Include like this:
if(!defined('XCACHED')) include 'pathto/xcache.inc';
or use function_exists.
Now you have compatibility with old xcache, and even better:
- You can use it with CLI/Cron.
- Storing engine is PHP7 native Zend OPCache.
- Your variables set is alive after reboot or apache restart.
OPCache have timestamp revalidation – some user defined timeout before changes take effect. If you use long revalidation time and sometimes you need to get variable directly from disk, use:
xcache_get('any_var', true);
You dont't need to check if variable exist, if not – xcache_get just return false.
xcache_isset return file timestamp (variable last modified time) as success value.
You may want to clean old data from disk sometimes. Kill all in the xcache folder or do something like this (add php script to crontab):
foreach(glob('/var/xcache/*', GLOB_ONLYDIR) as $d) exec("find $d/ -type f -mmin +10080 -delete");
Good luck!