問題描述
在我的應用程序中,我希望允許某些用戶退出除他/她之外的所有其他用戶.我已經完成了這個功能,當會話驅動程序設置為文件時,但現在我使用 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!