Merge pull request #13748 from nextcloud/cache-insert-event

add event for inserting cache entries
This commit is contained in:
Morris Jobke 2019-02-07 16:35:02 +01:00 committed by GitHub
commit baa6a2d52c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 112 additions and 20 deletions

View File

@ -173,6 +173,7 @@ return array(
'OCP\\Federation\\ICloudIdManager' => $baseDir . '/lib/public/Federation/ICloudIdManager.php',
'OCP\\Files' => $baseDir . '/lib/public/Files.php',
'OCP\\Files\\AlreadyExistsException' => $baseDir . '/lib/public/Files/AlreadyExistsException.php',
'OCP\\Files\\Cache\\CacheInsertEvent' => $baseDir . '/lib/public/Files/Cache/CacheInsertEvent.php',
'OCP\\Files\\Cache\\ICache' => $baseDir . '/lib/public/Files/Cache/ICache.php',
'OCP\\Files\\Cache\\ICacheEntry' => $baseDir . '/lib/public/Files/Cache/ICacheEntry.php',
'OCP\\Files\\Cache\\IPropagator' => $baseDir . '/lib/public/Files/Cache/IPropagator.php',

View File

@ -203,6 +203,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Federation\\ICloudIdManager' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudIdManager.php',
'OCP\\Files' => __DIR__ . '/../../..' . '/lib/public/Files.php',
'OCP\\Files\\AlreadyExistsException' => __DIR__ . '/../../..' . '/lib/public/Files/AlreadyExistsException.php',
'OCP\\Files\\Cache\\CacheInsertEvent' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/CacheInsertEvent.php',
'OCP\\Files\\Cache\\ICache' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICache.php',
'OCP\\Files\\Cache\\ICacheEntry' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICacheEntry.php',
'OCP\\Files\\Cache\\IPropagator' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/IPropagator.php',

View File

@ -40,10 +40,12 @@ namespace OC\Files\Cache;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Doctrine\DBAL\Driver\Statement;
use OCP\Files\Cache\CacheInsertEvent;
use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry;
use \OCP\Files\IMimeTypeLoader;
use OCP\Files\Search\ISearchQuery;
use OCP\Files\Storage\IStorage;
use OCP\IDBConnection;
/**
@ -71,6 +73,8 @@ class Cache implements ICache {
*/
protected $storageId;
private $storage;
/**
* @var Storage $storageCache
*/
@ -84,18 +88,17 @@ class Cache implements ICache {
*/
protected $connection;
protected $eventDispatcher;
/** @var QuerySearchHelper */
protected $querySearchHelper;
/**
* @param \OC\Files\Storage\Storage|string $storage
* @param IStorage $storage
*/
public function __construct($storage) {
if ($storage instanceof \OC\Files\Storage\Storage) {
$this->storageId = $storage->getId();
} else {
$this->storageId = $storage;
}
public function __construct(IStorage $storage) {
$this->storageId = $storage->getId();
$this->storage = $storage;
if (strlen($this->storageId) > 64) {
$this->storageId = md5($this->storageId);
}
@ -103,6 +106,7 @@ class Cache implements ICache {
$this->storageCache = new Storage($storage);
$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
$this->connection = \OC::$server->getDatabaseConnection();
$this->eventDispatcher = \OC::$server->getEventDispatcher();
$this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader);
}
@ -283,9 +287,11 @@ class Cache implements ICache {
}
if ($builder->execute()) {
return (int)$this->connection->lastInsertId('*PREFIX*filecache');
$fileId = (int)$this->connection->lastInsertId('*PREFIX*filecache');
$this->eventDispatcher->dispatch(CacheInsertEvent::class, new CacheInsertEvent($this->storage, $file, $fileId));
return $fileId;
}
} catch(UniqueConstraintViolationException $e) {
} catch (UniqueConstraintViolationException $e) {
// entry exists already
}

View File

@ -28,6 +28,8 @@
namespace OC\Files\Cache;
use OCP\Files\Storage\IStorage;
/**
* Handle the mapping between the string and numeric storage ids
*
@ -61,7 +63,7 @@ class Storage {
* @throws \RuntimeException
*/
public function __construct($storage, $isAvailable = true) {
if ($storage instanceof \OC\Files\Storage\Storage) {
if ($storage instanceof IStorage) {
$this->storageId = $storage->getId();
} else {
$this->storageId = $storage;

View File

@ -0,0 +1,72 @@
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2019 Robin Appelman <robin@icewind.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\Files\Cache;
use OCP\Files\Storage\IStorage;
use Symfony\Component\EventDispatcher\Event;
/**
* @since 16.0.0
*/
class CacheInsertEvent extends Event {
private $storage;
private $path;
private $fileId;
/**
* CacheInsertEvent constructor.
*
* @param IStorage $storage
* @param string $path
* @param int $fileId
* @since 16.0.0
*/
public function __construct(IStorage $storage, string $path, int $fileId) {
$this->storage = $storage;
$this->path = $path;
$this->fileId = $fileId;
}
/**
* @return IStorage
* @since 16.0.0
*/
public function getStorage(): IStorage {
return $this->storage;
}
/**
* @return string
* @since 16.0.0
*/
public function getPath(): string {
return $this->path;
}
/**
* @return int
* @since 16.0.0
*/
public function getFileId(): int {
return $this->fileId;
}
}

View File

@ -290,7 +290,8 @@ class FolderTest extends NodeTest {
->method('getUser')
->will($this->returnValue($this->user));
$storage = $this->createMock(Storage::class);
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$storage->expects($this->once())
->method('getCache')
@ -340,8 +341,10 @@ class FolderTest extends NodeTest {
$root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
/** @var \PHPUnit_Framework_MockObject_MockObject|Storage $storage */
$storage = $this->createMock(Storage::class);
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$mount = $this->createMock(IMountPoint::class);
$mount->expects($this->once())
@ -391,7 +394,8 @@ class FolderTest extends NodeTest {
->method('getUser')
->will($this->returnValue($this->user));
$storage = $this->createMock(Storage::class);
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$mount = $this->createMock(IMountPoint::class);
$mount->expects($this->once())
@ -441,7 +445,8 @@ class FolderTest extends NodeTest {
->method('getUser')
->will($this->returnValue($this->user));
$storage = $this->createMock(Storage::class);
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$mount = $this->createMock(IMountPoint::class);
$mount->expects($this->once())
@ -491,8 +496,9 @@ class FolderTest extends NodeTest {
->method('getUser')
->will($this->returnValue($this->user));
$storage = $this->createMock(Storage::class);
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
$subCache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$subCache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$subStorage = $this->createMock(Storage::class);
$subMount = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock();
@ -572,7 +578,8 @@ class FolderTest extends NodeTest {
->getMock();
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
$mount = new MountPoint($storage, '/bar');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null);
@ -625,7 +632,8 @@ class FolderTest extends NodeTest {
->getMock();
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
$mount = new MountPoint($storage, '/bar');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$fileInfo = new CacheEntry(['path' => '', 'mimetype' => 'text/plain'], null);
@ -673,7 +681,8 @@ class FolderTest extends NodeTest {
->getMock();
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
$mount = new MountPoint($storage, '/bar');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$fileInfo = new CacheEntry(['path' => 'foobar', 'mimetype' => 'text/plain'], null);
@ -726,7 +735,8 @@ class FolderTest extends NodeTest {
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
$mount1 = new MountPoint($storage, '/bar');
$mount2 = new MountPoint($storage, '/bar/foo/asd');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
$storage->method('getId')->willReturn('');
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
$fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null);