Skip to content

Commit

Permalink
代码重构:将默认的文件缓存变为数组缓存(重启后记录清空,但支持了加密数据的缓存)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuanyanwow committed Feb 20, 2020
1 parent f4e730e commit d3861c7
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 115 deletions.
17 changes: 0 additions & 17 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class Config extends SplBean
{
protected $size;
protected $listUrl;
protected $temDir;
protected $resendUrl;

public function getSize()
Expand All @@ -31,22 +30,6 @@ public function setistUrl($listUrl)
$this->listUrl = $listUrl;
}

/**
* @return mixed
*/
public function getTemDir()
{
return $this->temDir;
}

/**
* @param mixed $temDir
*/
public function setTemDir($temDir): void
{
$this->temDir = $temDir;
}

/**
* @param mixed $resendUrl
*/
Expand Down
82 changes: 0 additions & 82 deletions src/LogData.php

This file was deleted.

25 changes: 14 additions & 11 deletions src/Monitor.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Created by PhpStorm.
* 监控执行门面
* User: Siam
* Date: 2019/7/31
* Time: 17:39
Expand All @@ -9,28 +9,31 @@
namespace Siam\HttpMonitor;


use EasySwoole\Component\CoroutineRunner\Runner;
use EasySwoole\Component\Singleton;
use EasySwoole\HttpClient\Exception\InvalidUrl;
use Siam\HttpMonitor\Runner\ArrayRunner;

class Monitor
{
use Singleton;

/**
* @var LogData
* @var RunnerAbstract
*/
private $logData;
private $runner;
/** @var Config $config */
private $config;
/** @var array 过滤记录的列表 */
private $filter;

public function __construct(Config $config)
public function __construct(Config $config, RunnerAbstract $runner = null)
{
$this->config = $config;
$this->logData = new LogData();
$this->logData->setTemDir($config->getTemDir());
$this->logData->setSize($config->getSize());
if ($runner === null){
$runner = new ArrayRunner($config);
}
$this->config = $config;
$this->runner = $runner;
}


Expand All @@ -44,7 +47,7 @@ public function log($data)
if (isset($data['server']['request_uri']) && in_array($data['server']['request_uri'], $this->filter)){
return false;
}
$this->logData->addOne($data);
$this->runner->addOne($data);
}

/**
Expand All @@ -61,7 +64,7 @@ public function addFilter($router)
*/
public function getList()
{
$list = $this->logData->getData();
$list = $this->runner->getData();
foreach ($list as $key => $value)
{
$list[$key]['id'] = $key;
Expand Down Expand Up @@ -94,7 +97,7 @@ public function listView()
*/
public function resend($id)
{
$data = $this->logData->getData()[$id];
$data = $this->runner->getData()[$id];
// 复发请求
$url = 'http://127.0.0.1:'.$data['server']['server_port'].$data['server']['request_uri'];
$client = new \EasySwoole\HttpClient\HttpClient();
Expand Down
57 changes: 57 additions & 0 deletions src/Runner/ArrayRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* 数组缓存驱动
* User: Siam
* Date: 2019/7/31
* Time: 17:56
*/

namespace Siam\HttpMonitor\Runner;

use Siam\HttpMonitor\Config;
use Siam\HttpMonitor\RunnerAbstract;

class ArrayRunner extends RunnerAbstract
{
protected $config;
private $data = [];
private $dataKey = [];


public function __construct(Config $config)
{
$this->config = $config;
}

public function addOne($data)
{
$dataKey = time();
$this->checkSize();

$this->dataKey[] = $dataKey;
$this->data[$dataKey] = $data;
}

public function getData($dataKey = null)
{
if ($dataKey === null){
return $this->data;
}
if (isset($this->data[$dataKey])){
return $this->data[$dataKey];
}
return null;
}

public function checkSize()
{
if (count($this->dataKey) >= $this->config->getSize()){
$dataKey = array_pop($this->dataKey);
if ($this->data[$dataKey]){
unset($this->data[$dataKey]);
}
}
}


}
22 changes: 22 additions & 0 deletions src/RunnerAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* 执行者抽象
* User: Siam
* Date: 2020/2/20 0020
* Time: 12:32
*/

namespace Siam\HttpMonitor;

abstract class RunnerAbstract
{
protected $config;

abstract public function __construct(Config $config);

abstract public function addOne($data);

abstract public function getData($dataKey = null);

abstract public function checkSize();
}
7 changes: 4 additions & 3 deletions tests/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require "../vendor/autoload.php";
use Siam\HttpMonitor\Config;
use Siam\HttpMonitor\Monitor;
use Swoole\Http\Request;


$config = new Config([
Expand All @@ -33,7 +34,7 @@
'worker_num' => 4, //worker process num
]);

$http->on('request', function ($request, $response) {
$http->on('request', function (Request $request,\Swoole\Http\Response $response) {

Monitor::getInstance()->log([
'header' => $request->header,
Expand All @@ -56,11 +57,11 @@
return $response->end(Monitor::getInstance()->listView());
}
if ($request->server['path_info'] == '/resend' || $request->server['request_uri'] == '/resend') {
$content = $request->getBody()->__toString();
$content = $request->rawContent();
$content = json_decode($content, true);
return $response->end(Monitor::getInstance()->resend($content['id']));
}
$response->end($request->rawContent() ?? 'siam');
$response->end('siam');
});

$http->start();
4 changes: 2 additions & 2 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
// var_dump($list);

// 列表
$monitor->listView();
echo $monitor->listView();


// 复发请求
$monitor->resend();
// $monitor->resend();

0 comments on commit d3861c7

Please sign in to comment.