Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/private/Files/Type/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
namespace OC\Files\Type;

use OCP\Files\IMimeTypeLoader;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IDBConnection;

/**
Expand All @@ -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::');
}

/**
Expand Down Expand Up @@ -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');
Expand All @@ -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));
Comment on lines +169 to +170
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it has no TTL, we need to document that people need to clear the cache when they add a new mimetype manually?
E.g. in

class RepairMimeTypes implements IRepairStep {

}

/**
Expand Down