]> source.dussan.org Git - nextcloud-server.git/commitdiff
Cache available languages locally
authorJulius Härtl <jus@bitgrid.net>
Thu, 17 Nov 2022 09:34:43 +0000 (10:34 +0100)
committerJulius Härtl <jus@bitgrid.net>
Wed, 7 Dec 2022 21:32:06 +0000 (22:32 +0100)
Signed-off-by: Julius Härtl <jus@bitgrid.net>
lib/private/L10N/Factory.php
lib/private/Server.php
tests/lib/L10N/FactoryTest.php
tests/lib/L10N/L10nTest.php

index 7fcfab37fa6a51d50c4d54836a1a6f5f68177869..71910873db77f93974ccb8befb551363abd42d28 100644 (file)
@@ -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;
        }
 
index 07e90843a980399ea7857345bd6459573f5e134c..d420b6fd11d7d13d2302da112e013d716e0a65f6 100644 (file)
@@ -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
                        );
                });
index 4e01602a4f7d602cbe5da503599654c66d0e0951..7af069cbff3a1c6166c1e8ed6fa48f71f33f2dec 100644 (file)
@@ -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 {
index f410c523c0511214e25cf03b416238232556e51f..f224592432c98d74cd20699ca252d33a2bf1f91a 100644 (file)
@@ -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 {