Skip to content

Commit 2051844

Browse files
committed
Merge commit '6d052edfc7c0406bd886472b295afeaba3905ac8'
2 parents bcab894 + 6d052ed commit 2051844

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

AMP/Action.php

+45-36
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public function __construct($request, $response, $params = NULL)
2121

2222
}
2323

24-
2524

2625
public static function headlink()
2726
{
@@ -394,7 +393,6 @@ private function GetPostImg()
394393
}
395394
}
396395

397-
398396
try {
399397
list($width, $height, $type, $attr) = @getimagesize($img_url);
400398
$imgData=array(
@@ -524,7 +522,8 @@ private function substr_format($text, $length, $replace = '...', $encoding = 'UT
524522
}
525523
return $text;
526524
}
527-
525+
526+
528527
private function getSlugRule()
529528
{
530529
$router = explode('/', Helper::options()->routingTable['post']['url']);
@@ -538,48 +537,58 @@ private function getSlugRule()
538537
//For page_cacher
539538

540539
private function set($key, $cache){
541-
$installDb = $this->db;
542-
$time=(int)Helper::options()->plugin('AMP')->cacheTime;
543-
$expire = $time*60*60;
544-
if(is_array($cache)) $cache = json_encode($cache);
545-
$table = $this->tablename;
546-
$time = time();
547-
548-
$cache = addslashes($cache);
549-
$sql = "REPLACE INTO $table (`hash`,`cache`,`dateline`,`expire`) VALUES ('$key','$cache','$time','$expire')";
550-
$installDb->query($sql);
551-
540+
if(Helper::options()->plugin('AMP')->cacheTime>0) {
541+
$installDb = $this->db;
542+
$time = (int)Helper::options()->plugin('AMP')->cacheTime;
543+
$expire = $time * 60 * 60;
544+
if (is_array($cache)) $cache = json_encode($cache);
545+
$table = $this->tablename;
546+
$time = time();
547+
548+
$cache = addslashes($cache);
549+
$sql = "REPLACE INTO $table (`hash`,`cache`,`dateline`,`expire`) VALUES ('$key','$cache','$time','$expire')";
550+
$installDb->query($sql);
551+
}else{
552+
return null;
553+
}
552554
}
553555

554556
private function del($key){
555-
$installDb = $this->db;
556-
$tablename=$this->tablename;
557-
if(is_array($key)){
558-
foreach($key as $k=>$v){
559-
$this->del($v);
557+
if(Helper::options()->plugin('AMP')->cacheTime>0) {
558+
$installDb = $this->db;
559+
$tablename = $this->tablename;
560+
if (is_array($key)) {
561+
foreach ($key as $k => $v) {
562+
$this->del($v);
563+
}
564+
} else {
565+
if ($key == '*') {
566+
$installDb->query("DELETE FROM $tablename WHERE 1=1 ");
567+
} else {
568+
$delete = $installDb->delete($tablename)->where('hash = ?', $key)->limit(1);
569+
$installDb->query($delete);
570+
}
560571
}
561572
}else{
562-
if($key=='*'){
563-
$installDb->query("DELETE FROM $tablename WHERE 1=1 ");
564-
}else{
565-
$delete = $installDb->delete($tablename)->where('hash = ?', $key)->limit(1);
566-
$installDb->query($delete);
567-
}
573+
return null;
568574
}
569575
}
570576

571577
private function get($key){
572-
$installDb = $this->db;
573-
$tablename=$this->tablename;
574-
575-
$condition=$installDb->select('cache','dateline','expire')->from($tablename)->where('hash = ?', $key);
576-
577-
$row = $installDb->fetchRow($condition);
578-
if(!$row) return;
579-
if(time()-$row['dateline']>$row['expire']) $this->del($key);
580-
$cache = $row['cache'];
581-
$arr = json_decode($cache,true);
582-
return is_array($arr)?$arr:$cache;
578+
if(Helper::options()->plugin('AMP')->cacheTime>0) {
579+
$installDb = $this->db;
580+
$tablename = $this->tablename;
581+
582+
$condition = $installDb->select('cache', 'dateline', 'expire')->from($tablename)->where('hash = ?', $key);
583+
$row = $installDb->fetchRow($condition);
584+
if (!$row) return;
585+
if (time() - $row['dateline'] > $row['expire']) $this->del($key);
586+
$cache = $row['cache'];
587+
$arr = json_decode($cache, true);
588+
return is_array($arr) ? $arr : $cache;
589+
}else{
590+
return null;
591+
}
583592
}
584593

585594

AMP/Plugin.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
*
55
* @package AMP-MIP
66
* @author Holmesian
7-
* @version 0.6.1
7+
* @version 0.6.2
88
* @link https://holmesian.org
99
*/
1010
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
1111

1212
class AMP_Plugin implements Typecho_Plugin_Interface
1313
{
14-
private static $version = '0.6.1';
14+
private static $version = '0.6.2';
1515

1616
public static function activate()
1717
{
18+
$msg=self::install();
19+
1820
//挂载发布文章接口
1921
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->finishPublish = array('AMP_Action', 'sendRealtime');
2022
Typecho_Plugin::factory('Widget_Archive')->header = array('AMP_Action', 'headlink');
@@ -27,7 +29,7 @@ public static function activate()
2729
Helper::addRoute('mip_sitemap', '/mip_sitemap.xml', 'AMP_Action', 'mipsitemap');
2830
Helper::addRoute('clean_cache', '/clean_cache', 'AMP_Action', 'cleancache');
2931
Helper::addPanel(1, 'AMP/Links.php', 'AMP/MIP自动提交', '自动提交', 'administrator');
30-
$msg=self::install();
32+
3133
return $msg.'请进入设置填写接口调用地址';
3234
}
3335

@@ -54,7 +56,7 @@ public static function index()
5456
public static function config(Typecho_Widget_Helper_Form $form)
5557
{
5658

57-
$element = new Typecho_Widget_Helper_Form_Element_Text('cacheTime', null, '24', _t('缓存时间'), '单位:小时<br> v0.5.7起启用页面缓存,此项为缓存过期时间。如果需要重建缓存,请点击 <a href="' . Helper::options()->index . '/clean_cache">删除所有缓存</a>');
59+
$element = new Typecho_Widget_Helper_Form_Element_Text('cacheTime', null, '0', _t('缓存时间'), '单位:小时(设置成0表示关闭)<br> 此项为缓存过期时间,建议设置成24。如果需要重建缓存,请点击 <a href="' . Helper::options()->index . '/clean_cache">删除所有缓存</a>');
5860
$form->addInput($element);
5961

6062
$element = new Typecho_Widget_Helper_Form_Element_Text('baiduAPI', null, '', _t('MIP/AMP推送接口调用地址'), '请到http://ziyuan.baidu.com/mip/index获取接口调用地址。');
@@ -145,7 +147,11 @@ public static function call_me($type){//远程通知
145147
//Cache databse
146148
public static function DBsetup()
147149
{
150+
148151
$installDb = Typecho_Db::get();
152+
if(stristr($installDb->getAdapterName(),'mysql')== false){
153+
return('缓存不支持sqlite.');
154+
}
149155
$cacheTable = $installDb->getPrefix() . 'PageCache';
150156
try {
151157
$installDb->query("DROP TABLE IF EXISTS " . $cacheTable);

0 commit comments

Comments
 (0)