From c0a453e084e62f9b96feece16ec41d6f32794413 Mon Sep 17 00:00:00 2001 From: owen <32561126+owen0o0@users.noreply.github.com> Date: Tue, 26 Nov 2019 21:28:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A6=82=E6=9E=9C=E7=BC=93=E5=AD=98=E6=98=AF?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=9B=BE=E6=A0=87=EF=BC=8C=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E8=BF=87=E6=9C=9F=E6=97=B6=E9=97=B4=E4=B8=BA12=E5=B0=8F?= =?UTF-8?q?=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- get.php | 77 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 28 deletions(-) diff --git a/get.php b/get.php index e115244..8330f61 100644 --- a/get.php +++ b/get.php @@ -15,47 +15,64 @@ require "./Favicon.php"; $favicon = new \Jerrybendy\Favicon\Favicon; + +/* ------ 参数设置 ------ */ + +$defaultIco='favicon.png'; //默认图标路径 +$expire = 2592000; //缓存有效期30天, 单位为:秒,为0时不缓存 + +/* ------ 参数设置 ------ */ + + + /** - * 默认图标 + * 设置默认图标 */ -$favicon->setDefaultIcon('favicon.png'); +$favicon->setDefaultIcon($defaultIco); /** * 检测URL参数 */ $url = $_GET['url']; - /* * 格式化 URL, 并尝试读取缓存 */ $formatUrl = $favicon->formatUrl($url); -if (Cache::get($formatUrl) !== NULL) { +if($expire == 0){ + $favicon->getFavicon($formatUrl, false); + exit; +} +else{ + $defaultMD5 = md5(file_get_contents($defaultIco)); + if (Cache::get($formatUrl,$defaultMD5,$expire) !== NULL) { + foreach ($favicon->getHeader() as $header) { + @header($header); + } + echo Cache::get($formatUrl,$defaultMD5,$expire); + exit; + } + + /** + * 缓存中没有指定的内容时, 重新获取内容并缓存起来 + */ + $content = $favicon->getFavicon($formatUrl, TRUE); + + if( md5($content) == $defaultMD5 ){ + $expire = 43200; //如果返回默认图标,设置过期时间为12小时。Cache::get 方法中需同时修改 + } + + Cache::set($formatUrl, $content, $expire); foreach ($favicon->getHeader() as $header) { @header($header); } - echo Cache::get($formatUrl); + echo $content; exit; } -/** - * 缓存中没有指定的内容时, 重新获取内容并缓存起来 - */ -$content = $favicon->getFavicon($formatUrl, TRUE); - -Cache::set($formatUrl, $content, 86400); - -foreach ($favicon->getHeader() as $header) { - @header($header); -} - -echo $content; -exit; - - /** * 缓存类 @@ -66,9 +83,11 @@ class Cache * 获取缓存的值, 不存在时返回 null * * @param $key + * @param $default 默认图片 + * @param $expire 过期时间 * @return string */ - public static function get($key) + public static function get($key, $default, $expire) { $dir = 'cache'; //图标缓存目录 @@ -76,12 +95,16 @@ class Cache $f = parse_url($key)['host']; $a = $dir . '/' . $f . '.txt'; - $t = 2592000; // 缓存有效期30天, 这里单位:秒 - if ( !is_file($a) || (time() - filemtime($a)) > $t ) { + + $data = file_get_contents($a); + if( md5($data) == $default ){ + $expire = 43200; //如果返回默认图标,过期时间为12小时。 + } + if ( !is_file($a) || (time() - filemtime($a)) > $expire ) { return null; } else { - return file_get_contents($a); + return $data; } } @@ -91,11 +114,10 @@ class Cache * * @param $key * @param $value - * @param $expire + * @param $expire 过期时间 */ public static function set($key, $value, $expire) { - $dir = 'cache'; //图标缓存目录 //$f = md5( strtolower( $key ) ); @@ -106,8 +128,7 @@ class Cache //如果缓存目录不存在则创建 if (!is_dir($dir)) mkdir($dir,0777,true) or die('创建缓存目录失败!'); - $t = 2592000; // 缓存有效期30天, 这里单位:秒 - if ( !is_file($a) || (time() - filemtime($a)) > $t ) { + if ( !is_file($a) || (time() - filemtime($a)) > $expire ) { $imgdata = fopen($a, "w") or die("Unable to open file!"); //w 重写 a追加 fwrite($imgdata, $value); fclose($imgdata);