remove cache for profile json

This commit is contained in:
Pig Fang 2020-01-12 10:28:43 +08:00
parent d85c6d840b
commit 97ec85b289
10 changed files with 8 additions and 84 deletions

View File

@ -365,7 +365,6 @@ class AdminController extends Controller
$cache = Option::form('cache', OptionForm::AUTO_DETECT, function ($form) {
$form->checkbox('enable_avatar_cache')->label();
$form->checkbox('enable_preview_cache')->label();
$form->checkbox('enable_json_cache', 'JSON Profile')->label();
$form->checkbox('enable_notfound_cache', '404')->label();
})
->type('warning')

View File

@ -16,6 +16,9 @@ class TextureController extends Controller
{
public function __construct()
{
$this->middleware('cache.headers:public;max_age='.option('cache_expire_time'))
->only(['json']);
$this->middleware('cache.headers:etag;public;max_age='.option('cache_expire_time'))
->only([
'preview',
@ -29,16 +32,9 @@ class TextureController extends Controller
public function json($player)
{
$player = $this->getPlayerInstance($player);
if (option('enable_json_cache')) {
$json = Cache::rememberForever('json-'.$player->pid, function () use ($player) {
return $player->toJson();
});
return response($json)
->header('Content-Type', 'application/json')
->setLastModified($player->last_modified);
}
$player = Player::where('name', $player)->firstOrFail();
$isBanned = $player->user->permission === User::BANNED;
abort_if($isBanned, 403, trans('general.player-banned'));
return response()->json($player)->setLastModified($player->last_modified);
}
@ -159,12 +155,4 @@ class TextureController extends Controller
return $response;
}
protected function getPlayerInstance($player_name)
{
$player = Player::where('name', $player_name)->first();
abort_if($player->isBanned(), 403, trans('general.player-banned'));
return $player;
}
}

View File

@ -1,13 +0,0 @@
<?php
namespace App\Listeners;
use Cache;
class CleanPlayerJson
{
public function handle($event)
{
Cache::forget('json-'.$event->player->pid);
}
}

View File

@ -49,8 +49,5 @@ class EventServiceProvider extends ServiceProvider
if (option('enable_notfound_cache')) {
Event::subscribe(Listeners\CachePlayerExists::class);
}
if (option('enable_json_cache')) {
Event::listen(Events\PlayerProfileUpdated::class, Listeners\CleanPlayerJson::class);
}
}
}

View File

@ -206,7 +206,5 @@ cache:
enable_preview_cache:
title: Texture Preivew
label: Enable caching texture preivew
enable_json_cache:
label: Enable caching Json Profile
enable_notfound_cache:
label: Enable caching whether player is existed or not

View File

@ -66,6 +66,7 @@
- Removed Legacy API from core. (Install plugin if you need it.)
- Removed Universal Skin API from core. (Install plugin if you need it.)
- Removed auto update check.
- Removed cache for Profile JSON.
## Internal Changes

View File

@ -66,6 +66,7 @@
- 移除「传统皮肤加载方式」(如有需要,请安装插件)
- 移除 Universal Skin API如有需要请安装插件
- 移除自动更新检查
- 移除对 Profile JSON 的缓存
## 内部更改

View File

@ -190,12 +190,10 @@ class AdminFormsTest extends BrowserKitTestCase
->see(trans('options.cache.driver', ['driver' => config('cache.default')]))
->check('enable_avatar_cache')
->check('enable_preview_cache')
->check('enable_json_cache')
->check('enable_notfound_cache')
->press('submit_cache');
$this->assertTrue(option('enable_avatar_cache'));
$this->assertTrue(option('enable_preview_cache'));
$this->assertTrue(option('enable_json_cache'));
$this->assertTrue(option('enable_notfound_cache'));
Cache::shouldReceive('flush');

View File

@ -40,26 +40,6 @@ class TextureControllerTest extends TestCase
$player->user->permission = User::NORMAL;
$player->user->save();
$this->getJson("/{$player->name}.json")
->assertJson([
'username' => $player->name,
'skins' => [
'default' => $steve->hash,
],
'cape' => null,
])->assertHeader('Last-Modified');
option(['enable_json_cache' => true]);
Cache::shouldReceive('rememberForever')
->withArgs(function ($key, $closure) use ($player) {
$this->assertEquals('json-'.$player->pid, $key);
$this->assertEquals($player->toJson(), $closure());
return true;
})
->once()
->andReturn($player->toJson());
$this->getJson("/{$player->name}.json")
->assertJson([
'username' => $player->name,

View File

@ -1,25 +0,0 @@
<?php
namespace Tests;
use App\Events\PlayerProfileUpdated;
use App\Models\Player;
use Cache;
use Event;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class CleanPlayerJsonTest extends TestCase
{
use DatabaseTransactions;
public function testHandle()
{
option(['enable_json_cache' => true]);
$provider = new \App\Providers\EventServiceProvider(app());
$provider->boot();
$player = factory(Player::class)->create();
event(new PlayerProfileUpdated($player));
Cache::shouldReceive('forget')->with('json-'.$player->pid);
}
}