diff --git a/README.md b/README.md index 2723272..971accc 100644 --- a/README.md +++ b/README.md @@ -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 +## 代码示例 ``` 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()); } @@ -74,10 +75,9 @@ class EasySwooleEvent implements Event // TODO: Implement afterAction() method. } } - ``` -#### 4. 输出结果 +## 命中结果 ``` array(3) { @@ -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) @@ -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) @@ -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) +``` diff --git a/src/KeywordServer.php b/src/KeywordServer.php index 628c78c..399dc38 100644 --- a/src/KeywordServer.php +++ b/src/KeywordServer.php @@ -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; + } + /** * 设置临时目录 * @@ -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;