您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
workerman-redis組件 -setEx, pSetEx
发布时间:2021-12-25 22:06:45编辑:雪饮阅读()
setEx, pSetEx
为指定的 key 设置值及其过期时间。如果 key 已经存在, SETEX 命令将会替换旧的值。
實例:
<?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->setEx('key1', 3600, 'value1');
// pSetEx 单位为毫秒
$redis->pSetEx('key2', 3600000, 'value2');
};
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.
string(5) "auth:"
bool(true)
此時的key:
127.0.0.1:6379> keys *
1) "key2"
2) "key1"
3) "key"
實際上這個説明不了什麽,因爲與時間有關。
关键字词:workerman,redis,setEx,pSetEx