您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-redis組件-setBit
发布时间:2021-12-26 00:12:04编辑:雪饮阅读()
setBit
对 key 所储存的字符串值,设置或清除指定偏移量上的位(bit)。
返回值为0或1,是修改前的值。
實例:
<?php
use Workerman\Worker;
use Workerman\Redis\Client;
use Workerman\Connection\TcpConnection;
use Workerman\Timer;
require_once __DIR__ . '/vendor/autoload.php';
$worker = new Worker('http://0.0.0.0:6161');
global $offset;
$worker->onWorkerStart = function() {
global $offset;
$offset=0;
};
$worker->onMessage = function(TcpConnection $connection, $data) {
global $offset;
$redis = new Client('redis://127.0.0.1:6379');
$redis->auth("xy220807",function($result){
var_dump("auth:");
var_dump($result);
});
$redis->set('key', "*"); // ord("*") = 42 = 0x2f = "0010 1010"
$redis->setBit('key', $offset, 1, function ($result) {
global $offset;
var_dump("setBit ".$offset.":");
var_dump($result); // 0
$offset++;
}) ;
};
Worker::runAll();
實例運行並有http客戶端的7次訪問后如:
[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.
string(5) "auth:"
bool(true)
string(9) "setBit 0:"
int(0)
string(5) "auth:"
bool(true)
string(9) "setBit 1:"
int(0)
string(5) "auth:"
bool(true)
string(9) "setBit 2:"
int(1)
string(5) "auth:"
bool(true)
string(9) "setBit 3:"
int(0)
string(5) "auth:"
bool(true)
string(9) "setBit 4:"
int(1)
string(5) "auth:"
bool(true)
string(9) "setBit 5:"
int(0)
string(5) "auth:"
bool(true)
string(9) "setBit 6:"
int(1)
关键字词:workerman,redis,setBit