Skip to content

Commit

Permalink
Merge pull request #3214 from nextcloud/cache-storage-info
Browse files Browse the repository at this point in the history
cache the storage info for 5 min
  • Loading branch information
icewind1991 authored Jan 24, 2017
2 parents 30e9d67 + cbc18b7 commit f55260b
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions lib/private/legacy/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class OC_Helper {

/**
* Creates an absolute url for public use
*
* @param string $service id
* @param bool $add_slash
* @return string the url
Expand All @@ -62,13 +63,14 @@ public static function linkToPublic($service, $add_slash = false) {
if ($service === 'files') {
$url = OC::$server->getURLGenerator()->getAbsoluteURL('/s');
} else {
$url = OC::$server->getURLGenerator()->getAbsoluteURL(OC::$server->getURLGenerator()->linkTo('', 'public.php').'?service='.$service);
$url = OC::$server->getURLGenerator()->getAbsoluteURL(OC::$server->getURLGenerator()->linkTo('', 'public.php') . '?service=' . $service);
}
return $url . (($add_slash && $service[strlen($service) - 1] != '/') ? '/' : '');
}

/**
* Make a human file size
*
* @param int $bytes file size in bytes
* @return string a human readable file size
*
Expand Down Expand Up @@ -104,6 +106,7 @@ public static function humanFileSize($bytes) {

/**
* Make a php file size
*
* @param int $bytes file size in bytes
* @return string a php parseable file size
*
Expand All @@ -130,6 +133,7 @@ public static function phpFileSize($bytes) {

/**
* Make a computer file size
*
* @param string $str file size in human readable format
* @return float a file size in bytes
*
Expand Down Expand Up @@ -172,6 +176,7 @@ public static function computerFileSize($str) {

/**
* Recursive copying of folders
*
* @param string $src source folder
* @param string $dest target folder
*
Expand All @@ -194,6 +199,7 @@ static function copyr($src, $dest) {

/**
* Recursive deletion of folders
*
* @param string $dir path to the folder
* @param bool $deleteSelf if set to false only the content of the folder will be deleted
* @return bool
Expand Down Expand Up @@ -393,6 +399,7 @@ public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $

/**
* performs a search in a nested array
*
* @param array $haystack the array to be searched
* @param string $needle the search string
* @param string $index optional, only search this key name
Expand Down Expand Up @@ -425,7 +432,7 @@ public static function recursiveArraySearch($haystack, $needle, $index = null) {
* @return int number of bytes representing
*/
public static function maxUploadFilesize($dir, $freeSpace = null) {
if (is_null($freeSpace) || $freeSpace < 0){
if (is_null($freeSpace) || $freeSpace < 0) {
$freeSpace = self::freeSpace($dir);
}
return min($freeSpace, self::uploadLimit());
Expand All @@ -443,7 +450,7 @@ public static function freeSpace($dir) {
$freeSpace = max($freeSpace, 0);
return $freeSpace;
} else {
return (INF > 0)? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188
return (INF > 0) ? INF : PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188
}
}

Expand Down Expand Up @@ -510,7 +517,7 @@ public static function findBinaryPath($program) {
if (empty($paths)) {
$paths = '/usr/local/bin /usr/bin /opt/bin /bin';
} else {
$paths = str_replace(':',' ',getenv('PATH'));
$paths = str_replace(':', ' ', getenv('PATH'));
}
$command = 'find ' . $paths . ' -name ' . escapeshellarg($program) . ' 2> /dev/null';
exec($command, $output, $returnCode);
Expand All @@ -533,6 +540,12 @@ public static function findBinaryPath($program) {
* @throws \OCP\Files\NotFoundException
*/
public static function getStorageInfo($path, $rootInfo = null) {
$memcache = \OC::$server->getMemCacheFactory()->create('storageInfo');
$cacheKey = $rootInfo ? '__root__' . md5($path) : md5($path);
$cached = $memcache->get($cacheKey);
if (is_array($cached)) {
return $cached;
}
// return storage info without adding mount points
$includeExtStorage = \OC::$server->getSystemConfig()->getValue('quota_include_external_storage', false);

Expand Down Expand Up @@ -597,10 +610,20 @@ public static function getStorageInfo($path, $rootInfo = null) {
$ownerId = $storage->getOwner($path);
$ownerDisplayName = '';
$owner = \OC::$server->getUserManager()->get($ownerId);
if($owner) {
if ($owner) {
$ownerDisplayName = $owner->getDisplayName();
}

$memcache->set($cacheKey, [
'free' => $free,
'used' => $used,
'quota' => $quota,
'total' => $total,
'relative' => $relative,
'owner' => $ownerId,
'ownerDisplayName' => $ownerDisplayName,
], 5 * 60);

return [
'free' => $free,
'used' => $used,
Expand Down Expand Up @@ -645,6 +668,7 @@ private static function getGlobalStorageInfo() {

/**
* Returns whether the config file is set manually to read-only
*
* @return bool
*/
public static function isReadOnlyConfigEnabled() {
Expand Down

0 comments on commit f55260b

Please sign in to comment.