您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-redis組件-exists
发布时间:2021-12-25 22:15:27编辑:雪饮阅读()
exists
命令用于检查给定 key 是否存在。返回结果为数字,代表存在的key的个数。
實例:
<?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');
$worker->onWorkerStart = function() {
};
$worker->onMessage = function(TcpConnection $connection, $data) {
$redis = new Client('redis://127.0.0.1:6379');
$redis->auth("xy220807",function($result){
var_dump("auth:");
var_dump($result);
});
$redis->set('key', 'value');
$redis->exists('key', function ($result) {
var_dump("key:");
var_dump($result); // 1
});
$redis->exists('NonExistingKey', function ($result) {
var_dump("NonExistingKey:");
var_dump($result); // 0
});
$redis->mset(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz']);
$redis->exists(['foo', 'bar', 'baz'], function ($result) {
var_dump("foo,bar,baz:");
var_dump($result); // 3
});
};
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.
string(5) "auth:"
bool(true)
string(4) "key:"
int(1)
string(15) "NonExistingKey:"
int(0)
string(12) "foo,bar,baz:"
int(3)
上面mset是批量設置多個key時所用的方法。
关键字词:workerman,redis,exists