Skip to content

缓存添加 PostgreSQL 支持 #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public static function config(Typecho_Widget_Helper_Form $form)
'redis' => _t('Redis'),
'memcached' => _t('Memcached'),
'mysql' => _t('MySQL'),
'sqlite' => _t('SQLite')
'sqlite' => _t('SQLite'),
'postgres' => _t('PostgreSQL')
);
$t = new Typecho_Widget_Helper_Form_Element_Radio(
'cachetype',
Expand All @@ -128,7 +129,7 @@ public static function config(Typecho_Widget_Helper_Form $form)
null,
'6379',
_t('缓存服务端口'),
_t('默认端口 memcache: 11211, Redis: 6379, Mysql: 3306')
_t('默认端口 memcache: 11211, Redis: 6379, Mysql: 3306, PostgreSQL: 5432')
);
$form->addInput($t);

Expand Down
52 changes: 52 additions & 0 deletions driver/postgres.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
class MetingCache implements MetingCacheI
{
private $db = null;
public function __construct($option)
{
$this->db = Typecho_Db::get();
$dbname = $this->db->getPrefix() . 'metingcache';
$this->install();
$this->db->query($this->db->delete('table.metingcache')->where('time <= ?', time()));
}
public function install()
{
$sql = '
CREATE TABLE IF NOT EXISTS "' . $this->db->getPrefix() . 'metingcache' . '" (
"key" varchar(32) NOT NULL PRIMARY KEY,
"data" text,
"time" NUMERIC(20) DEFAULT NULL
);';
$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!');
}
}
}