APlayer-Typecho/driver/sqlite.class.php
metowolf b66f1b0957 🍒 支持memcached缓存
- 支持 memcached 缓存
 - 支持缓存检测
 - 支持歌词翻译
 - 升级 Meting 1.5.2
   - 不再强制 BC Math 扩展 #44
   - 网易云音乐支持海外主机
   - 修复 OpenSSL/mcrypt 兼容
 - 修复 QQ 音乐歌单识别 #43
 - 修复一些 bug
2018-03-08 14:18:32 +08:00

66 lines
1.9 KiB
PHP

<?php
class MetingCache implements MetingCacheI
{
private $db = null;
public function __construct($option)
{
$this->db = Typecho_Db::get();
$dbname = $this->db->getPrefix() . 'metingcache';
$sql = "SELECT name FROM sqlite_master WHERE type='table' AND name= '" . $dbname . "'";
if (count($this->db->fetchAll($sql)) == 0) {
$this->install();
} else {
$this->db->query($this->db->delete('table.metingcache')->where('time <= ?', time()));
}
}
public function install()
{
$sql = '
DROP TABLE IF EXISTS `%dbname%`;
CREATE TABLE `%dbname%` (
`key` varchar(32) NOT NULL PRIMARY KEY,
`data` text,
`time` int(20) DEFAULT NULL
);';
$dbname = $this->db->getPrefix() . 'metingcache';
$search = array('%dbname%');
$replace = array($dbname);
$sql = str_replace($search, $replace, $sql);
$sqls = explode(';', $sql);
foreach ($sqls as $sql) {
$this->db->query($sql);
}
}
public function set($key, $value, $expire = 86400)
{
$this->db->query($this->db->insert('table.metingcache')->rows(array(
'key' => md5($key),
'data' => $value,
'time' => time() + $expire
)));
}
public function get($key)
{
$rs = $this->db->fetchRow($this->db->select('data')->from('table.metingcache')->where('key = ?', md5($key)));
if (count($rs) == 0) {
return false;
} else {
return $rs['data'];
}
}
public function flush()
{
return $this->db->query($this->db->delete('table.metingcache'));
}
public function check()
{
$number = uniqid();
$this->set('check', $number, 60);
$cache = $this->get('check');
if ($number != $cache) {
throw new Exception('Cache Test Fall!');
}
}
}