Skip to content

Commit

Permalink
增加 Redis 缓存驱动 增加 Redis 数据库
Browse files Browse the repository at this point in the history
  • Loading branch information
tianye committed Aug 2, 2016
1 parent 049beb5 commit 249a68d
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 33 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
],
"require": {
"php": ">=5.4",
"guzzlehttp/guzzle": "^5.3"
"guzzlehttp/guzzle": "^5.3",
"predis/predis": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
Expand Down
6 changes: 5 additions & 1 deletion demo/authorized.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
$config = new Config();
$config->init(['component_app_id' => '第三方平台appId', 'component_app_secret' => '第三方平台appSecret', 'component_app_token' => '第三方平台appToken', 'component_app_key' => '第三方平台appKey']);

$authorized = new Authorized();
$cacheDriver = new \OpenOauth\Core\CacheDriver\RedisDriver(['host' => '127.0.0.1', 'port' => '6379', 'database' => '1']);
$databaseDriver = new \OpenOauth\Core\DatabaseDriver\RedisDriver(['host' => '127.0.0.1', 'port' => '6379', 'database' => '1']);

$authorized = new Authorized($cacheDriver, $databaseDriver);

$authorized->getAuthHTML('index.php');
7 changes: 5 additions & 2 deletions demo/notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
$config = new Config();
$config->init(['component_app_id' => '第三方平台appId', 'component_app_secret' => '第三方平台appSecret', 'component_app_token' => '第三方平台appToken', 'component_app_key' => '第三方平台appKey']);

$decryption = new Decryption();
$cacheDriver = new \OpenOauth\Core\CacheDriver\RedisDriver(['host' => '127.0.0.1', 'port' => '6379', 'database' => '1']);
$databaseDriver = new \OpenOauth\Core\DatabaseDriver\RedisDriver(['host' => '127.0.0.1', 'port' => '6379', 'database' => '1']);

$decryption = new Decryption($cacheDriver, $databaseDriver);
$xml_array = $decryption->decryptionNoticeXML();

Tools::dataRecodes('xml_array', $xml_array, 'notice');

$notify_processing = new NotifyProcessing();
$notify_processing = new NotifyProcessing($cacheDriver, $databaseDriver);
switch ($xml_array['InfoType']) {
case 'component_verify_ticket':
//每10分钟 接收一次微信推送过来 当前 第三方平台的 ticket 并且缓存
Expand Down
5 changes: 4 additions & 1 deletion demo/oauth_demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
$config = new Config();
$config->init(['component_app_id' => '第三方平台appId', 'component_app_secret' => '第三方平台appSecret', 'component_app_token' => '第三方平台appToken', 'component_app_key' => '第三方平台appKey']);

$open_auth = new OpenAuth('授权的服务号appId');
$cacheDriver = new \OpenOauth\Core\CacheDriver\RedisDriver(['host' => '127.0.0.1', 'port' => '6379', 'database' => '1']);
$databaseDriver = new \OpenOauth\Core\DatabaseDriver\RedisDriver(['host' => '127.0.0.1', 'port' => '6379', 'database' => '1']);

$open_auth = new OpenAuth($cacheDriver, $databaseDriver, '授权的服务号appId');

//snsapi_base 小授权 snsapi_userinfo 大授权
$user = $open_auth->authorize(null, 'snsapi_userinfo');
Expand Down
2 changes: 1 addition & 1 deletion src/Core/CacheDriver/FileDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function _get($name)
* 根据缓存名 设置缓存值和超时时间.
*
* @param string $name 缓存名
* @param mi $value 缓存值
* @param void $value 缓存值
* @param int $expires 超时时间
*
* @return boolean;
Expand Down
39 changes: 24 additions & 15 deletions src/Core/CacheDriver/RedisDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace OpenOauth\Core\CacheDriver;

use OpenOauth\Core\CacheDriver\BaseDriver;
use OpenOauth\Core\Config;
use Predis\Client;

/**
* 文件缓存驱动.
Expand All @@ -13,25 +15,31 @@ class RedisDriver extends BaseDriver
private $redis;
private $pre;

public function __construct()
public function __construct($configs = [], $dir = 'Cache:OpenOauth:')
{
$pre = 'WeixinApi:';
parent::__construct($pre);
parent::__construct($dir);

if (!class_exists('redis')) {
exit('Redis初始化连接失败');
}
$this->pre = $this->cacheDir;

$config = $configs + ['host' => '127.0.0.1', 'port' => '6379', 'database' => '0', 'scheme' => 'tcp', 'auth' => null];

ini_set('default_socket_timeout', 50);//socket连接超时时间;
$this->redis = new \redis();
// 如果用框架 此处最好走配置文件
$host = '127.0.0.1';
$port = '6379';
$this->pre = $pre;
$link = $this->redis->connect($host, $port, 40);

if (!$link) {
exit('Redis初始化连接失败');

$this->redis = new Client(
[
'scheme' => $config['scheme'],
'host' => $config['host'],
'port' => $config['port'],
]);

$this->redis->select($config['database']);

if (!empty($config['auth'])) {
$this->redis->auth($config['auth']);
}

if (!$this->redis) {
exit('Redis初始化连接失败-cache');
}
}

Expand Down Expand Up @@ -66,6 +74,7 @@ public function _set($name, $value, $expires)
{
$name = $this->createFileName($name);
$data = $this->packData($value);

if ($expires === 0) {
return $this->redis->set($name, $data);
}
Expand Down
9 changes: 6 additions & 3 deletions src/Core/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace OpenOauth\Core;

use OpenOauth\Core\CacheDriver\BaseDriver as CacheBaseDriver;
use OpenOauth\Core\DatabaseDriver\BaseDriver as DatabaseBaseDriver;

use OpenOauth\Core\CacheDriver\FileDriver as CacheFileDriver;
use OpenOauth\Core\DatabaseDriver\FileDriver as DatabaseFileDriver;
use OpenOauth\Core\Config;
Expand All @@ -24,10 +27,10 @@ class Core
* Core constructor.
*
*/
function __construct()
public function __construct(CacheBaseDriver $cacheDriver = null, DatabaseBaseDriver $databaseDriver = null)
{
self::$cacheDriver = new CacheFileDriver(dirname(dirname(dirname(__FILE__))) . '/cache');
self::$databaseDriver = new DatabaseFileDriver(dirname(dirname(dirname(__FILE__))) . '/database');
self::$cacheDriver = empty($cacheDriver) ? new CacheFileDriver(dirname(dirname(dirname(__FILE__))) . '/cache') : $cacheDriver;
self::$databaseDriver = empty($databaseDriver) ? new DatabaseFileDriver(dirname(dirname(dirname(__FILE__))) . '/database') : $databaseDriver;

$configs = Config::$configs;
try {
Expand Down
116 changes: 116 additions & 0 deletions src/Core/DatabaseDriver/RedisDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace OpenOauth\Core\DatabaseDriver;

use OpenOauth\Core\DatabaseDriver\BaseDriver;
use OpenOauth\Core\Config;
use Predis\Client;

/**
* 文件缓存驱动.
*
*/
class RedisDriver extends BaseDriver
{
private $redis;
private $pre;

public function __construct($configs = [], $dir = 'Database:OpenOauth:')
{
parent::__construct($dir);

$this->pre = $this->databaseDir;

$config = $configs + ['host' => '127.0.0.1', 'port' => '6379', 'database' => '0', 'scheme' => 'tcp', 'auth' => null];

ini_set('default_socket_timeout', 50);//socket连接超时时间;

$this->redis = new Client(
[
'scheme' => $config['scheme'],
'host' => $config['host'],
'port' => $config['port'],
]);

$this->redis->select($config['database']);

if (!empty($config['auth'])) {
$this->redis->auth($config['auth']);
}

if (!$this->redis) {
exit('Redis初始化连接失败-database');
}
}

/**
* 根据缓存名获取缓存内容.
*
* @param string $name
*
* @return bool|mixed|string
*/
public function _get($name)
{
$name = $this->createFileName($name);
$data = $this->redis->get($name);
if ($data) {
$data = $this->unpackData($data);
}

return $data;
}

/**
* 根据缓存名 设置缓存值和超时时间.
*
* @param string $name 缓存名
* @param void $value 缓存值
*
* @return boolean;
*/
public function _set($name, $value)
{
$name = $this->createFileName($name);
$data = $this->packData($value);

return $this->redis->set($name, $data);
}

/**
* 数据打包.
*
* @param void $data 缓存值
* @param int $expires 超时时间
*
* @return string
*/
private function packData($data)
{
return serialize($data);
}

/**
* 数据解包.
*
* @param $data
*
* @return mixed
*/
private function unpackData($data)
{
return unserialize($data);
}

/**
* 创建缓存文件名.
*
* @param string $name 缓存名
*
* @return string
*/
private function createFileName($name)
{
return $this->pre . md5($name);
}
}
7 changes: 0 additions & 7 deletions src/Core/Http/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ public static function _post($apiUrl, $data)
}
}

// $php_version = explode('-', phpversion());
// $php_version = $php_version[0];
// if (strnatcasecmp($php_version, '5.6.0') >= 0) {
// curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
// }

curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HEADER, true);
Expand All @@ -43,7 +37,6 @@ public static function _post($apiUrl, $data)
$body = $res;
if ($http_code == 200) {
list($header, $body) = explode("\r\n\r\n", $res, 2);
//list($header, $body) = explode("keep-alive", $res, 2);
$header = http_parse_headers($header);
}

Expand Down
6 changes: 4 additions & 2 deletions src/OpenAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use OpenOauth\Core\Core;
use OpenOauth\Core\Http\Http;
use OpenOauth\Core\CacheDriver\BaseDriver as CacheBaseDriver;
use OpenOauth\Core\DatabaseDriver\BaseDriver as DatabaseBaseDriver;

/**
* 微信Auth相关接口.
Expand All @@ -23,9 +25,9 @@ class OpenAuth extends Core
*
* @param $authorized_app_id
*/
public function __construct($authorized_app_id)
public function __construct(CacheBaseDriver $cacheDriver = null, DatabaseBaseDriver $databaseDriver = null, $authorized_app_id)
{
parent::__construct();
parent::__construct($cacheDriver, $databaseDriver);

$this->authorized_app_id = $authorized_app_id;
}
Expand Down

0 comments on commit 249a68d

Please sign in to comment.