在我的应用程序中,我希望允许某些用户退出除他/她之外的所有其他用户.我已经完成了这个功能,当会话驱动程序设置为文件时,但现在我使用 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?
以下是我在 session driver 为 file 时使用的代码:
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:
<代码>数组: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
分开.
在文件configdatabase.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'
现在你需要利用它
转到文件'configsession.php'
go to file 'configsession.php'
并将其设置为 'connection' =>'会话连接',
然后转到文件 configcache.php
并将其设置为
'redis' => [
'driver' => 'redis',
'connection' => 'cache-connection',
],
现在你可以得到你的 redis 会话记录.
and now you can get your redis session records.
use IlluminateSupportFacadesRedis;
Log::debug( Redis::connection('session-connection')->keys('*') );
这篇关于Laravel 使用 Redis 驱动程序的所有会话 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!