Merge pull request #7529 from owncloud/getlocalfile-cache

Add caching for getLocalFile on remote storages
This commit is contained in:
icewind1991 2014-03-07 13:29:47 +01:00
commit 3eb58d9973
2 changed files with 116 additions and 101 deletions

View File

@ -208,11 +208,12 @@ class DAV extends \OC\Files\Storage\Common{
} else {
$ext = '';
}
$tmpFile = \OCP\Files::tmpFile($ext);
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
if ($this->file_exists($path)) {
$this->getFile($path, $tmpFile);
$tmpFile = $this->getCachedFile($path);
} else {
$tmpFile = \OCP\Files::tmpFile($ext);
}
\OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
self::$tempFiles[$tmpFile] = $path;
return fopen('close://' . $tmpFile, $mode);
}
@ -251,8 +252,7 @@ class DAV extends \OC\Files\Storage\Common{
if ($this->file_exists($path)) {
try {
$this->client->proppatch($this->encodePath($path), array('{DAV:}lastmodified' => $mtime));
}
catch (\Sabre_DAV_Exception_NotImplemented $e) {
} catch (\Sabre_DAV_Exception_NotImplemented $e) {
return false;
}
} else {
@ -261,17 +261,7 @@ class DAV extends \OC\Files\Storage\Common{
return true;
}
/**
* @param string $path
* @param string $target
*/
public function getFile($path, $target) {
$this->init();
$source=$this->fopen($path, 'r');
file_put_contents($target, $source);
}
public function uploadFile($path, $target) {
protected function uploadFile($path, $target) {
$this->init();
$source = fopen($path, 'r');
@ -295,6 +285,7 @@ class DAV extends \OC\Files\Storage\Common{
\OCP\Util::writeLog("webdav client", 'curl GET ' . curl_getinfo($curl, CURLINFO_EFFECTIVE_URL) . ' returned status code ' . $statusCode, \OCP\Util::ERROR);
}
curl_close($curl);
$this->removeCachedFile($target);
}
public function rename($path1, $path2) {
@ -303,6 +294,8 @@ class DAV extends \OC\Files\Storage\Common{
$path2 = $this->createBaseUri() . $this->encodePath($this->cleanPath($path2));
try {
$this->client->request('MOVE', $path1, null, array('Destination' => $path2));
$this->removeCachedFile($path1);
$this->removeCachedFile($path2);
return true;
} catch (\Exception $e) {
return false;
@ -315,6 +308,7 @@ class DAV extends \OC\Files\Storage\Common{
$path2 = $this->createBaseUri() . $this->encodePath($this->cleanPath($path2));
try {
$this->client->request('COPY', $path1, null, array('Destination' => $path2));
$this->removeCachedFile($path2);
return true;
} catch (\Exception $e) {
return false;
@ -368,6 +362,7 @@ class DAV extends \OC\Files\Storage\Common{
/**
* URL encodes the given path but keeps the slashes
*
* @param string $path to encode
* @return string encoded path
*/

View File

@ -27,6 +27,11 @@ abstract class Common implements \OC\Files\Storage\Storage {
protected $watcher;
protected $storageCache;
/**
* @var string[]
*/
protected $cachedFiles = array();
public function __construct($parameters) {
}
@ -122,11 +127,13 @@ abstract class Common implements \OC\Files\Storage\Storage {
public function file_put_contents($path, $data) {
$handle = $this->fopen($path, "w");
$this->removeCachedFile($path);
return fwrite($handle, $data);
}
public function rename($path1, $path2) {
if ($this->copy($path1, $path2)) {
$this->removeCachedFile($path1);
return $this->unlink($path1);
} else {
return false;
@ -137,6 +144,7 @@ abstract class Common implements \OC\Files\Storage\Storage {
$source = $this->fopen($path1, 'r');
$target = $this->fopen($path2, 'w');
list($count, $result) = \OC_Helper::streamCopy($source, $target);
$this->removeCachedFile($path2);
return $result;
}
@ -162,13 +170,14 @@ abstract class Common implements \OC\Files\Storage\Storage {
}
public function getLocalFile($path) {
return $this->toTmpFile($path);
return $this->getCachedFile($path);
}
/**
* @param string $path
* @return string
*/
private function toTmpFile($path) { //no longer in the storage api, still useful here
protected function toTmpFile($path) { //no longer in the storage api, still useful here
$source = $this->fopen($path, 'r');
if (!$source) {
return false;
@ -352,4 +361,15 @@ abstract class Common implements \OC\Files\Storage\Storage {
// default, which is not local
return false;
}
protected function getCachedFile($path) {
if (!isset($this->cachedFiles[$path])) {
$this->cachedFiles[$path] = $this->toTmpFile($path);
}
return $this->cachedFiles[$path];
}
protected function removeCachedFile($path) {
unset($this->cachedFiles[$path]);
}
}