• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php文件缓存方法总结_PHP

php 搞代码 3年前 (2022-01-26) 18次浏览 已收录 0个评论

为大家分享很全的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() _file($id);    return $instance->_filePutContents($file, $cache); }  /**  * 清除一条缓存  *   * @param string cache id    * @return void  */  public static function delete($id) {  $instance = self::getInstance();    if(!$instance->has($id))  {   return false;  }  $file = $instance->_file($id);  //删除该缓存  return unlink($file); }  /**  * 判断缓存是否存在  *   * @param string $id cache_id  * @return boolean true 缓存存在 false 缓存不存在  */ public static function has($id) {  $instance = self::getInstance();  $file  = $instance->_file($id);    if(!is_file($file))  {   return false;  }  return true; }  /**  * 通过缓存id得到缓存信息路径  * @param string $id  * @return string 缓存文件路径  */ protected function _file($id) {  $instance = self::getInstance();  $fileNmae = $instance->_idToFileName($id);  return $instance->_options['cache_dir'] . $fileNmae; }   /**  * 通过id得到缓存信息存储文件名  *   * @param $id  * @return string 缓存文件名  */ protected function _idToFileName($id) {  $instance = self::getInstance();  $prefix = $instance->_options['file_name_prefix'];  return $prefix . '---' . $id; }  /**  * 通过filename得到缓存id  *   * @param $id  * @return string 缓存id  */ protected function _fileNameToId($fileName) {  $instance = self::getInstance();  $prefix = $instance->_options['file_name_prefix'];  return preg_replace('/^' . $prefix . '---(.*)$/', '$1', $fileName); }  /**  * 把数据写入文件  *   * @param string $file 文件名称  * @param array $contents 数据内容  * @return bool   */ protected function _filePutContents($file, $contents) {  if($this->_options['mode'] == 1)  {   $contents = serialize($contents);  }  else   {   $time = time();    $contents = "";  }    $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 functi<mark style="color:transparent">来4源gaodaimacom搞#代%码*网</mark><code>搞代gaodaima码</code>on 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 缓存存储方式/*   array ( 0 => 1, 1 => 2, 2 => 3, 3 => 34, 4 => 5, 5 => 6, 6 => 6, ), 'expire' => 0, 'mtime' => 1318224645,)?> *  *  */cache::setCacheMode('2');if(!$row = cache::get('zj2')){  $array = array(1,2,3,34,5,6,6); $row = cache::set('zj2',$array);}// cache::flush(); 清空所有缓存print_r($row);


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php文件缓存方法总结_PHP
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址