/** * write template to cache * * @access public * @param string cache key * @param array templates to store * @return boolean true on success */ function write($key, $templates) { if (!function_exists('mmcache_lock')) { return false; } mmcache_lock($key); if ($this->getParam('lifetime') == 'auto') { mmcache_put($key, serialize($templates)); } else { mmcache_put($key, serialize($templates), $this->getParam('lifetime') * 60); } mmcache_unlock($key); return true; }
/** * set value of variable in shared mem * * @param string $name name of the variable * @param string $value value of the variable * @param int $ttl (optional) time to life of the variable * * @return bool true on success * @access public */ function set($name, $value, $ttl = 0) { mmcache_lock($name); return mmcache_put($name, $value, $ttl); }
function lock($key, $waitTimeout = 0) { mmcache_lock($key); return true; }
/** * 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 string|int|mixed[]|null $value * @param int $ttl = 120 */ function cache_put_data($key, $value, $ttl = 120) { global $cache_memcached, $memcached, $cache_hits, $cache_count, $db_show_debug; 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(true); } $key = cache_get_key($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') && !empty($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': case 'apcu': // 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 . 'elkarte'); } else { apc_store($key . 'elkarte', $value, $ttl); } } break; case 'zend': // Zend Platform/ZPS/etc. if (function_exists('zend_shm_cache_store')) { zend_shm_cache_store('ELK::' . $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(\'ELK\')) 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'] = microtime(true) - $st; } }