Build "redis" plugin into core

This commit is contained in:
Pig Fang 2019-03-20 11:00:14 +08:00
parent 3366237828
commit d27dc54377
7 changed files with 81 additions and 22 deletions

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use Redis;
use Option;
use Carbon\Carbon;
use App\Models\User;
@ -249,8 +250,26 @@ class AdminController extends Controller
})
->handle();
$redis = Option::form('redis', 'Redis', function ($form) {
$form->checkbox('enable_redis')->label();
});
if (option('enable_redis')) {
try {
Redis::ping();
$redis->addMessage(trans('options.redis.connect.success'), 'success');
} catch (\Exception $e) {
$redis->addMessage(
trans('options.redis.connect.failed', ['msg' => $e->getMessage()]),
'danger'
);
}
}
$redis->handle();
return view('admin.resource')
->with('forms', compact('resources'));
->with('forms', compact('resources', 'redis'));
}
public function getUserData(Request $request)

View File

@ -4,6 +4,7 @@ namespace App\Providers;
use Blade;
use Event;
use Redis;
use App\Events;
use App\Models\User;
use ReflectionException;
@ -44,6 +45,15 @@ class AppServiceProvider extends ServiceProvider
} catch (ReflectionException $e) {
throw new PrettyPageException(trans('errors.cipher.unsupported', ['cipher' => config('secure.cipher')]));
}
try {
if (Redis::ping()) {
config(['cache.default' => 'redis']);
config(['session.driver' => 'redis']);
}
} catch (\Exception $e) {
//
}
}
/**

View File

@ -220,7 +220,7 @@ return [
'Password' => Illuminate\Support\Facades\Password::class,
'Queue' => Illuminate\Support\Facades\Queue::class,
'Redirect' => Illuminate\Support\Facades\Redirect::class,
'Predis' => Illuminate\Support\Facades\Redis::class,
'Redis' => Illuminate\Support\Facades\Redis::class,
'Request' => Illuminate\Support\Facades\Request::class,
'Response' => Illuminate\Support\Facades\Response::class,
'Route' => Illuminate\Support\Facades\Route::class,

View File

@ -124,6 +124,17 @@ announ:
announcement:
description: Styling with Markdown is supported. You can also specify a different announcement for each language. To edit a specific language's corresponding announcement, please switch to that language and submit your edit.
meta:
title: SEO <meta> tags
meta_keywords:
title: Keywords
hint: Split with commas.
meta_description:
title: Description
hint: Description defined in "general options" will be used if you left it empty.
meta_extras:
title: Other Custom <meta> Tags
resources:
title: Resource Files
hint: Please check these options if you enabled CDN for your site.
@ -151,13 +162,10 @@ resources:
all the files of that directory will be loaded as CDN.<br>
<b>How to verify?</b> Verify if <code>{Your CDN URL}/app/index.js</code> can be accessed.
meta:
title: SEO <meta> tags
meta_keywords:
title: Keywords
hint: Split with commas.
meta_description:
title: Description
hint: Description defined in "general options" will be used if you left it empty.
meta_extras:
title: Other Custom <meta> Tags
redis:
enable_redis:
title: Enable
label: Enable Redis
connect:
success: Connected to Redis server successfully.
failed: 'Failed to connect Redis server. Error: :msg'

View File

@ -124,6 +124,17 @@ announ:
announcement:
description: 可使用 Markdown 进行排版。每种支持的语言都可以对应不同的站点公告,如果想要编辑某种特定语言下的公告,请在右上角切换至该语言后再提交修改。
meta:
title: SEO <meta> 标签
meta_keywords:
title: 关键词
hint: 使用半角逗号分隔
meta_description:
title: 描述
hint: 留空以使用 站点配置 中的站点描述
meta_extras:
title: 其它自定义 <meta> 标签
resources:
title: 资源文件配置
hint: 如果启用了 CDN 缓存请适当修改这些配置
@ -150,13 +161,10 @@ resources:
填写的 CDN 地址必须是 <code>/public</code> 目录的镜像,此目录下的所有文件都将会从 CDN 加载。<br>
<b>测试方法</b>:检查 <code>{填写的地址}/app/index.js</code> 是否能够访问。
meta:
title: SEO <meta> 标签
meta_keywords:
title: 关键词
hint: 使用半角逗号分隔
meta_description:
title: 描述
hint: 留空以使用 站点配置 中的站点描述
meta_extras:
title: 其它自定义 <meta> 标签
redis:
enable_redis:
title: 启用
label: 使用 Redis
connect:
success: 成功连接 Redis 服务器。
failed: '连接 Redis 服务器失败。错误消息: :msg'

View File

@ -22,6 +22,7 @@
</div>
<div class="col-md-6">
{!! $forms['redis']->render() !!}
</div>
</div>

View File

@ -2,6 +2,7 @@
namespace Tests;
use Redis;
use App\Models\User;
use App\Models\Player;
use App\Models\Texture;
@ -167,6 +168,18 @@ class AdminControllerTest extends BrowserKitTestCase
->type('', 'cdn_address')
->press('submit_resources');
$this->visit('/')->dontSee('url/app/index.js');
$this->visit('/admin/resource')
->check('enable_redis')
->press('submit_redis');
$this->assertTrue(option('enable_redis'));
Redis::shouldReceive('ping')->once()->andReturn(true);
$this->visit('/admin/resource')->see(trans('options.redis.connect.success'));
Redis::shouldReceive('ping')->once()->andThrow(new \Exception('fake'));
$this->visit('/admin/resource')
->see(trans('options.redis.connect.failed', ['msg' => 'fake']));
}
public function testUsers()