本文使用「署名 4.0 国际 (CC BY 4.0)」许可协议,欢迎转载、或重新修改使用,但需要注明来源。 [署名 4.0 国际 (CC BY 4.0)](https://creativecommons.org/licenses/by/4.0/deed.zh) 本文作者: 苏洋 创建时间: 2012年02月16日 统计字数: 2298字 阅读时间: 5分钟阅读 本文链接: https://soulteary.com/2012/02/16/php-%E7%BC%93%E5%AD%98%E5%AE%9E%E7%8E%B0.html ----- # PHP 缓存实现 如何用PHP实现缓存功能,看过这篇,你应该就明了了。 ```php $val) { $key = is_string($key) ? '\'' . addcslashes($key, '\'\\') . '\'' : $key; $val = !is_array($val) && (!preg_match("/^\-?[1-9]\d*$/", $val) || strlen($val) > 12) ? '\'' . addcslashes($val, '\'\\') . '\'' : $val; if (is_array($val)) { $evaluate .= "$comma$key => " . array2str($val, $level + 1); } else { $evaluate .= "$comma$key => $val"; } $comma = ",\n$space"; } } $evaluate .= "\n$space)"; return $evaluate; } //typeid :文章类别 //nums:文章数量 //len:标题截取长度 //contentlen:内容截取长度 //zhiding:置顶级别 //attach:是否取附件 function getnews($typeid = 0, $nums = 1, $len = 30, $contentlen = 100, $zhiding = 0, $attach = 0) { global $xy_db; $len = 100; $mkey = md5($typeid . $nums . $len . $contentlen . $zhiding . $attach); if (intval($nums) < 1) $nums = 1; $path = SITE_ROOT_PATH . './temp/cache/' . $mkey . '.php'; if (is_file($path)) { @require_once($path); if (($expiration + 60) >= time()) { if (is_array($cachenews)) { $j = 0; for ($i = 0; $i < count($cachenews); $i++) { if ($j < $nums) { $data[] = $cachenews[$i]; $j++; } } return $data; } } } $time = time(); if ($typeid) { $cd .= " and type_id='$typeid'"; } if ($zhiding) { $cd .= " and level='$zhiding' "; } if ($attach) { $cd .= " and attach_num>0 "; } if (empty($typeid)) { $cd .= " and (xy_id='" . XY_ID . "' or xy_id=0) "; } else { $cd .= " and xy_id='" . XY_ID . "' "; } $query = $xy_db->query("select * from xy_news where 1 $cd order by orders desc, id desc limit 0,$nums "); $cachenews = array(); $i = 0; $j = 0; while ($a = $xy_db->fetch_array($query)) { if ($attach) { $r = $xy_db->fetch_first("select * from xy_news_attach where news_id='$a[id]' and is_image=1 limit 0,1"); $a['picurl'] = $r['file_url']; } $a['addtime'] = date('m-d', $a['addtime']); $a['content'] = cutstr(strip_tags($a['content']), $contentlen); $a['fullsubject'] = $a['subject']; $a['linkurl'] = 'news_detail.html?id=' . $a['id']; $cachenews[$i] = $a; $data1[] = $cachenews[$i]; if ($i < $nums) { $cachenews[$i]['subject'] = cutstr($cachenews[$i]['subject'], $len); $data[] = $cachenews[$i]; } $i++; } if ($cachenews) { $cachenews = ''; if ($fp = @fopen($path, 'w')) { @flock($fp, 2); fwrite($fp, $cachenews); fclose($fp); } } return $data; } ?> ```