mirror of https://github.com/owen0o0/getFavicon
如果缓存是默认图标,设置过期时间为12小时
parent
2cceb8d816
commit
c0a453e084
55
get.php
55
get.php
|
@ -15,29 +15,42 @@ require "./Favicon.php";
|
||||||
|
|
||||||
$favicon = new \Jerrybendy\Favicon\Favicon;
|
$favicon = new \Jerrybendy\Favicon\Favicon;
|
||||||
|
|
||||||
|
|
||||||
|
/* ------ 参数设置 ------ */
|
||||||
|
|
||||||
|
$defaultIco='favicon.png'; //默认图标路径
|
||||||
|
$expire = 2592000; //缓存有效期30天, 单位为:秒,为0时不缓存
|
||||||
|
|
||||||
|
/* ------ 参数设置 ------ */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认图标
|
* 设置默认图标
|
||||||
*/
|
*/
|
||||||
$favicon->setDefaultIcon('favicon.png');
|
$favicon->setDefaultIcon($defaultIco);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 检测URL参数
|
* 检测URL参数
|
||||||
*/
|
*/
|
||||||
$url = $_GET['url'];
|
$url = $_GET['url'];
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 格式化 URL, 并尝试读取缓存
|
* 格式化 URL, 并尝试读取缓存
|
||||||
*/
|
*/
|
||||||
$formatUrl = $favicon->formatUrl($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) {
|
foreach ($favicon->getHeader() as $header) {
|
||||||
@header($header);
|
@header($header);
|
||||||
}
|
}
|
||||||
|
echo Cache::get($formatUrl,$defaultMD5,$expire);
|
||||||
echo Cache::get($formatUrl);
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +59,11 @@ if (Cache::get($formatUrl) !== NULL) {
|
||||||
*/
|
*/
|
||||||
$content = $favicon->getFavicon($formatUrl, TRUE);
|
$content = $favicon->getFavicon($formatUrl, TRUE);
|
||||||
|
|
||||||
Cache::set($formatUrl, $content, 86400);
|
if( md5($content) == $defaultMD5 ){
|
||||||
|
$expire = 43200; //如果返回默认图标,设置过期时间为12小时。Cache::get 方法中需同时修改
|
||||||
|
}
|
||||||
|
|
||||||
|
Cache::set($formatUrl, $content, $expire);
|
||||||
|
|
||||||
foreach ($favicon->getHeader() as $header) {
|
foreach ($favicon->getHeader() as $header) {
|
||||||
@header($header);
|
@header($header);
|
||||||
|
@ -54,7 +71,7 @@ foreach ($favicon->getHeader() as $header) {
|
||||||
|
|
||||||
echo $content;
|
echo $content;
|
||||||
exit;
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,9 +83,11 @@ class Cache
|
||||||
* 获取缓存的值, 不存在时返回 null
|
* 获取缓存的值, 不存在时返回 null
|
||||||
*
|
*
|
||||||
* @param $key
|
* @param $key
|
||||||
|
* @param $default 默认图片
|
||||||
|
* @param $expire 过期时间
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function get($key)
|
public static function get($key, $default, $expire)
|
||||||
{
|
{
|
||||||
$dir = 'cache'; //图标缓存目录
|
$dir = 'cache'; //图标缓存目录
|
||||||
|
|
||||||
|
@ -76,12 +95,16 @@ class Cache
|
||||||
$f = parse_url($key)['host'];
|
$f = parse_url($key)['host'];
|
||||||
|
|
||||||
$a = $dir . '/' . $f . '.txt';
|
$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;
|
return null;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return file_get_contents($a);
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -91,11 +114,10 @@ class Cache
|
||||||
*
|
*
|
||||||
* @param $key
|
* @param $key
|
||||||
* @param $value
|
* @param $value
|
||||||
* @param $expire
|
* @param $expire 过期时间
|
||||||
*/
|
*/
|
||||||
public static function set($key, $value, $expire)
|
public static function set($key, $value, $expire)
|
||||||
{
|
{
|
||||||
|
|
||||||
$dir = 'cache'; //图标缓存目录
|
$dir = 'cache'; //图标缓存目录
|
||||||
|
|
||||||
//$f = md5( strtolower( $key ) );
|
//$f = md5( strtolower( $key ) );
|
||||||
|
@ -106,8 +128,7 @@ class Cache
|
||||||
//如果缓存目录不存在则创建
|
//如果缓存目录不存在则创建
|
||||||
if (!is_dir($dir)) mkdir($dir,0777,true) or die('创建缓存目录失败!');
|
if (!is_dir($dir)) mkdir($dir,0777,true) or die('创建缓存目录失败!');
|
||||||
|
|
||||||
$t = 2592000; // 缓存有效期30天, 这里单位:秒
|
if ( !is_file($a) || (time() - filemtime($a)) > $expire ) {
|
||||||
if ( !is_file($a) || (time() - filemtime($a)) > $t ) {
|
|
||||||
$imgdata = fopen($a, "w") or die("Unable to open file!"); //w 重写 a追加
|
$imgdata = fopen($a, "w") or die("Unable to open file!"); //w 重写 a追加
|
||||||
fwrite($imgdata, $value);
|
fwrite($imgdata, $value);
|
||||||
fclose($imgdata);
|
fclose($imgdata);
|
||||||
|
|
Loading…
Reference in New Issue