Skip to content

增加Redis密码支持 #10

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 2 commits 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
33 changes: 14 additions & 19 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public static function config(Typecho_Widget_Helper_Form $form)
$element = new Typecho_Widget_Helper_Form_Element_Text('port', null, '11211', '端口号', '端口号,memcache对应11211,Redis对应6379,其他类型随意填写');
$form->addInput($element);

$element = new Typecho_Widget_Helper_Form_Element_Text('auth', null, 'password', '连接密码', '连接密码,没有则不填');
$form->addInput($element);

$list = array('关闭', '开启');
$element = new Typecho_Widget_Helper_Form_Element_Radio('is_debug', $list, 0, '是否开启debug');
$form->addInput($element);
Expand Down Expand Up @@ -190,13 +193,11 @@ public static function C()
} else {
if (self::$plugin_config->is_debug) echo "Can't find cache!";
}

} catch (Exception $e) {
echo $e->getMessage();
}
// 先进行一次刷新
ob_flush();

}

/**
Expand Down Expand Up @@ -285,7 +286,6 @@ public static function S()
if (self::$plugin_config->is_debug) echo "Cache updated!\n";
self::set(self::$key, serialize($data));
}

}


Expand All @@ -302,7 +302,7 @@ public static function post_update($contents, $class)
//获取系统配置
$options = Helper::options();

if(!$options->plugin('TpCache')->cache_driver){
if (!$options->plugin('TpCache')->cache_driver) {
return;
}
//获取文章类型
Expand Down Expand Up @@ -348,10 +348,10 @@ public static function comment_update($comment)
// 获取评论的PATH_INFO
$path_info = $req->getPathInfo();
// 删除最后的 /comment就是需删除的key
$article_url = preg_replace('/\/comment$/i','',$path_info);
$article_url = preg_replace('/\/comment$/i', '', $path_info);

self::init($article_url);

self::delete($article_url);
}

Expand All @@ -361,7 +361,7 @@ public static function comment_update($comment)
* @return bool
* @throws Typecho_Plugin_Exception
*/
public static function init($pathInfo='')
public static function init($pathInfo = '')
{
if (is_null(self::$sys_config)) {
self::$sys_config = Helper::options();
Expand All @@ -374,15 +374,14 @@ public static function init($pathInfo='')
return false;
}

if(empty($pathInfo)){
if (empty($pathInfo)) {

if (is_null(self::$request)) {
self::$request = new Typecho_Request();
}

//获取路径信息
$pathInfo = self::$request->getPathInfo();

}
//判断是否需要缓存
if (!self::needCache($pathInfo)) return false;
Expand All @@ -397,7 +396,8 @@ public static function init($pathInfo='')
* @return bool
* @throws Typecho_Plugin_Exception
*/
public static function init_driver(){
public static function init_driver()
{
if (is_null(self::$cache)) {
$driver_name = self::$plugin_config->cache_driver;
$class_name = "typecho_$driver_name";
Expand All @@ -421,7 +421,6 @@ public static function set($path, $data)

public static function add($path, $data)
{

}

public static function get($path)
Expand All @@ -441,15 +440,11 @@ public static function delete($path, $del_home = null)
{
$prefixs = array(
'http'
. '://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] :
($_SERVER['SERVER_NAME'] . (in_array($_SERVER['SERVER_PORT'], array(80, 443))
? '' : ':' . $_SERVER['SERVER_PORT']))
),
. '://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'] . (in_array($_SERVER['SERVER_PORT'], array(80, 443))
? '' : ':' . $_SERVER['SERVER_PORT']))),
'https'
. '://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] :
($_SERVER['SERVER_NAME'] . (in_array($_SERVER['SERVER_PORT'], array(80, 443))
? '' : ':' . $_SERVER['SERVER_PORT']))
),
. '://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ($_SERVER['SERVER_NAME'] . (in_array($_SERVER['SERVER_PORT'], array(80, 443))
? '' : ':' . $_SERVER['SERVER_PORT']))),
);
$keys = array();
if (!is_array($path)) {
Expand Down
24 changes: 16 additions & 8 deletions driver/typecho_redis.class.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,46 @@
<?php

class typecho_redis implements TpCache{
class typecho_redis implements TpCache
{

private static $_instance = null;
private $redis = null;
private $host = '127.0.0.1';
private $port = 11211;
private $expire = 86400;
private $auth = 'password';

private function __construct($option=null) {
private function __construct($option = null)
{
$this->host = $option->host;
$this->port = $option->port;
$this->auth = $option->auth;
$this->expire = $option->expire + 0;
$this->init($option);
}

static public function getInstance($option) {
if (is_null ( self::$_instance ) || ! isset ( self::$_instance )) {
static public function getInstance($option)
{
if (is_null(self::$_instance) || !isset(self::$_instance)) {
self::$_instance = new self($option);
}
return self::$_instance;
}

public function init($option)
{
try{
try {
$this->redis = new Redis();
$this->redis->connect($this->host, $this->port);
}catch (Exception $e){
if (!empty($this->auth)) {
$this->redis->auth($this->auth);
}
} catch (Exception $e) {
echo $e->getMessage();
}
}

public function add($key, $value, $expire=null)
public function add($key, $value, $expire = null)
{
return $this->redis->set($key, $value, is_null($expire) ? $this->expire : $expire);
}
Expand All @@ -42,7 +50,7 @@ public function delete($key)
return $this->redis->delete($key);
}

public function set($key, $value, $expire=null)
public function set($key, $value, $expire = null)
{
return $this->redis->set($key, $value, is_null($expire) ? $this->expire : $expire);
}
Expand Down