无为清净楼资源网 Design By www.qnjia.com

为大家分享很全的php文件缓存,供大家参考,具体内容如下

<"./",
  'file_name_prefix' => 'cache',
  'mode'   => '1', //mode 1 为serialize model 2为保存为可执行文件
 ); 
 
 /**
  * 得到本类实例
  * 
  * @return Ambiguous
  */
 public static function getInstance()
 {
  if(self::$_instance === null)
  {
   self::$_instance = new self();
  }
  return self::$_instance;
 } 
 
 /**
  * 得到缓存信息
  * 
  * @param string $id
  * @return boolean|array
  */
 public static function get($id)
 {
  $instance = self::getInstance();
  
  //缓存文件不存在
  if(!$instance->has($id))
  {
   return false;
  }
  
  $file = $instance->_file($id);
  
  $data = $instance->_fileGetContents($file);
  
  if($data['expire'] == 0 || time() < $data['expire'])
  {
   return $data['contents'];
  }
  return false;
 }
 
 /**
  * 设置一个缓存
  * 
  * @param string $id 缓存id
  * @param array $data 缓存内容
  * @param int $cacheLife 缓存生命 默认为0无限生命
  */
 public static function set($id, $data, $cacheLife = 0)
 {
  $instance = self::getInstance();
  
  $time = time();
  $cache   = array();
  $cache['contents'] = $data;
  $cache['expire'] = $cacheLife === 0 "<".
     " // mktime: ". $time. "\n".
     " return ".
     var_export($contents, true).
     "\n";
  }
  
  $result = false;
  $f = @fopen($file, 'w');
  if ($f) {
   @flock($f, LOCK_EX);
   fseek($f, 0);
   ftruncate($f, 0);
   $tmp = @fwrite($f, $contents);
   if (!($tmp === false)) {
    $result = true;
   }
   @fclose($f);
  }
  @chmod($file,0777);
  return $result;    
 }
 
 /**
  * 从文件得到数据
  * 
  * @param sring $file
  * @return boolean|array
  */
 protected function _fileGetContents($file)
 {
  if(!is_file($file))
  {
   return false;
  }
  
  if($this->_options['mode'] == 1)
  {
   $f = @fopen($file, 'r'); 
   @$data = fread($f,filesize($file));
   @fclose($f);
   return unserialize($data);
  }
  else
  {
   return include $file;
  }
 }
 
 /**
  * 构造函数
  */
 protected function __construct()
 {
 
 }
 
 /**
  * 设置缓存路径
  * 
  * @param string $path
  * @return self
  */
 public static function setCacheDir($path)
 {
  $instance = self::getInstance();
  if (!is_dir($path)) {
   exit('file_cache: ' . $path.' 不是一个有效路径 ');
  }
  if (!is_writable($path)) {
   exit('file_cache: 路径 "'.$path.'" 不可写');
  }
 
  $path = rtrim($path,'/') . '/';
  $instance->_options['cache_dir'] = $path;
  
  return $instance;
 }
 
 /**
  * 设置缓存文件前缀
  * 
  * @param srting $prefix
  * @return self
  */
 public static function setCachePrefix($prefix)
 {
  $instance = self::getInstance();
  $instance->_options['file_name_prefix'] = $prefix;
  return $instance;
 }
 
 /**
  * 设置缓存存储类型
  * 
  * @param int $mode
  * @return self
  */
 public static function setCacheMode($mode = 1)
 {
  $instance = self::getInstance();
  if($mode == 1)
  {
   $instance->_options['mode'] = 1;
  }
  else
  {
   $instance->_options['mode'] = 2;
  }
  
  return $instance;
 }
 
 /**
  * 删除所有缓存
  * @return boolean
  */
 public static function flush()
 {
  $instance = self::getInstance();
  $glob = @glob($instance->_options['cache_dir'] . $instance->_options['file_name_prefix'] . '--*');
  
  if(empty($glob))
  {
   return false;
  }
  
  foreach ($glob as $v)
  {
   $fileName = basename($v);
   $id = $instance->_fileNameToId($fileName);
   $instance->delete($id);
  }
  return true;
 }
}

/* 初始化设置cache的配置信息什么的 */
cache::setCachePrefix('core'); //设置缓存文件前缀
cache::setCacheDir('./cache'); //设置存放缓存文件夹路径

//模式1 缓存存储方式
//a:3:{s:8:"contents";a:7:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:34;i:4;i:5;i:5;i:6;i:6;i:6;}s:6:"expire";i:0;s:5:"mtime";i:1318218422;}
//模式2 缓存存储方式
/*
 <"htmlcode">
<"'. $this->_options['cache_dir'] .'" 不可写');
   }
   
  } else {
   exit('file_cache: "options" cache_dir 不能为空 ');
  }
 }
 
 /**
  * 设置缓存路径
  * @param string $value
  * @return void
  */
 static function setCacheDir($value)
 {
  $self = & self::getInstance();
   
  if (!is_dir($value)) {
   exit('file_cache: ' . $value.' 不是一个有效路径 ');
  }
  if (!is_writable($value)) {
   exit('file_cache: 路径 "'.$value.'" 不可写');
  }
  
  $value = rtrim($this->_options['cache_dir'],'/') . '/';
   
  $self->_options['cache_dir'] = $value;
 }
  
 /**
  * 存入缓存数据
  * @param array $data   放入缓存的数据
  * @param string $id   缓存id(又名缓存识别码)
  * @param cache_life   缓存时间
  * @return boolean True if no problem
  */
 static function save($data, $id = null, $cache_life = null)
 {
  $self = & self::getInstance();
  if (!$id) {
   if ($self->_id) {
    $id = $self->_id;
   } else {
    exit('file_cache:save() id 不能为空!');
   }
  }
  $time = time();
   
  if($cache_life) {
   $data[self::FILE_LIFE_KEY] = $time + $cache_life;
  }
  elseif
  ($cache_life != 0){
   $data[self::FILE_LIFE_KEY] = $time + $self->_options['file_life'];
  }
   
  $file = $self->_file($id);
   
  $data = "<".
    " // mktime: ". $time. "\n".
    " return ".
    var_export($data, true).
    "\n"
    ;
   
  $res = $self->_filePutContents($file, $data);
  return $res;
 }
  
  
 /**
  * 得到缓存信息
  *
  * @param string $id 缓存id
  * @return string|array 缓存数据
  */
 static function load($id)
 {
  $self = & self::getInstance();
  $time = time();
  //检测缓存是否存在
  if (!$self->test($id)) {
   // The cache is not hit !
   return false;
  }
   
  //全部清空识别文件
  $clearFile = $self->_file(self::CLEAR_ALL_KEY);
   
  $file = $self->_file($id);
   
  //判断缓存是否已被全部清除
  if(is_file($clearFile) && filemtime($clearFile) > filemtime($file))
  {
   return false;
  }
  
  $data = $self->_fileGetContents($file);
  if(empty($data[self::FILE_LIFE_KEY]) || $time < $data[self::FILE_LIFE_KEY]) {
   unset($data[self::FILE_LIFE_KEY]); 
   return $data;   
  }
  return false;
 } 
  
 /**
  * 写入缓存文件
  *
  * @param string $file 缓存路径
  * @param string $string 缓存信息
  * @return boolean true 成功
  */
 protected function _filePutContents($file, $string)
 {
  $self = & self::getInstance();
  $result = false;
  $f = @fopen($file, 'ab+');
  if ($f) {
   if ($self->_options['file_locking']) @flock($f, LOCK_EX);
   fseek($f, 0);
   ftruncate($f, 0);
   $tmp = @fwrite($f, $string);
   if (!($tmp === false)) {
    $result = true;
   }
   @fclose($f);
  }
  @chmod($file, $self->_options['cache_file_umask']);
  return $result;
 }
  
 /**
  * 格式化后的缓存文件路径
  *
  * @param string $id 缓存id
  * @return string 缓存文件名(包括路径)
  */
 protected function _file($id)
 {
  $self = & self::getInstance();
  $fileName = $self->_idToFileName($id);
  return $self->_options['cache_dir'] . $fileName;
 } 
  
 /**
  * 格式化后的缓存文件名字
  *
  * @param string $id 缓存id
  * @return string 缓存文件名
  */
 protected function _idToFileName($id)
 {
  $self = & self::getInstance();
  $self->_id = $id;
  $prefix = $self->_options['file_name_prefix'];
  $result = $prefix . '---' . $id;
  return $result;
 } 
  
 /**
  * 判断缓存是否存在
  *
  * @param string $id Cache id
  * @return boolean True 缓存存在 False 缓存不存在
  */
 static function test($id)
 {
  $self = & self::getInstance();
  $file = $self->_file($id);
   
  if (!is_file($file)) {
   return false;
  }
   
  return true;
 }
  
 /**
  * 得到缓存信息
  *
  * @param string $file 缓存路径
  * @return string 缓存内容
  */
 protected function _fileGetContents($file)
 {
  if (!is_file($file)) {
   return false;
  }
  return include $file;
 }  
  
 /**
  * 清除所有缓存
  * 
  * @return void
  */ 
 static function clear()
 {
  $self = & self::getInstance();
  $self->save('CLEAR_ALL',self::CLEAR_ALL_KEY); 
 } 
  
 /**
  * 清除一条缓存
  * 
  * @param string cache id 
  * @return void
  */ 
 static function del($id)
 {
  $self = & self::getInstance();
  if(!$self->test($id)){
   // 该缓存不存在
   return false;
  }
  $file = $self->_file($id);
  return unlink($file);
 } 
}

存入数据

<"htmlcode">
<"htmlcode">
<"htmlcode">
//在执行所有cache_func前调用

$_options = array(
 'cache_dir' => './cache', //缓存文件目录
 'file_name_prefix' => 'cache',//缓存文件前缀
 'file_life' => 100000, //缓存文件生命
);
cache::setOptions($options);

//再执行 就会按着新配置信息执行,否则是默认信息
cache::save($arr,'arr');
//就是这个方法 貌似不合理 望大家指点

以上就是本文的全部内容,希望对大家的学习有所帮助。

标签:
php,文件缓存

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?