【发布时间】:2021-11-18 03:05:16
【问题描述】:
遇到了我在编码过程中遇到的最奇怪的错误。
我正在使用:laravel ratchet websocket Laravel Cache 和文件驱动
我正在尝试使用来自工匠命令类的 laravel 缓存从其闭包函数中缓存棘轮 websocket 响应消息。
当我在 websocket 响应中使用 var_dump 时,我会在终端上打印出所有消息。但是当我尝试保存在缓存中时,它返回 true 但缓存为空。不被存储,不显示错误信息。
这是我的代码:
use Illuminate\Support\Facades\Cache as Cache;
class Ticker extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'daemon:t';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Listens to websockets t';
/**
* Create a new command instance.
*
* @return void
*/
protected $cah;
public function __construct(Cache $Cache)
{
$this->cah = new $Cache();
parent::__construct();
}
public function mini(callable $callback)
{
// phpunit can't cover async function
\Ratchet\Client\connect('wss://stream......')->then(function ($ws) use ($callback) {
$ws->on('message', function ($data) use ($ws, $callback) {
$json = json_decode($data, true);
return call_user_func($callback, $this->cah, $json);
});
$ws->on('close', function ($code = null, $reason = null) {
// WPCS: XSS OK.
echo "....: WebSocket Connection closed! ({$code} - {$reason})" . PHP_EOL;
});
}, function ($e) {
// WPCS: XSS OK.
echo "....: Could not connect: {$e->getMessage()}" . PHP_EOL;
});
}
// save
public function saveT($t){
//$this->alert($t);
try{
foreach ($t as $obj) {
$this->cah::put($obj['s'], $obj, now()->addSeconds(5));
}
// used in testing
//print_r($t);
} catch (\Exception $exception) {
$this->alert($exception->getMessage());
}
}
/**
* Execute the console command.
*
* @return mixed
* @throws \Exception
*/
public function handle()
{
$this->mini(function ($api,$json){
// saving directly though the $api object
$api::put(['s' => $json], now()->addSeconds(10));
// when savingnthrough saveT function
// $this->saveT($json);
try{
// call_user_func_array(array($this->cah::class,'put'),array('test','tested',now()->addSeconds(5)));
} catch (\Exception $exception) {
$this->alert($exception->getMessage());
}
},);
}
}
【问题讨论】:
-
$cache和$Cache是两个不同的变量(变量区分大小写)。 -
在您的第二个示例中(您使用完整的命名空间),您仍然有
use (&$Cache),它在那里不需要(如果您尚未定义$Cache,则会失败)。在开发过程中,请确保您是displaying all errors and warnings,因为我看不到所有尝试如何在没有错误/警告的情况下失败(特别是当您应该得到“未定义变量”-通知/警告时) -
很抱歉,在格式化我的问题时出现了错误。在代码中正确引用它(第一个字母 Cap )
-
请复制/粘贴您的实际代码,而不是在此处重写。在调试代码时,我们看到您的确切代码至关重要。重写可能会引入其他问题(例如变量大小写),甚至会无意中修复实际问题。
-
我已经添加了我想要在最后工作的最后一段代码。我只想在闭包内执行缓存 focade。
标签: php caching laravel-8 laravel-cache