Merge pull request #5123 from owncloud/cache_mimetypes

Load all mimetypes in one go
This commit is contained in:
Frank Karlitschek 2013-10-05 02:24:38 -07:00
commit 4bce2f8b85
1 changed files with 26 additions and 21 deletions

View File

@ -34,8 +34,8 @@ class Cache {
*/ */
private $storageCache; private $storageCache;
private $mimetypeIds = array(); private static $mimetypeIds = array();
private $mimetypes = array(); private static $mimetypes = array();
/** /**
* @param \OC\Files\Storage\Storage|string $storage * @param \OC\Files\Storage\Storage|string $storage
@ -64,30 +64,35 @@ class Cache {
* @return int * @return int
*/ */
public function getMimetypeId($mime) { public function getMimetypeId($mime) {
if (!isset($this->mimetypeIds[$mime])) { if (empty(self::$mimetypeIds)) {
$result = \OC_DB::executeAudited('SELECT `id` FROM `*PREFIX*mimetypes` WHERE `mimetype` = ?', array($mime)); $this->loadMimetypes();
if ($row = $result->fetchRow()) {
$this->mimetypeIds[$mime] = $row['id'];
} else {
$result = \OC_DB::executeAudited('INSERT INTO `*PREFIX*mimetypes`(`mimetype`) VALUES(?)', array($mime));
$this->mimetypeIds[$mime] = \OC_DB::insertid('*PREFIX*mimetypes');
}
$this->mimetypes[$this->mimetypeIds[$mime]] = $mime;
} }
return $this->mimetypeIds[$mime];
if (!isset(self::$mimetypeIds[$mime])) {
$result = \OC_DB::executeAudited('INSERT INTO `*PREFIX*mimetypes`(`mimetype`) VALUES(?)', array($mime));
self::$mimetypeIds[$mime] = \OC_DB::insertid('*PREFIX*mimetypes');
self::$mimetypes[self::$mimetypeIds[$mime]] = $mime;
}
return self::$mimetypeIds[$mime];
} }
public function getMimetype($id) { public function getMimetype($id) {
if (!isset($this->mimetypes[$id])) { if (empty(self::$mimetypes)) {
$sql = 'SELECT `mimetype` FROM `*PREFIX*mimetypes` WHERE `id` = ?'; $this->loadMimetypes();
$result = \OC_DB::executeAudited($sql, array($id));
if ($row = $result->fetchRow()) {
$this->mimetypes[$id] = $row['mimetype'];
} else {
return null;
}
} }
return $this->mimetypes[$id];
return isset(self::$mimetypes[$id]) ? self::$mimetypes[$id] : null;
}
protected function loadMimetypes(){
$result = \OC_DB::executeAudited('SELECT `id`, `mimetype` FROM `*PREFIX*mimetypes`', array());
if ($result) {
while ($row = $result->fetchRow()) {
self::$mimetypeIds[$row['mimetype']] = $row['id'];
self::$mimetypes[$row['id']] = $row['mimetype'];
}
}
} }
/** /**