如何防止为LANGUAGE SWITCH路由请求缓存请求Laravel Spatie responsecache [英] How to Prevent a request from being cached for LANGUAGE SWITCH routing Laravel Spatie responsecache
问题描述
我正在使用Laravel Spatie响应缓存: https://github.com/spatie/laravel-responsecache 和多种语言(语言切换..).
I am using Laravel Spatie response cache: https://github.com/spatie/laravel-responsecache and Multiple language (language switch..).
如何解决:防止在Laravel中为语言切换器路由缓存请求.
How to fix: Preventing a request from being cached for language switcher route in Laravel.
我试图添加:-> middleware('doNotCacheResponse');在lang.switch路由中,但没有成功...
I tried to add: ->middleware('doNotCacheResponse'); in lang.switch route but without success...
Route::get('lang/{language}', ['as' => 'lang.switch', 'uses' => 'LanguageController@switchLang'])->middleware('doNotCacheResponse');
当在要缓存的语言之间切换页面(链接)时,预期结果将阻止请求.
Expected results to prevent request when switching pages (links) between language to be cached....
推荐答案
不确定我是否正确理解您.如果问题是您选择其他语言后获得了缓存结果,那么我认为您应该清除整个缓存:
Not sure if I understand you correctly. If the problem is that you get cached results after you select a different language, I think you should clear the entire cache:
LanguageController {
public function switchLang() {
// Switch language
ResponseCache::clear();
}
}
此外,似乎spatie/laravel-responsecache允许您使用ResponseCache::forget('/some-uri');
Also, seems like spatie/laravel-responsecache allows you to delete specific URIs with ResponseCache::forget('/some-uri');
要在更改数据后删除缓存,请执行以下操作
To delete cache after altering your data, do something like this
PostController {
public function index()
{
// Display list of posts
}
public function edit()
{
// Display form to edit post
}
public function update()
{
// Delete related cache here and update post
ResponseCache::forget('/posts');
}
}
这篇关于如何防止为LANGUAGE SWITCH路由请求缓存请求Laravel Spatie responsecache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!