Laravel使用Redis驱动程序的所有会话ID [英] Laravel all sessions IDs with Redis driver
问题描述
在我的应用程序中,我希望允许某些用户退出他/她以外的所有其他用户.好的,当将会话驱动程序设置为文件时,我已经完成了此功能,但是现在我将redis用作会话驱动程序,我无法找到任何方法来列出所有当前会话,就像我在归档文件时所做的那样驱动程序.
In my application I want to allow for some user, to be able to sign out all other users except him/her. I have done this functionality, well, when the Session driver was set to file, but now I'm using redis as session driver and I could not able to find any way to list up all current sessions like I have done when it was file driver.
问题是:使用redis作为会话驱动程序时,如何列出所有会话ID?
The question is: How to list up all sessions IDs when using redis as a session driver?
以下是会话驱动程序为文件时使用的代码:
The following is the code that I have used when session driver was file:
public function signoutAllUsers(Request $request,$sesId=null){
//dd(session());
if ($sesId == session()->getId()){
$dir = storage_path().'/framework/sessions';
$files = scandir($dir);
foreach ($files as $file){
if ($file == session()->getId() || strpos($file,'.') !== false){
//echo "ggg";
continue;
}
try{
unlink($dir.'/'.$file);
}
catch(\Exception $e){
return $e;
}
}
$request->session()->flash('status','success');
$request->session()->flash('msg',__('All users have been signed out successfully'));
return redirect('/method/create');
}
else{
return redirect('/method/create');
}
}
更新
我发现了一个有限的解决方案,该解决方案取决于Redis
外观方法command
:
Redis::command('keys',['*'])
但是,它返回的输出看起来像:
Redis::command('keys',['*'])
However, it returns output looks like:
array:4 [▼
0 => "laravel:cav17Job1_7l46wAdE2--__"
1 => "laravel:cav17Job1_7l46wAdE2--_"
2 => "laravel:WwerTYmw2VNAfR5nKj3OOGBp2hKytSBK4MWMJ2P9"
3 => "laravel:12tyuwzoFhXPM4f6w4yRPxrYywPon4W41neq6gu"
]
上面的输出包含会话ID和其他缓存条目,在我的应用程序中,我也使用Redis缓存.
array:4 [▼
0 => "laravel:cav17Job1_7l46wAdE2--__"
1 => "laravel:cav17Job1_7l46wAdE2--_"
2 => "laravel:WwerTYmw2VNAfR5nKj3OOGBp2hKytSBK4MWMJ2P9"
3 => "laravel:12tyuwzoFhXPM4f6w4yRPxrYywPon4W41neq6gu"
]
The above output contains both sessions ids and other cache entries, in my application I am using Redis for cache too.
问题变成了,我该如何给存储在redis中的会话提供不同于laravel
的其他密钥(缓存密钥)?
The question becomes, How could I give sessions stored in redis, different key other than laravel
which is the cache key?
推荐答案
将session
和cache
分开保存.
在文件\config\database.php
您可以设置许多 redis
连接,默认情况下有"default"
,但是您可以添加更多
You can set many redis
connections, by default there is a "default"
but you can add more to it
假设您创建了'session-connection'
和'cache-connection'
现在您需要利用它
转到文件"config \ session.php"
go to file 'config\session.php'
并将其设置为'connection' => 'session-connection',
然后转到文件config\cache.php
并将其设置为
'redis' => [
'driver' => 'redis',
'connection' => 'cache-connection',
],
现在您可以获取Redis会话记录.
and now you can get your redis session records.
use Illuminate\Support\Facades\Redis;
\Log::debug( Redis::connection('session-connection')->keys('*') );
这篇关于Laravel使用Redis驱动程序的所有会话ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!