検索
ホームページphp教程PHP源码文件缓存类,增加前缀清理功能

php代码

<?php
/**
 * 文件缓存类,增加前缀清理功能
 * @author WeakSun*/
namespace Think\Cache\Driver;

use Think\Cache;

defined(&#39;THINK_PATH&#39;) or exit();

/**
 * 文件类型缓存类
 */
class File extends Cache {

	/**
	 * 架构函数
	 * @access public
	 */
	public function __construct($options = array()) {
		if (!empty($options)) {
			$this->options = $options;
		}
		$this->options[&#39;temp&#39;] = !empty($options[&#39;temp&#39;]) ? $options[&#39;temp&#39;] : C(&#39;DATA_CACHE_PATH&#39;);
		$this->options[&#39;prefix&#39;] = isset($options[&#39;prefix&#39;]) ? $options[&#39;prefix&#39;] : C(&#39;DATA_CACHE_PREFIX&#39;);
		$this->options[&#39;expire&#39;] = isset($options[&#39;expire&#39;]) ? $options[&#39;expire&#39;] : C(&#39;DATA_CACHE_TIME&#39;);
		$this->options[&#39;length&#39;] = isset($options[&#39;length&#39;]) ? $options[&#39;length&#39;] : 0;
		$this->options[&#39;temp&#39;] = rtrim($this->options[&#39;temp&#39;], &#39;\\/&#39;) . DIRECTORY_SEPARATOR;
		$this->init();
	}

	/**
	 * 初始化检查
	 * @access private
	 * @return boolean
	 */
	private function init() {
		// 创建应用缓存目录
		if (!is_dir($this->options[&#39;temp&#39;])) {
			mkdir($this->options[&#39;temp&#39;]);
		}
	}

	/**
	 * 取得变量的存储文件名
	 * @access private
	 * @param string $name 缓存变量名
	 * @return string
	 */
	private function filename($name) {
		$name = md5(C(&#39;DATA_CACHE_KEY&#39;) . $name);
		if (C(&#39;DATA_CACHE_SUBDIR&#39;)) {
			// 使用子目录
			$dir = &#39;&#39;;
			for ($i = 0; $i < C(&#39;DATA_PATH_LEVEL&#39;); $i++) {
				$dir .= $name{$i} . DIRECTORY_SEPARATOR;
			}
			if (!is_dir($this->options[&#39;temp&#39;] . $dir)) {
				mkdir($this->options[&#39;temp&#39;] . $dir, 0755, true);
			}
			$filename = $dir . $this->options[&#39;prefix&#39;] . $name . &#39;.tmp&#39;;
		} else {
			$filename = $this->options[&#39;prefix&#39;] . $name . &#39;.tmp&#39;;
		}
		return $this->options[&#39;temp&#39;] . $filename;
	}

	/**
	 * 读取缓存
	 * @access public
	 * @param string $name 缓存变量名
	 * @return mixed
	 */
	public function get($name) {
		$filename = $this->filename($name);
		if (!is_file($filename)) {
			return false;
		}
		N(&#39;cache_read&#39;, 1);
		$content = file_get_contents($filename);
		if (false !== $content) {
			$expire = (int) substr($content, 8, 12);
			if ($expire != 0 && time() > filemtime($filename) + $expire) {
				//缓存过期删除缓存文件
				unlink($filename);
				return false;
			}
			if (C(&#39;DATA_CACHE_CHECK&#39;)) {//开启数据校验
				$check = substr($content, 20, 32);
				$content = substr($content, 52);
				if ($check != md5($content)) {//校验错误
					return false;
				}
			} else {
				$content = substr($content, 20);
			}
			if (C(&#39;DATA_CACHE_COMPRESS&#39;) && function_exists(&#39;gzcompress&#39;)) {
				//启用数据压缩
				$content = gzuncompress($content);
			}
			$content = unserialize($content);
			return $content;
		} else {
			return false;
		}
	}

	/**
	 * 写入缓存
	 * @access public
	 * @param string $name 缓存变量名
	 * @param mixed $value  存储数据
	 * @param int $expire  有效时间 0为永久
	 * @return boolean
	 */
	public function set($name, $value, $expire = null) {
		N(&#39;cache_write&#39;, 1);
		if (is_null($expire)) {
			$expire = $this->options[&#39;expire&#39;];
		}
		$filename = $this->filename($name);
		$data = serialize($value);
		if (C(&#39;DATA_CACHE_COMPRESS&#39;) && function_exists(&#39;gzcompress&#39;)) {
			//数据压缩
			$data = gzcompress($data, 3);
		}
		if (C(&#39;DATA_CACHE_CHECK&#39;)) {//开启数据校验
			$check = md5($data);
		} else {
			$check = &#39;&#39;;
		}
		$data = "<?php\n//" . sprintf(&#39;%012d&#39;, $expire) . $check . $data;
		$result = file_put_contents($filename, $data);
		if ($result) {
			if ($this->options[&#39;length&#39;] > 0) {
				// 记录缓存队列
				$this->queue($name);
			}
			clearstatcache();
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 删除缓存
	 * @access public
	 * @param string $name 缓存变量名
	 * @return boolean
	 */
	public function rm($name) {
		if ($name == &#39;%&#39;) {
			return $this->clearDir($this->options[&#39;temp&#39;], true, $this->options[&#39;prefix&#39;]);
		} else {
			return $this->clearDir($this->filename($name));
		}
	}

	/**
	 * 清除缓存
	 * @access public
	 * @param string $name 缓存变量名
	 * @return boolean
	 */
	public function clear() {
		return $this->clearDir($this->options[&#39;temp&#39;]);
	}

	/**
	 * 清空目录
	 * @param string $dir 目录路径
	 * @param boolean $stay 是否保留目录
	 * @param string $pattern 文件筛选规则
	 * @return boolean
	 */
	protected function clearDir($dir, $stay = true, $pattern = null) {
		if (is_dir($dir)) {
			$dir = rtrim($dir, &#39;\\/&#39;) . DIRECTORY_SEPARATOR;
			$list = array_merge(glob($dir . &#39;*&#39;, GLOB_ONLYDIR), glob($dir . "{$pattern}*.*", GLOB_NOSORT));
			foreach ($list as $file) {
				$this->clearDir($file, !is_null($pattern), $pattern);
			}
			$stay || $stay = rmdir($dir);
		} elseif (is_file($dir)) {
			return unlink($dir);
		}
		return $stay;
	}

}
声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

PhpStorm Mac バージョン

PhpStorm Mac バージョン

最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター