您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-redis組件 -hSet
发布时间:2021-12-27 22:33:17编辑:雪饮阅读()
hSet
为哈希表中的字段赋值 。
如果字段是哈希表中的一个新建字段,并且值设置成功,返回 1 。 如果哈希表中域字段已经存在且旧值已被新值覆盖,返回 0 。
實例:
<?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) {
$redis = new Client('redis://127.0.0.1:6379');
$redis->del('h');
$redis->hSet('h', 'key1', 'hello', function ($r) {
var_dump($r); // 1
});
$redis->hGet('h', 'key1', function ($r) {
var_dump($r); // hello
});
$redis->hSet('h', 'key1', 'plop', function ($r) {
var_dump($r); // 0
});
$redis->hGet('h', 'key1', function ($r) {
var_dump($r); // plop
});
};
Worker::runAll();
實例運行並有http客戶端訪問如:
[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.
int(1)
string(5) "hello"
int(0)
string(4) "plop"
int(1)
string(5) "hello"
int(0)
string(4) "plop"
关键字词:workerman,redis,hSet