缓存真的是个好东西,好玩又有趣.
PHP代码
- <?php
- /*
- 常量 CACHE_DIR 定义缓存文件所在目录
- */
- define('CACHE_DIR','/data/cache');
- class cache {
- var $cache = array();
- //获取缓存
- function getCache($cacheName) {
- if(!array_key_exists($cacheName,$this->cache)) {
- include $this->cacheFile($cacheName);
- $this->cache[$cacheName] = $cache[$cacheName];
- }
- return $this->cache[$cacheName];
- }
- //存储缓存
- function saveCache($cacheName,$cacheVar){
- $cacheCode = "<?php\r\n//Cache create at ".date('Y-m-d H:i:s')."\r\n";
- $cacheCode .= '$cache[\''.$cacheName.'\'] = ';
- $cacheCode .= $this->createVarCode($cacheVar);
- $cacheCode .= ";\r\n";
- return $this->writeFile($this->cacheFile($cacheName),$cacheCode) ? true : false;
- }
- //创建缓存变量原生态代码
- function createVarCode($myVar,$level=0) {
- $tabEnd = '';
- for($i=0;$i<$level;$i++) {
- $tabEnd .= "\t";
- }
- $tab = $tabEnd."\t";
- if(is_array($myVar)) {
- $varCode = "array(\r\n";
- foreach($myVar as $key => $val) {
- $key = is_numeric($key) ? $key : '\''.$key.'\'';
- if(is_array($val)) {
- $varCode .= $tab.$key.' => '.$this->createVarCode($val,$level + 1);
- } else {
- $varCode .= $tab.$key.' => \''.$this->addslashes($val).'\'';
- }
- $varCode .= ",\r\n";
- }
- if($myVar) {
- $varCode = substr_replace($varCode,'',-3,1)."$tabEnd)";
- }
- } else {
- $varCode = '\''.$this->addslashes($myVar).'\'';
- }
- return $varCode;
- }
- function addslashes($text) {
- $text = addcslashes($text,'\\');
- $text = addcslashes($text,'\'');
- return $text;
- }
- //返回缓存文件地址
- function cacheFile($cacheName) {
- $cacheFile = CACHE_DIR.'/'.$cacheName.'.php';
- return $cacheFile;
- }
- //判断缓存文件是否存在
- function cacheExists($cacheName) {
- $state = file_exists($this->cacheFile($cacheName));
- return $state;
- }
- function deleteCache($cacheName) {
- return @unlink($this->cacheFile($cacheName)) ? true : false;
- }
- function printCache() {
- print_r($this->cache);
- }
- //写文件
- function writeFile($fileName,$content,$wmode='w'){
- $s = false;
- if($fp = @fopen($fileName,$wmode)) {
- if(flock($fp,LOCK_EX)) {
- $s = @fwrite($fp,$content) ? true : false;
- flock($fp,LOCK_UN);
- fclose($fp);
- }
- }
- return $s;
- }
- }
加上这个函数
PHP代码
- //从缓存获取并处理
- function GetCache($cacheName,$func='') {
- global $cache;
- !defined('CACHE_FUNC') && require_once API('cache_function');
- if($cache->cacheExists($cacheName)) {
- $thisCache = $cache->getCache($cacheName);
- } elseif($func) {
- eval($func.'();');
- exit('缓存 <b>'.$cacheName.'</b> 未能找到,系统已经更新,请刷新页面!');
- }
- return $thisCache;
- }
这样子调用
PHP代码
- $settings = GetCache('settings','cache_reload_settings'); //从缓存获取设置
我们先定义了一个函灵敏 cache_reload_settings() ,这样 GetCache 函数会先从缓存获取,没有的话就会调用 cache_reload_settings() 函数更新缓存,其实PHP的 var_export 函数让我很郁闷.
Look:
PHP代码
- /*
- 缓存更新函数模块
- 14:25 2009-4-20
- */
- define('CACHE_FUNC',true);
- if(!defined('CACHE_DIR')) {
- require_once API('cache');
- }
- if(!$cache) {
- $cache = new cache;
- }
- function cache_reload_settings() {
- global $db,$cache;
- $settings = array();
- $result = $db->query("SELECT * FROM hl_settings");
- while($row = $db->fetchArray($result)) {
- $settings[$row['n']] = $row['v'];
- }
- $cache->saveCache('settings',$settings);
- }
评论 共有 0 条评论
暂无评论,快发表你的评论吧。