function cache_put_data($key, $value, $ttl = 120) { global $boardurl, $sourcedir, $modSettings, $memcached; global $cache_hits, $cache_count, $db_show_debug; if (empty($modSettings['cache_enable']) && !empty($modSettings)) { return; } $cache_count = isset($cache_count) ? $cache_count + 1 : 1; if (isset($db_show_debug) && $db_show_debug === true) { $cache_hits[$cache_count] = array('k' => $key, 'd' => 'put', 's' => $value === null ? 0 : strlen(serialize($value))); $st = microtime(); } $key = md5($boardurl . filemtime($sourcedir . '/Load.php')) . '-SMF-' . $key; $value = $value === null ? null : serialize($value); // The simple yet efficient memcached. if (function_exists('memcache_set') && isset($modSettings['cache_memcached']) && trim($modSettings['cache_memcached']) != '') { // Not connected yet? if (empty($memcached)) { get_memcached_server(); } if (!$memcached) { return; } memcache_set($memcached, $key, $value, 0, $ttl); } elseif (function_exists('eaccelerator_put')) { if (mt_rand(0, 10) == 1) { eaccelerator_gc(); } if ($value === null) { @eaccelerator_rm($key); } else { eaccelerator_put($key, $value, $ttl); } } elseif (function_exists('mmcache_put')) { if (mt_rand(0, 10) == 1) { mmcache_gc(); } if ($value === null) { @mmcache_rm($key); } else { mmcache_put($key, $value, $ttl); } } elseif (function_exists('apc_store')) { // An extended key is needed to counteract a bug in APC. if ($value === null) { apc_delete($key . 'smf'); } else { apc_store($key . 'smf', $value, $ttl); } } elseif (function_exists('output_cache_put')) { output_cache_put($key, $value); } if (isset($db_show_debug) && $db_show_debug === true) { $cache_hits[$cache_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); } }
/** * Put data in the cache * * Adds data to whatever cache method we're using * * @param string $key the cache data identifier * @param mixed $value the value to be stored * @param int $ttl how long are we going to cache this data (in seconds) * @return void * @since 0.1.0 */ function smfapi_cachePutData($key, $value, $ttl = 120) { global $boardurl, $sourcedir, $modSettings, $memcached; global $cache_hits, $cache_count, $db_show_debug, $cachedir; if (empty($modSettings['cache_enable']) && !empty($modSettings)) { return; } $cache_count = isset($cache_count) ? $cache_count + 1 : 1; if (isset($db_show_debug) && $db_show_debug === true) { $cache_hits[$cache_count] = array('k' => $key, 'd' => 'put', 's' => $value === null ? 0 : strlen(serialize($value))); $st = microtime(); } $key = md5($boardurl . filemtime($sourcedir . '/Load.php')) . '-SMF-' . strtr($key, ':', '-'); $value = $value === null ? null : serialize($value); // eAccelerator... if (function_exists('eaccelerator_put')) { if (mt_rand(0, 10) == 1) { eaccelerator_gc(); } if ($value === null) { @eaccelerator_rm($key); } else { eaccelerator_put($key, $value, $ttl); } } elseif (function_exists('mmcache_put')) { if (mt_rand(0, 10) == 1) { mmcache_gc(); } if ($value === null) { @mmcache_rm($key); } else { mmcache_put($key, $value, $ttl); } } elseif (function_exists('apc_store')) { // An extended key is needed to counteract a bug in APC. if ($value === null) { apc_delete($key . 'smf'); } else { apc_store($key . 'smf', $value, $ttl); } } elseif (function_exists('output_cache_put')) { output_cache_put($key, $value); } elseif (function_exists('xcache_set') && ini_get('xcache.var_size') > 0) { if ($value === null) { xcache_unset($key); } else { xcache_set($key, $value, $ttl); } } else { if ($value === null) { @unlink($cachedir . '/data_' . $key . '.php'); } else { $cache_data = '<' . '?' . 'php if (!defined(\'SMF\')) die; if (' . (time() + $ttl) . ' < time()) $expired = true; else{$expired = false; $value = \'' . addcslashes($value, '\\\'') . '\';}' . '?' . '>'; $fh = @fopen($cachedir . '/data_' . $key . '.php', 'w'); if ($fh) { // write the file. set_file_buffer($fh, 0); flock($fh, LOCK_EX); $cache_bytes = fwrite($fh, $cache_data); flock($fh, LOCK_UN); fclose($fh); // check that the cache write was successful; all the data should be written // if it fails due to low diskspace, remove the cache file if ($cache_bytes != strlen($cache_data)) { @unlink($cachedir . '/data_' . $key . '.php'); } } } } if (isset($db_show_debug) && $db_show_debug === true) { $cache_hits[$cache_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); } return; }
function cache_put_data($key, $value, $ttl = 120) { global $boardurl, $sourcedir, $modSettings, $memcached; global $cache_hits, $cache_count, $db_show_debug, $cachedir; if (empty($modSettings['cache_enable']) && !empty($modSettings)) { return; } $cache_count = isset($cache_count) ? $cache_count + 1 : 1; if (isset($db_show_debug) && $db_show_debug === true) { $cache_hits[$cache_count] = array('k' => $key, 'd' => 'put', 's' => $value === null ? 0 : strlen(serialize($value))); $st = microtime(); } $key = md5($boardurl . filemtime($sourcedir . '/Load.php')) . '-SMF-' . strtr($key, ':', '-'); $value = $value === null ? null : serialize($value); // The simple yet efficient memcached. if (function_exists('memcache_set') && isset($modSettings['cache_memcached']) && trim($modSettings['cache_memcached']) != '') { // Not connected yet? if (empty($memcached)) { get_memcached_server(); } if (!$memcached) { return; } memcache_set($memcached, $key, $value, 0, $ttl); } elseif (function_exists('eaccelerator_put')) { if (mt_rand(0, 10) == 1) { eaccelerator_gc(); } if ($value === null) { @eaccelerator_rm($key); } else { eaccelerator_put($key, $value, $ttl); } } elseif (function_exists('mmcache_put')) { if (mt_rand(0, 10) == 1) { mmcache_gc(); } if ($value === null) { @mmcache_rm($key); } else { mmcache_put($key, $value, $ttl); } } elseif (function_exists('apc_store')) { // An extended key is needed to counteract a bug in APC. if ($value === null) { apc_delete($key . 'smf'); } else { apc_store($key . 'smf', $value, $ttl); } } elseif (function_exists('output_cache_put')) { output_cache_put($key, $value); } elseif (function_exists('xcache_set') && ini_get('xcache.var_size') > 0) { if ($value === null) { xcache_unset($key); } else { xcache_set($key, $value, $ttl); } } elseif (function_exists('fwrite')) { if ($value === null) { @unlink($cachedir . '/data_' . $key . '.php'); } else { $fp = @fopen($cachedir . '/data_' . $key . '.php', 'w'); if ($fp) { // Write the header. @flock($fp, LOCK_EX); fwrite($fp, '<' . '?' . 'php if (!defined(\'SMF\')) die; if (' . (time() + $ttl) . ' < time()) $expired = true; else{$expired = false; $value = \'' . addcslashes($value, '\\\'') . '\';}' . '?' . '>'); @flock($fp, LOCK_UN); fclose($fp); } } } if (isset($db_show_debug) && $db_show_debug === true) { $cache_hits[$cache_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); } }
/** * Empty out the cache in use as best it can * * It may only remove the files of a certain type (if the $type parameter is given) * Type can be user, data or left blank * - user clears out user data * - data clears out system / opcode data * - If no type is specified will perfom a complete cache clearing * For cache engines that do not distinguish on types, a full cache flush will be done * * @param string $type = '' */ function clean_cache($type = '') { global $cache_accelerator, $cache_uid, $cache_password, $cache_memcached, $memcached; switch ($cache_accelerator) { case 'memcached': if ((function_exists('memcache_flush') || function_exists('memcached_flush')) && !empty($cache_memcached)) { // Not connected yet? if (empty($memcached)) { get_memcached_server(); } if (!$memcached) { return; } // Clear it out, really invalidate whats there if (function_exists('memcache_flush')) { memcache_flush($memcached); } else { memcached_flush($memcached); } } break; case 'eaccelerator': if (function_exists('eaccelerator_clear') && function_exists('eaccelerator_clean')) { // Clean out the already expired items @eaccelerator_clean(); // Remove all unused scripts and data from shared memory and disk cache, // e.g. all data that isn't used in the current requests. @eaccelerator_clear(); } case 'mmcache': if (function_exists('mmcache_gc')) { // Removes all expired keys from shared memory, this is not a complete cache flush :( // @todo there is no clear function, should we try to find all of the keys and delete those? with mmcache_rm mmcache_gc(); } break; case 'apc': case 'apcu': if (function_exists('apc_clear_cache')) { // If passed a type, clear that type out if ($type === '' || $type === 'data') { apc_clear_cache('user'); apc_clear_cache('system'); } elseif ($type === 'user') { apc_clear_cache('user'); } } break; case 'zend': if (function_exists('zend_shm_cache_clear')) { zend_shm_cache_clear('ELK'); } break; case 'xcache': if (function_exists('xcache_clear_cache') && function_exists('xcache_count')) { // Xcache may need auth credentials, depending on how its been set up if (!empty($cache_uid) && !empty($cache_password)) { $_SERVER['PHP_AUTH_USER'] = $cache_uid; $_SERVER['PHP_AUTH_PW'] = $cache_password; } // Get the counts so we clear each instance $pcnt = xcache_count(XC_TYPE_PHP); $vcnt = xcache_count(XC_TYPE_VAR); // Time to clear the user vars and/or the opcache if ($type === '' || $type === 'user') { for ($i = 0; $i < $vcnt; $i++) { xcache_clear_cache(XC_TYPE_VAR, $i); } } if ($type === '' || $type === 'data') { for ($i = 0; $i < $pcnt; $i++) { xcache_clear_cache(XC_TYPE_PHP, $i); } } } break; } // To be complete, we also clear out the cache dir so we get any js/css hive files if (is_dir(CACHEDIR)) { // Remove the cache files in our disk cache directory $dh = opendir(CACHEDIR); while ($file = readdir($dh)) { if ($file != '.' && $file != '..' && $file != 'index.php' && $file != '.htaccess' && (!$type || substr($file, 0, strlen($type)) == $type)) { @unlink(CACHEDIR . '/' . $file); } } closedir($dh); } // Invalidate cache, to be sure! // ... as long as Load.php can be modified, anyway. @touch(SOURCEDIR . '/Load.php'); // Give addons a way to trigger cache cleaning. call_integration_hook('integrate_clean_cache'); clearstatcache(); }
/** * Puts value in the cache under key for ttl seconds. * * - It may "miss" so shouldn't be depended on * - Uses the cache engine chosen in the ACP and saved in settings.php * - It supports: * Turck MMCache: http://turck-mmcache.sourceforge.net/index_old.html#api * Xcache: http://xcache.lighttpd.net/wiki/XcacheApi * memcache: http://www.php.net/memcache * APC: http://www.php.net/apc * eAccelerator: http://bart.eaccelerator.net/doc/phpdoc/ * Zend: http://files.zend.com/help/Zend-Platform/output_cache_functions.htm * Zend: http://files.zend.com/help/Zend-Platform/zend_cache_functions.htm * * @param string $key * @param mixed $value * @param int $ttl = 120 */ function cache_put_data($key, $value, $ttl = 120) { global $boardurl, $sourcedir, $modSettings, $memcached; global $cache_hits, $cache_count, $db_show_debug, $cachedir; global $cache_accelerator, $cache_enable; if (empty($cache_enable)) { return; } $cache_count = isset($cache_count) ? $cache_count + 1 : 1; if (isset($db_show_debug) && $db_show_debug === true) { $cache_hits[$cache_count] = array('k' => $key, 'd' => 'put', 's' => $value === null ? 0 : strlen(serialize($value))); $st = microtime(); } $key = md5($boardurl . filemtime($sourcedir . '/Load.php')) . '-SMF-' . strtr($key, ':/', '-_'); $value = $value === null ? null : serialize($value); switch ($cache_accelerator) { case 'memcached': // The simple yet efficient memcached. if (function_exists('memcached_set') || function_exists('memcache_set') && isset($modSettings['cache_memcached']) && trim($modSettings['cache_memcached']) != '') { // Not connected yet? if (empty($memcached)) { get_memcached_server(); } if (!$memcached) { return; } memcache_set($memcached, $key, $value, 0, $ttl); } break; case 'eaccelerator': // eAccelerator... if (function_exists('eaccelerator_put')) { if (mt_rand(0, 10) == 1) { eaccelerator_gc(); } if ($value === null) { @eaccelerator_rm($key); } else { eaccelerator_put($key, $value, $ttl); } } break; case 'mmcache': // Turck MMCache? if (function_exists('mmcache_put')) { if (mt_rand(0, 10) == 1) { mmcache_gc(); } if ($value === null) { @mmcache_rm($key); } else { mmcache_lock($key); mmcache_put($key, $value, $ttl); mmcache_unlock($key); } } break; case 'apc': // Alternative PHP Cache, ahoy! if (function_exists('apc_store')) { // An extended key is needed to counteract a bug in APC. if ($value === null) { apc_delete($key . 'smf'); } else { apc_store($key . 'smf', $value, $ttl); } } break; case 'zend': // Zend Platform/ZPS/etc. if (function_exists('zend_shm_cache_store')) { zend_shm_cache_store('SMF::' . $key, $value, $ttl); } elseif (function_exists('output_cache_put')) { output_cache_put($key, $value); } break; case 'xcache': if (function_exists('xcache_set') && ini_get('xcache.var_size') > 0) { if ($value === null) { xcache_unset($key); } else { xcache_set($key, $value, $ttl); } } break; default: // Otherwise custom cache? if ($value === null) { @unlink($cachedir . '/data_' . $key . '.php'); } else { $cache_data = '<' . '?' . 'php if (!defined(\'SMF\')) die; if (' . (time() + $ttl) . ' < time()) $expired = true; else{$expired = false; $value = \'' . addcslashes($value, '\\\'') . '\';}'; // Write out the cache file, check that the cache write was successful; all the data must be written // If it fails due to low diskspace, or other, remove the cache file if (file_put_contents($cachedir . '/data_' . $key . '.php', $cache_data, LOCK_EX) !== strlen($cache_data)) { @unlink($cachedir . '/data_' . $key . '.php'); } } break; } if (function_exists('call_integration_hook')) { call_integration_hook('cache_put_data', array(&$key, &$value, &$ttl)); } if (isset($db_show_debug) && $db_show_debug === true) { $cache_hits[$cache_count]['t'] = array_sum(explode(' ', microtime())) - array_sum(explode(' ', $st)); } }