Skip to content

Commit e39a3cd

Browse files
Merge pull request #1823 from CachetHQ/settings-cache
Refactored settings caching
2 parents d0afcbf + 13d0ff3 commit e39a3cd

File tree

7 files changed

+154
-27
lines changed

7 files changed

+154
-27
lines changed

app/Foundation/Providers/ConfigServiceProvider.php

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CachetHQ\Cachet\Foundation\Providers;
1313

1414
use CachetHQ\Cachet\Models\Setting as SettingModel;
15+
use CachetHQ\Cachet\Settings\Cache;
1516
use CachetHQ\Cachet\Settings\Repository;
1617
use Exception;
1718
use Illuminate\Support\ServiceProvider;
@@ -32,29 +33,24 @@ class ConfigServiceProvider extends ServiceProvider
3233
*/
3334
public function boot()
3435
{
35-
$path = $this->app->bootstrapPath().'/cache/cachet.'.$this->app->environment().'.php';
36-
37-
try {
38-
$cache = $this->app->files->getRequire($path);
39-
} catch (Exception $e) {
40-
$cache = false;
41-
}
42-
43-
$this->app->terminating(function () use ($cache, $path) {
44-
if ($this->app->setting->stale() || $cache === false) {
45-
$this->app->files->put($path, '<?php return '.var_export($this->app->setting->all(), true).';'.PHP_EOL);
36+
$env = $this->app->environment();
37+
$repo = $this->app->make(Repository::class);
38+
$cache = $this->app->make(Cache::class);
39+
$loaded = $cache->load($env);
40+
41+
$this->app->terminating(function () use ($repo, $cache) {
42+
if ($repo->stale()) {
43+
$cache->clear();
4644
}
4745
});
4846

4947
try {
50-
// Get the default settings.
51-
$defaultSettings = $this->app->config->get('setting');
52-
53-
// Get the configured settings.
54-
$appSettings = $cache === false ? $this->app->setting->all() : $cache;
48+
if ($loaded === false) {
49+
$loaded = $repo->all();
50+
$cache->store($env, $loaded);
51+
}
5552

56-
// Merge the settings
57-
$settings = array_merge($defaultSettings, $appSettings);
53+
$settings = array_merge($this->app->config->get('setting'), $loaded);
5854

5955
$this->app->config->set('setting', $settings);
6056
} catch (Exception $e) {
@@ -95,10 +91,12 @@ public function boot()
9591
*/
9692
public function register()
9793
{
98-
$this->app->singleton('setting', function () {
99-
return new Repository(new SettingModel());
94+
$this->app->singleton(Cache::class, function ($app) {
95+
return new Cache($app->files, $app->bootstrapPath().'/cachet');
10096
});
10197

102-
$this->app->alias('setting', Repository::class);
98+
$this->app->singleton(Repository::class, function () {
99+
return new Repository(new SettingModel());
100+
});
103101
}
104102
}

app/Http/Controllers/Dashboard/SettingsController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
1313

1414
use CachetHQ\Cachet\Models\User;
15+
use CachetHQ\Cachet\Settings\Repository;
1516
use Exception;
1617
use GrahamCampbell\Binput\Facades\Binput;
1718
use Illuminate\Routing\Controller;
@@ -220,7 +221,7 @@ public function postSettings()
220221
{
221222
$redirectUrl = Session::get('redirect_to', route('dashboard.settings.setup'));
222223

223-
$setting = app('setting');
224+
$setting = app(Repository::class);
224225

225226
if (Binput::get('remove_banner') === '1') {
226227
$setting->set('app_banner', null);

app/Http/Controllers/SetupController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CachetHQ\Cachet\Http\Controllers;
1313

1414
use CachetHQ\Cachet\Models\User;
15+
use CachetHQ\Cachet\Settings\Repository;
1516
use Dotenv\Dotenv;
1617
use Dotenv\Exception\InvalidPathException;
1718
use GrahamCampbell\Binput\Facades\Binput;
@@ -175,7 +176,7 @@ public function postStep3()
175176

176177
Auth::login($user);
177178

178-
$setting = app('setting');
179+
$setting = app(Repository::class);
179180

180181
$settings = array_pull($postData, 'settings');
181182

app/Settings/Cache.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Cachet.
5+
*
6+
* (c) Alt Three Services Limited
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace CachetHQ\Cachet\Settings;
13+
14+
use Exception;
15+
use Illuminate\Filesystem\Filesystem;
16+
17+
/**
18+
* This is the settings cache class.
19+
*
20+
* @author Graham Campbell <[email protected]>
21+
* @author James Brooks <[email protected]>
22+
*/
23+
class Cache
24+
{
25+
/**
26+
* The filesystem instance.
27+
*
28+
* @var \Illuminate\Filesystem\Filesystem
29+
*/
30+
protected $files;
31+
32+
/**
33+
* Is path to the setting cache.
34+
*
35+
* @var string
36+
*/
37+
protected $path;
38+
39+
/**
40+
* Create a new settings cache instance.
41+
*
42+
* @param \Illuminate\Filesystem\Filesystem $files
43+
* @param string $path
44+
*
45+
* @return void
46+
*/
47+
public function __construct(Filesystem $files, $path)
48+
{
49+
$this->files = $files;
50+
$this->path = $path;
51+
}
52+
53+
/**
54+
* Store the settings in the cache.
55+
*
56+
* @param string $env
57+
* @param array $data
58+
*
59+
* @return void
60+
*/
61+
public function store($env, array $data)
62+
{
63+
$this->files->put($this->path($env), '<?php return '.var_export($data, true).';'.PHP_EOL);
64+
}
65+
66+
/**
67+
* Load the settings from the cache.
68+
*
69+
* @param string $env
70+
*
71+
* @return array|false
72+
*/
73+
public function load($env)
74+
{
75+
try {
76+
return $this->files->getRequire($this->path($env));
77+
} catch (Exception $e) {
78+
return false;
79+
}
80+
}
81+
82+
/**
83+
* Clear the settings cache.
84+
*
85+
* Note that we're careful not to remove the .gitignore file.
86+
*
87+
* @return void
88+
*/
89+
public function clear()
90+
{
91+
$this->files->delete($this->files->allFiles($this->path));
92+
}
93+
94+
/**
95+
* Returns the settings cache path.
96+
*
97+
* @return string
98+
*/
99+
protected function path($env)
100+
{
101+
return "{$this->path}/{$env}.php";
102+
}
103+
}

app/Settings/Repository.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
use CachetHQ\Cachet\Models\Setting;
1515

16+
/**
17+
* This is the settings repository class.
18+
*
19+
* @author Graham Campbell <[email protected]>
20+
* @author James Brooks <[email protected]>
21+
*/
1622
class Repository
1723
{
1824
/**
@@ -30,7 +36,7 @@ class Repository
3036
protected $stale = false;
3137

3238
/**
33-
* Create a new settings service instance.
39+
* Create a new settings repository instance.
3440
*
3541
* @param \CachetHQ\Cachet\Models\Setting $model
3642
*

app/Subscribers/CommandSubscriber.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CachetHQ\Cachet\Subscribers;
1313

14+
use CachetHQ\Cachet\Settings\Cache;
1415
use Carbon\Carbon;
1516
use Exception;
1617
use Illuminate\Console\Command;
@@ -26,7 +27,14 @@
2627
class CommandSubscriber
2728
{
2829
/**
29-
* The config repository.
30+
* The settings cache instance.
31+
*
32+
* @var \CachetHQ\Cachet\Settings\Cache
33+
*/
34+
protected $cache;
35+
36+
/**
37+
* The config repository instance.
3038
*
3139
* @var \Illuminate\Contracts\Config\Repository
3240
*/
@@ -35,12 +43,14 @@ class CommandSubscriber
3543
/**
3644
* Create a new command subscriber instance.
3745
*
46+
* @param \CachetHQ\Cachet\Settings\Cache $cache
3847
* @param \Illuminate\Contracts\Config\Repository $config
3948
*
4049
* @return void
4150
*/
42-
public function __construct(Repository $config)
51+
public function __construct(Cache $cache, Repository $config)
4352
{
53+
$this->cache = $cache;
4454
$this->config = $config;
4555
}
4656

@@ -59,14 +69,20 @@ public function subscribe(Dispatcher $events)
5969
}
6070

6171
/**
62-
* Backup the databases.
72+
* Clear the settings cache, and backup the databases.
6373
*
6474
* @param \Illuminate\Console\Command $command
6575
*
6676
* @return void
6777
*/
6878
public function fire(Command $command)
6979
{
80+
$command->line('Clearing settings cache...');
81+
82+
$this->cache->clear();
83+
84+
$command->line('Settings cache cleared!');
85+
7086
$command->line('Backing up database...');
7187

7288
try {

bootstrap/cachet/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

0 commit comments

Comments
 (0)