From 2d769d10880543c7d750d6fed4e88717f5f17d9a Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 2 Feb 2022 13:37:49 +0100 Subject: [PATCH] cache mimetype mappings in local cache saves a db request for every request that touches mimetypes Signed-off-by: Robin Appelman --- lib/private/Files/Type/Loader.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/private/Files/Type/Loader.php b/lib/private/Files/Type/Loader.php index 8193a3f7b0350..33b72418a68e2 100644 --- a/lib/private/Files/Type/Loader.php +++ b/lib/private/Files/Type/Loader.php @@ -25,6 +25,8 @@ namespace OC\Files\Type; use OCP\Files\IMimeTypeLoader; +use OCP\ICache; +use OCP\ICacheFactory; use OCP\IDBConnection; /** @@ -43,13 +45,17 @@ class Loader implements IMimeTypeLoader { /** @var array [mimetype => id] */ protected $mimetypeIds; + /** @var ICache */ + private $cache; + /** * @param IDBConnection $dbConnection */ - public function __construct(IDBConnection $dbConnection) { + public function __construct(IDBConnection $dbConnection, ICacheFactory $cacheFactory) { $this->dbConnection = $dbConnection; $this->mimetypes = []; $this->mimetypeIds = []; + $this->cache = $cacheFactory->createLocal('mimetype::'); } /** @@ -140,6 +146,13 @@ protected function store($mimetype) { * Load all mimetypes from DB */ private function loadMimetypes() { + $cachedTypes = $this->cache->get('mimetypes'); + $cachedIds = $this->cache->get('mimeids'); + if ($cachedTypes && $cachedIds) { + $this->mimetypes = json_decode($cachedTypes, true); + $this->mimetypeIds = json_decode($cachedIds, true); + return; + } $qb = $this->dbConnection->getQueryBuilder(); $qb->select('id', 'mimetype') ->from('mimetypes'); @@ -152,6 +165,9 @@ private function loadMimetypes() { $this->mimetypes[$row['id']] = $row['mimetype']; $this->mimetypeIds[$row['mimetype']] = $row['id']; } + + $this->cache->set('mimetypes', json_encode($this->mimetypes)); + $this->cache->set('mimeids', json_encode($this->mimetypeIds)); } /**