
本文环境 CentOS8.0,PHP8.1,MongoDB5.0,Workerman 4.0\ 不懂的可以评论联系我。 著作权归OwenZhang所有。商业转载请联系OwenZhang获得授权,非商业转载请注明出处。

Workerman是一款纯PHP开发的开源高性能的PHP 应用容器。
Workerman不是重复造轮子,它不是一个MVC框架,而是一个更底层更通用的服务框架,你可以用它开发tcp代理、梯子代理、做游戏服务器、邮件服务器、ftp服务器、甚至开发一个php版本的redis、php版本的数据库、php版本的nginx、php版本的php-fpm等等。Workerman可以说是PHP领域的一次创新,让开发者彻底摆脱了PHP只能做WEB的束缚。
实际上Workerman类似一个PHP版本的nginx,核心也是多进程+Epoll+非阻塞IO。Workerman每个进程能维持上万并发连接。由于本身常驻内存,不依赖Apache、nginx、php-fpm这些容器,拥有超高的性能。同时支持TCP、UDP、UNIXSOCKET,支持长连接,支持Websocket、HTTP、WSS、HTTPS等通讯协议以及各种自定义协议。拥有定时器、异步socket客户端、异步Redis、异步Http、异步消息队列等众多高性能组件。
MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。
<?php
/**
* This file is part of webman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
return [
'default' => 'mysql',
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'chongdian'),
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'root'),
'unix_socket' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
'trigger_sql' => true,
],
'mongodb' => [
'driver' => 'mongodb',
'host' => '127.0.0.1',
'port' => 27017,
'database' => 'chongdiang_converter',
'username' => 'chongdiang_converter_2022',
'password' => 'Owen123Zhang',
'options' => [
'database' => 'chongdiang_converter', // required with Mongo 3+
],
],
],
];<?php
namespace app\service;
use MongoDB\Driver\Exception\BulkWriteException;
use support\Db;
class MongodbService
{
public static ?MongodbService $_instance = null;
protected $content;
/**
* @return MongodbService|null
*/
public static function instance(): ?MongodbService
{
if (!static::$_instance) static::$_instance = new self();
return static::$_instance;
}
public function getConnect($dbName = '')
{
if (empty($dbName)) $dbName = 'dian_cartoon';
return Db::connection('mongodb')->collection($dbName);
}
public function initDb($dbName = ''): MongodbService
{
$this->content = $this->getConnect($dbName);
return $this;
}
public function _find(array $where)
{
return $this->content->where($where)->first();
}
public function _insert(array $data = [])
{
return $this->content->insert($data);
}
public function _update(array $where = [], array $data = [])
{
return $this->content->where($where)->update($data);
}
public function _updateIn(array $where = [], array $data = [])
{
return $this->content->whereIn($where[0], $where[1])->update($data);
}
public function _select($where = [], $order = [], $page = 1, $limit = 10)
{
$page = $limit * (max(($page - 1), 0));
return $this->content->where($where)->orderBy($order[0] ?? 'id', $order[1] ?? 'asc')->skip($page)->take($limit)->get()->toArray();
}
public function _count($where = [])
{
return $this->content->where($where)->count();
}
public function _selectIn(array $where = [], $order = [], $page = 1, $limit = 10)
{
$page = $limit * (max(($page - 1), 0));
return $this->content->whereIn($where[0], $where[1])->orderBy($order[0] ?? 'id', $order[1] ?? 'asc')->skip($page)->take($limit)->get()->toArray();
}
public function _delete(array $where): bool
{
if (empty($where)) return false;
return $this->content->where($where)->delete();
}
public function _converter_times(int $uid, int $vip): bool
{
try {
$date = date('Y-m-d');
$where['uid'] = $uid;
$where['date_v'] = $date;
$where['vip'] = $vip;
$has = $this->initDb('converter_times')->_find($where);
if ($has) {
$this->content->where($where)->increment('times', 1);
} else {
$data = ['uid' => $uid, 'date_v' => $date, 'vip' => $vip, 'times' => 1];
$this->_insert($data);
}
return true;
} catch (BulkWriteException $exception) {
return false;
}
}
public function _converter_day_count(int $uid, string $date = '', bool $is_vip = false): int
{
if (empty($date)) $date = date('Y-m-d');
try {
$where['uid'] = $uid;
$where['date_v'] = $date;
$where['vip'] = !$is_vip ? 0 : 1;
$has = $this->initDb('converter_times')->_find($where);
return $has['times'] ?? 0;
} catch (BulkWriteException $exception) {
return 0;
}
}
}$mongodb_data = [
'status' => TaskStatus::TASK_CREATED->value,
'c_time' => date('Y-m-d H:i:s'),
'date_v' => date('Y-m-d'),
'uid' => $request->uid,
'source_name' => 'owen333',
'file_name' => 'file_name333',
'vip' => 'tcp_vip33',
];
$a = MongodbService::instance()->initDb()->_insert($mongodb_data);效果如下

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。