function secache_no_flock() { parent::secache(); $this->__support_usleep = version_compare(PHP_VERSION, 5, '>=') ? 20 : 1; }
<?php require '../secache/secache.php'; $cache = new secache(); $cache->workat('cachedata'); function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return (double) $usec + (double) $sec; } $begin_time = microtime_float(); for ($i = 0; $i < 1000; $i++) { $key = md5($i); //You must *HASH* it by your self $value = str_repeat('No. <strong>' . $i . '</strong> is <em style="color:red">great</em>! ', rand(1, 10)); // must be a *STRING* $cache->store($key, $value); } echo '<h2>Insert x 1000 = ' . (microtime_float() - $begin_time) . ' ms</h2>'; echo '<hr /><h2>test read</h2>'; for ($i = 0; $i < 1000; $i += 200) { $key = md5($i); //You must *HASH* it by your self if ($cache->fetch($key, $value)) { echo '<li>' . $key . '=>' . $value . '</li>'; } else { echo '<li>Data get failed! <b>' . $key . '</b></li>'; } }
/** * 是否存在缓存 * @access public * @param string $key * @return bool */ public function hasCache($key) { $secache = new secache(); $secache->workat($this->_cache_file); return $secache->fetch($key, $content); }
function ts_cache($key, $value = "__secache_get", $expireTime = -1, $type = "secache") { if ($type == "secache") { vendor("secache"); $cache = new secache(); // if(!is_dir(C('Cache_Data'))){ // mk_dir(C('Cache_Data')); // } $cache->workat(C('Cache_Data')); if ($value && $value != "__secache_get") { //赋值 $var["content"] = $value; $var["time"] = $expireTime == -1 ? -1 : time() + $expireTime; return $cache->store(md5($key), $var); } elseif (!$value) { //删除 return $cache->delete(md5($key)); } else { //取值 $cache->fetch(md5($key), $var); if ($var["time"] < 0 || $var["time"] > time()) { return $var["content"]; } else { return false; } } } //以后还可以扩充memcache接口 ... }
//You must *HASH* it by your self $value = 'No. ' . $i . ' is ok'; // must be a *STRING* $cache->store($key, $value); } echo '=================' . PHP_EOL; echo 'Insert ' . $insertCount . ' Count Cost: ' . (microtime_float() - $begin_time) . ' ms' . PHP_EOL; echo '=================' . PHP_EOL; echo 'Test read' . PHP_EOL; echo '=================' . PHP_EOL; testRead($cache, $insertCount); echo '=================' . PHP_EOL; echo 'Test read again' . PHP_EOL; echo '=================' . PHP_EOL; unset($cache); $cache = new secache(); $cache->workat($cacheFileName); testRead($cache, $insertCount); function testRead($cache, $insertCount) { for ($i = 0; $i < $insertCount; $i += 200) { $key = md5($i); //You must *HASH* it by your self if ($cache->fetch($key, $value)) { echo $i . '[KEY=' . $key . '] DATA: ' . $value . PHP_EOL; } else { echo $i . '[KEY=' . $key . '] DATA GET FAILED ' . PHP_EOL; exit; } } }