forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Action.php
72 lines (65 loc) · 2.2 KB
/
Action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* MostCache Plugin
*
* @copyright Copyright (c) 2014 skylzl (http://www.woyoudian.com)
* @license GNU General Public License 2.0
*
*/
class MostCache_Action extends Typecho_Widget implements Widget_Interface_Do
{
private $db;
private $config;
private static $pluginName = 'MostCache';
private static $tableName = 'most_cache';
public function __construct($request, $response, $params = NULL)
{
parent::__construct($request, $response, $params);
$this->config = Helper::options()->plugin(self::$pluginName);
$this->db = Typecho_Db::get();
}
/**
*删除指定缓存
*
* @param string $hash
*/
public function del($hash){
$this->db->query($this->db->delete('table.'.self::$tableName)->where('hash = ?', $hash));
}
/**
* 修改缓存规则
*/
public function edit(){
if($_POST['preg']){
$preg = $_POST['preg'];
$select = $this->db->query("SELECT * FROM ".$this->db->getPrefix()."options WHERE name ='plugin:MostCache'");
$oldConfig = $this->db->fetchAll($select);
$newConfig = unserialize($oldConfig[0]['value']);
unset($newConfig['cacheType']);
$newConfig['cacheType'] = $preg;
$this->db->query($this->db->update('table.options')->rows(array('value' => serialize($newConfig)))->where('name = ?', 'plugin:MostCache'));
}
}
/**
* 重设缓存
*/
public function resetCache(){
if($this->config->cacheMode=='Mysql'){#1.Mysql模式
$table = $this->db->getPrefix().self::$tableName;
$this->db->query("TRUNCATE TABLE $table ");
}else{#2.memcache模式
$mc = new Memcache;
$mc->connect($this->config->mem_server, $this->config->mem_prot) or die ("连接memcached服务器失败");
$mc->flush();
}
$this->request->throwJson('success');
}
public function action(){
$this->widget('Widget_User')->pass('administrator');
$this->on($this->request->is('del'))->del($this->request->del);
$this->on($this->request->is('resetCache'))->resetCache();
$this->on($this->request->is('edit'))->edit();
$this->response->goBack();
}
}
?>