您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-redis組件
发布时间:2021-12-23 22:34:08编辑:雪饮阅读()
workerman-redis
介绍
workeman/redis是基于workerman的异步redis组件。
注意:此项目主要目的是实现redis异步订阅(subscribe、pSubscribe),除此之外请使用redis扩展。
從介紹來看貌似很鷄肋,畢竟像是thinkphp從第五版好像開始就redis比較友好了,不過這裏談談哥的看法,不一定對。
爲什麽要異步呢?
就拿redis的取消訂閲來介紹吧,其語法大致如下:
127.0.0.1:6379> subscribe channel1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel1"
3) (integer) 1
unsubscribe channel1
這裏你實際去操作時候會發現unsubscribe命令沒有響應,哥實在是不懂,訂閲后就一直阻塞狀態,那麽網上說是要按一些什麽鍵之類再取消訂閲就行了,哥試過了,沒有作用。或者是哥試的不正確吧。
那麽從這件事情上來說,哥的理解就是讓redis異步,異步后就不阻塞了,就能解決類似取消訂閲的場景了吧。當然異步也可以有其它實現方式吧。這裏僅僅是哥自己意淫的想法。
不多說了,具體使用如:
先安裝依賴:
[root@localhost www.fpm.com]# /usr/local/php734/bin/php /usr/bin/composer require workerman/redis
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Continue as root/super user [yes]? yes
Using version ^1.0 for workerman/redis
./composer.json has been updated
Running composer update workerman/redis
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
- Locking workerman/redis (v1.0.7)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Downloading workerman/redis (v1.0.7)
- Installing workerman/redis (v1.0.7): Extracting archive
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
然後是使用實例:
<?php
use Workerman\Worker;
use Workerman\Redis\Client;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:6161');
$worker->onWorkerStart = function() {
global $redis;
$redis = new Client('redis://127.0.0.1:6379');
};
$worker->onMessage = function(TcpConnection $connection, $data) {
global $redis;
$redis->set('key', 'hello world');
$redis->get('key', function ($result) use ($connection) {
$connection->send($result);
});
};
Worker::runAll();
然後是實例部署:
[root@localhost www.fpm.com]# /usr/local/php734/bin/php channelServer.php start
Workerman[channelServer.php] start in DEBUG mode
----------------------------------------- WORKERMAN -----------------------------------------
Workerman version:4.0.22 PHP version:7.3.4
------------------------------------------ WORKERS ------------------------------------------
proto user worker listen processes status
tcp root none http://0.0.0.0:6161 1 [OK]
---------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.
然後是實例實測了:
[root@localhost ~]# elinks http://127.0.0.1:6161 --dump
hello world
关键字词:workerman,redis,組件
相关文章
- workerman-mysql組件-其它數據庫類=medoo的數據庫類的
- workerman-mysql组件-其它数据库类使用-thinkphp(thin
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm
- workerman常用组件-MySQL组件-workerman之使用Workerm