mirror of
https://github.com/nextcloud/server.git
synced 2026-02-23 01:40:59 -05:00
Cache available languages locally
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
parent
be4c061b75
commit
306d7829b5
4 changed files with 23 additions and 3 deletions
|
|
@ -40,6 +40,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace OC\L10N;
|
||||
|
||||
use OCP\ICache;
|
||||
use OCP\ICacheFactory;
|
||||
use OCP\IConfig;
|
||||
use OCP\IRequest;
|
||||
use OCP\IUser;
|
||||
|
|
@ -94,7 +96,9 @@ class Factory implements IFactory {
|
|||
protected $request;
|
||||
|
||||
/** @var IUserSession */
|
||||
protected $userSession;
|
||||
protected IUserSession $userSession;
|
||||
|
||||
private ICache $cache;
|
||||
|
||||
/** @var string */
|
||||
protected $serverRoot;
|
||||
|
|
@ -109,11 +113,13 @@ class Factory implements IFactory {
|
|||
IConfig $config,
|
||||
IRequest $request,
|
||||
IUserSession $userSession,
|
||||
ICacheFactory $cacheFactory,
|
||||
$serverRoot
|
||||
) {
|
||||
$this->config = $config;
|
||||
$this->request = $request;
|
||||
$this->userSession = $userSession;
|
||||
$this->cache = $cacheFactory->createLocal('L10NFactory');
|
||||
$this->serverRoot = $serverRoot;
|
||||
}
|
||||
|
||||
|
|
@ -338,6 +344,10 @@ class Factory implements IFactory {
|
|||
$key = 'null';
|
||||
}
|
||||
|
||||
if ($availableLanguages = $this->cache->get($key)) {
|
||||
$this->availableLanguages[$key] = $availableLanguages;
|
||||
}
|
||||
|
||||
// also works with null as key
|
||||
if (!empty($this->availableLanguages[$key])) {
|
||||
return $this->availableLanguages[$key];
|
||||
|
|
@ -374,6 +384,7 @@ class Factory implements IFactory {
|
|||
}
|
||||
|
||||
$this->availableLanguages[$key] = $available;
|
||||
$this->cache->set($key, $available, 60);
|
||||
return $available;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -688,6 +688,7 @@ class Server extends ServerContainer implements IServerContainer {
|
|||
$c->get(\OCP\IConfig::class),
|
||||
$c->getRequest(),
|
||||
$c->get(IUserSession::class),
|
||||
$c->get(ICacheFactory::class),
|
||||
\OC::$SERVERROOT
|
||||
);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ namespace Test\L10N;
|
|||
|
||||
use OC\L10N\Factory;
|
||||
use OC\L10N\LanguageNotFoundException;
|
||||
use OCP\ICacheFactory;
|
||||
use OCP\IConfig;
|
||||
use OCP\IRequest;
|
||||
use OCP\IUser;
|
||||
|
|
@ -32,6 +33,9 @@ class FactoryTest extends TestCase {
|
|||
/** @var IUserSession|MockObject */
|
||||
protected $userSession;
|
||||
|
||||
/** @var ICacheFactory|MockObject */
|
||||
protected $cacheFactory;
|
||||
|
||||
/** @var string */
|
||||
protected $serverRoot;
|
||||
|
||||
|
|
@ -41,6 +45,7 @@ class FactoryTest extends TestCase {
|
|||
$this->config = $this->createMock(IConfig::class);
|
||||
$this->request = $this->createMock(IRequest::class);
|
||||
$this->userSession = $this->createMock(IUserSession::class);
|
||||
$this->cacheFactory = $this->createMock(ICacheFactory::class);
|
||||
|
||||
$this->serverRoot = \OC::$SERVERROOT;
|
||||
}
|
||||
|
|
@ -64,13 +69,14 @@ class FactoryTest extends TestCase {
|
|||
$this->config,
|
||||
$this->request,
|
||||
$this->userSession,
|
||||
$this->cacheFactory,
|
||||
$this->serverRoot,
|
||||
])
|
||||
->setMethods($methods)
|
||||
->getMock();
|
||||
}
|
||||
|
||||
return new Factory($this->config, $this->request, $this->userSession, $this->serverRoot);
|
||||
return new Factory($this->config, $this->request, $this->userSession, $this->cacheFactory, $this->serverRoot);
|
||||
}
|
||||
|
||||
public function dataFindAvailableLanguages(): array {
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ namespace Test\L10N;
|
|||
use DateTime;
|
||||
use OC\L10N\Factory;
|
||||
use OC\L10N\L10N;
|
||||
use OCP\ICacheFactory;
|
||||
use OCP\IConfig;
|
||||
use OCP\IRequest;
|
||||
use OCP\IUserSession;
|
||||
|
|
@ -32,7 +33,8 @@ class L10nTest extends TestCase {
|
|||
$request = $this->createMock(IRequest::class);
|
||||
/** @var IUserSession $userSession */
|
||||
$userSession = $this->createMock(IUserSession::class);
|
||||
return new Factory($config, $request, $userSession, \OC::$SERVERROOT);
|
||||
$cacheFactory = $this->createMock(ICacheFactory::class);
|
||||
return new Factory($config, $request, $userSession, $cacheFactory, \OC::$SERVERROOT);
|
||||
}
|
||||
|
||||
public function testSimpleTranslationWithTrailingColon(): void {
|
||||
|
|
|
|||
Loading…
Reference in a new issue