Performance: Actually make use of the translator cache

We had added a `storage/locale` directory to our skeleton, but we had
forgotten to hook it up with the translator. Enabling caching saves
parsing that locale YAML files on every pageload which should be good
for performance.

The locale cache will be cleared whenever an extension that uses the
`Locales` or `LanguagePack` extenders is enabled/disabled. If debug
mode is ON, then the caching mechanism will automatically check if any
of the loaded YAML files are dirty and update accordingly.
This commit is contained in:
Toby Zerner 2018-12-07 09:38:08 +10:30
parent 6b6fce878a
commit 13ce2d1e3d
4 changed files with 60 additions and 6 deletions

View File

@ -18,7 +18,7 @@ use Illuminate\Contracts\Container\Container;
use InvalidArgumentException;
use RuntimeException;
class LanguagePack implements ExtenderInterface
class LanguagePack implements ExtenderInterface, LifecycleInterface
{
public function extend(Container $container, Extension $extension = null)
{
@ -63,4 +63,14 @@ class LanguagePack implements ExtenderInterface
}
}
}
public function onEnable(Container $container, Extension $extension)
{
$container->make('flarum.locales')->clearCache();
}
public function onDisable(Container $container, Extension $extension)
{
$container->make('flarum.locales')->clearCache();
}
}

View File

@ -16,7 +16,7 @@ use Flarum\Extension\Extension;
use Flarum\Locale\LocaleManager;
use Illuminate\Contracts\Container\Container;
class Locales implements ExtenderInterface
class Locales implements ExtenderInterface, LifecycleInterface
{
protected $directory;
@ -46,4 +46,14 @@ class Locales implements ExtenderInterface
);
}
}
public function onEnable(Container $container, Extension $extension)
{
$container->make('flarum.locales')->clearCache();
}
public function onDisable(Container $container, Extension $extension)
{
$container->make('flarum.locales')->clearCache();
}
}

View File

@ -24,9 +24,15 @@ class LocaleManager
protected $css = [];
public function __construct(Translator $translator)
/**
* @var string
*/
protected $cacheDir;
public function __construct(Translator $translator, string $cacheDir = null)
{
$this->translator = $translator;
$this->cacheDir = $cacheDir;
}
public function getLocale(): string
@ -106,4 +112,11 @@ class LocaleManager
{
$this->translator = $translator;
}
public function clearCache()
{
if ($this->cacheDir) {
array_map('unlink', glob($this->cacheDir.'/*'));
}
}
}

View File

@ -13,10 +13,10 @@ namespace Flarum\Locale;
use Flarum\Event\ConfigureLocales;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Event\ClearingCache;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
use Symfony\Component\Translation\MessageSelector;
use Symfony\Component\Translation\TranslatorInterface;
class LocaleServiceProvider extends AbstractServiceProvider
@ -31,6 +31,10 @@ class LocaleServiceProvider extends AbstractServiceProvider
$locales->addLocale($this->getDefaultLocale(), 'Default');
$events->dispatch(new ConfigureLocales($locales));
$events->listen(ClearingCache::class, function () use ($locales) {
$locales->clearCache();
});
}
/**
@ -38,11 +42,23 @@ class LocaleServiceProvider extends AbstractServiceProvider
*/
public function register()
{
$this->app->singleton(LocaleManager::class);
$this->app->singleton(LocaleManager::class, function () {
return new LocaleManager(
$this->app->make('translator'),
$this->getCacheDir()
);
});
$this->app->alias(LocaleManager::class, 'flarum.locales');
$this->app->singleton('translator', function () {
$translator = new Translator($this->getDefaultLocale(), new MessageSelector());
$translator = new Translator(
$this->getDefaultLocale(),
null,
$this->getCacheDir(),
$this->app->inDebugMode()
);
$translator->setFallbackLocales(['en']);
$translator->addLoader('prefixed_yaml', new PrefixedYamlFileLoader());
@ -59,4 +75,9 @@ class LocaleServiceProvider extends AbstractServiceProvider
return $repo->get('default_locale', 'en');
}
private function getCacheDir(): string
{
return $this->app->storagePath().'/locale';
}
}