Skip to content

Commit

Permalink
Merge pull request #4 from huizhang001/master
Browse files Browse the repository at this point in the history
Feat: 可设置每个进程最大所占内存大小
  • Loading branch information
kiss291323003 authored Oct 24, 2019
2 parents 18776d2 + c0d1e2c commit 44fdbdb
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 26 deletions.
126 changes: 101 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
# EasySwoole Keyword组件
# 关键词检测服务-Keyword

## 简介

#### 能做什么?

此组件可用来进行实时关键词检测,异步数据检测清洗等,并且已经提升为
Easyswoole的多进程关键词服务,服务内以文件方式加载词库,并且支持实时
增、删关键词。

`感谢Easyswoole开发组其它小伙伴们的指导和AbelZhou开源的字典树供我学习和集成到关键词服务组件中`

`感谢Easyswoole开发组的其它小伙伴的耐心指导和AbelZhou开源的字典树供我学习`

## 使用
Keyword组件底层围绕字典树并基于UnixSock通讯和自定义进程实现的关键词检测服务,开发本组件的目的是帮小伙伴们快速部署关键词检测服务,尤其是对于内容型产品尤为重要。

#### 1. 下载
::: warning
此组件稳定后,会尝试使用AC自动机或其它检测方式,提供底层可配置化检测服务
:::

## 安装

```
composer require easyswoole/keyword
```
#### 1. 准备词库文件
第一列为关键词,以"\t"分割,后面的参数全为其它信息,会当关键词命中的时候将其它信息返回

## 准备词库

第一列为关键词,其它列当命中关键词时会相应返回

```
我是
我叫
@es@其它信息1@es@其它信息2
我是@es@其它信息1
我叫@es@其它信息1@es@其它信息2@es@其它信息3
```

#### 3. EasyswooleEvent.php
## 代码示例

```
<?php
Expand All @@ -53,8 +51,11 @@ class EasySwooleEvent implements Event
{
// TODO: Implement mainServerCreate() method.
KeywordServer::getInstance()
->setMaxMem('1024M')
->setProcessNum(5)
->setServerName('Easyswoole 关键词检测')
->setTempDir(EASYSWOOLE_TEMP_DIR)
->setKeywordPath('/Users/xx/sites/xx/keyword.txt')
->setKeywordPath('/Users/xx/xx/xx/keyword.txt')
->attachToServer(ServerManager::getInstance()
->getSwooleServer());
}
Expand All @@ -74,10 +75,9 @@ class EasySwooleEvent implements Event
// TODO: Implement afterAction() method.
}
}
```

#### 4. 输出结果
## 命中结果

```
array(3) {
Expand All @@ -86,7 +86,11 @@ array(3) {
["keyword"]=>
string(3) "我"
["other"]=>
array(0) {
array(2) {
[0]=>
string(13) "其它信息1"
[1]=>
string(13) "其它信息2"
}
["count"]=>
int(1)
Expand All @@ -96,7 +100,13 @@ array(3) {
["keyword"]=>
string(6) "我叫"
["other"]=>
array(0) {
array(3) {
[0]=>
string(13) "其它信息1"
[1]=>
string(13) "其它信息2"
[2]=>
string(13) "其它信息3"
}
["count"]=>
int(1)
Expand All @@ -106,11 +116,77 @@ array(3) {
["keyword"]=>
string(16) "我叫Easyswoole"
["other"]=>
array(0) {
array(1) {
[0]=>
string(12) "附加信息"
}
["count"]=>
int(1)
}
}
```
::: warning
keyword:命中的关键词,other:为关键词其它信息,count:检测文本中的命中次数
:::

## 支持的方法

#### KeywordServer

设置临时目录
```
public function setTempDir(string $tempDir): KeywordServer
```

设置进程数量,默认3
```
public function setProcessNum(int $num): KeywordServer
```

设置每个进程最多所占内存大小
```
public function setMaxMem(string $maxMem='512M')
```

设置UnixSocket的Backlog队列长度
```
public function setBacklog(?int $backlog = null)
```

设置服务名称
```
public function setServerName(string $serverName): KeywordServer
```

设置词库路径
```
public function setKeywordPath(string $keywordPath): KeywordServer
```

绑定到当前主服务
```
function attachToServer(swoole_server $server)
```

#### KeywordClient

向字典树中添加关键词
```
public function append($keyword, array $otherInfo=[], float $timeout = 1.0)
```
::: warning
添加一次各进程间会自动同步
:::

向字典树中移除关键词
```
public function remove($keyword, float $timeout = 1.0)
```
::: warning
添加一次各进程间会自动同步
:::

搜索关键词
```
public function search($keyword, float $timeout = 1.0)
```
17 changes: 16 additions & 1 deletion src/KeywordServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,27 @@ class KeywordServer implements KeywordClientInter
private $processNum = 3;
private $run = false;
private $backlog = 256;
private $keywordPath='';
private $keywordPath = '';
private $maxMem = '512M';

function __construct()
{
$this->tempDir = getcwd();
}

/**
* 设置每个进程所占内存大小
*
* @param string $maxMem
* CreateTime: 2019/10/24 上午1:10
* @return KeywordServer
*/
public function setMaxMem(string $maxMem='512M'): KeywordServer
{
$this->maxMem = $maxMem;
return $this;
}

/**
* 设置临时目录
*
Expand Down Expand Up @@ -169,6 +183,7 @@ private function initProcess(): array
$config->setAsyncCallback(false);
$config->setWorkerIndex($i);
$config->setKeywordPath($this->keywordPath);
$config->setMaxMem($this->maxMem);
$array[$i] = new KeywordProcess($config);
}
return $array;
Expand Down

0 comments on commit 44fdbdb

Please sign in to comment.