Merge pull request #13748 from nextcloud/cache-insert-event
add event for inserting cache entries
This commit is contained in:
commit
baa6a2d52c
|
@ -173,6 +173,7 @@ return array(
|
||||||
'OCP\\Federation\\ICloudIdManager' => $baseDir . '/lib/public/Federation/ICloudIdManager.php',
|
'OCP\\Federation\\ICloudIdManager' => $baseDir . '/lib/public/Federation/ICloudIdManager.php',
|
||||||
'OCP\\Files' => $baseDir . '/lib/public/Files.php',
|
'OCP\\Files' => $baseDir . '/lib/public/Files.php',
|
||||||
'OCP\\Files\\AlreadyExistsException' => $baseDir . '/lib/public/Files/AlreadyExistsException.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\\ICache' => $baseDir . '/lib/public/Files/Cache/ICache.php',
|
||||||
'OCP\\Files\\Cache\\ICacheEntry' => $baseDir . '/lib/public/Files/Cache/ICacheEntry.php',
|
'OCP\\Files\\Cache\\ICacheEntry' => $baseDir . '/lib/public/Files/Cache/ICacheEntry.php',
|
||||||
'OCP\\Files\\Cache\\IPropagator' => $baseDir . '/lib/public/Files/Cache/IPropagator.php',
|
'OCP\\Files\\Cache\\IPropagator' => $baseDir . '/lib/public/Files/Cache/IPropagator.php',
|
||||||
|
|
|
@ -203,6 +203,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
|
||||||
'OCP\\Federation\\ICloudIdManager' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudIdManager.php',
|
'OCP\\Federation\\ICloudIdManager' => __DIR__ . '/../../..' . '/lib/public/Federation/ICloudIdManager.php',
|
||||||
'OCP\\Files' => __DIR__ . '/../../..' . '/lib/public/Files.php',
|
'OCP\\Files' => __DIR__ . '/../../..' . '/lib/public/Files.php',
|
||||||
'OCP\\Files\\AlreadyExistsException' => __DIR__ . '/../../..' . '/lib/public/Files/AlreadyExistsException.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\\ICache' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICache.php',
|
||||||
'OCP\\Files\\Cache\\ICacheEntry' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICacheEntry.php',
|
'OCP\\Files\\Cache\\ICacheEntry' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/ICacheEntry.php',
|
||||||
'OCP\\Files\\Cache\\IPropagator' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/IPropagator.php',
|
'OCP\\Files\\Cache\\IPropagator' => __DIR__ . '/../../..' . '/lib/public/Files/Cache/IPropagator.php',
|
||||||
|
|
|
@ -40,10 +40,12 @@ namespace OC\Files\Cache;
|
||||||
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
||||||
use OCP\DB\QueryBuilder\IQueryBuilder;
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
||||||
use Doctrine\DBAL\Driver\Statement;
|
use Doctrine\DBAL\Driver\Statement;
|
||||||
|
use OCP\Files\Cache\CacheInsertEvent;
|
||||||
use OCP\Files\Cache\ICache;
|
use OCP\Files\Cache\ICache;
|
||||||
use OCP\Files\Cache\ICacheEntry;
|
use OCP\Files\Cache\ICacheEntry;
|
||||||
use \OCP\Files\IMimeTypeLoader;
|
use \OCP\Files\IMimeTypeLoader;
|
||||||
use OCP\Files\Search\ISearchQuery;
|
use OCP\Files\Search\ISearchQuery;
|
||||||
|
use OCP\Files\Storage\IStorage;
|
||||||
use OCP\IDBConnection;
|
use OCP\IDBConnection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -71,6 +73,8 @@ class Cache implements ICache {
|
||||||
*/
|
*/
|
||||||
protected $storageId;
|
protected $storageId;
|
||||||
|
|
||||||
|
private $storage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Storage $storageCache
|
* @var Storage $storageCache
|
||||||
*/
|
*/
|
||||||
|
@ -84,18 +88,17 @@ class Cache implements ICache {
|
||||||
*/
|
*/
|
||||||
protected $connection;
|
protected $connection;
|
||||||
|
|
||||||
|
protected $eventDispatcher;
|
||||||
|
|
||||||
/** @var QuerySearchHelper */
|
/** @var QuerySearchHelper */
|
||||||
protected $querySearchHelper;
|
protected $querySearchHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param \OC\Files\Storage\Storage|string $storage
|
* @param IStorage $storage
|
||||||
*/
|
*/
|
||||||
public function __construct($storage) {
|
public function __construct(IStorage $storage) {
|
||||||
if ($storage instanceof \OC\Files\Storage\Storage) {
|
$this->storageId = $storage->getId();
|
||||||
$this->storageId = $storage->getId();
|
$this->storage = $storage;
|
||||||
} else {
|
|
||||||
$this->storageId = $storage;
|
|
||||||
}
|
|
||||||
if (strlen($this->storageId) > 64) {
|
if (strlen($this->storageId) > 64) {
|
||||||
$this->storageId = md5($this->storageId);
|
$this->storageId = md5($this->storageId);
|
||||||
}
|
}
|
||||||
|
@ -103,6 +106,7 @@ class Cache implements ICache {
|
||||||
$this->storageCache = new Storage($storage);
|
$this->storageCache = new Storage($storage);
|
||||||
$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
|
$this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
|
||||||
$this->connection = \OC::$server->getDatabaseConnection();
|
$this->connection = \OC::$server->getDatabaseConnection();
|
||||||
|
$this->eventDispatcher = \OC::$server->getEventDispatcher();
|
||||||
$this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader);
|
$this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,9 +287,11 @@ class Cache implements ICache {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($builder->execute()) {
|
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
|
// entry exists already
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,8 @@
|
||||||
|
|
||||||
namespace OC\Files\Cache;
|
namespace OC\Files\Cache;
|
||||||
|
|
||||||
|
use OCP\Files\Storage\IStorage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle the mapping between the string and numeric storage ids
|
* Handle the mapping between the string and numeric storage ids
|
||||||
*
|
*
|
||||||
|
@ -61,7 +63,7 @@ class Storage {
|
||||||
* @throws \RuntimeException
|
* @throws \RuntimeException
|
||||||
*/
|
*/
|
||||||
public function __construct($storage, $isAvailable = true) {
|
public function __construct($storage, $isAvailable = true) {
|
||||||
if ($storage instanceof \OC\Files\Storage\Storage) {
|
if ($storage instanceof IStorage) {
|
||||||
$this->storageId = $storage->getId();
|
$this->storageId = $storage->getId();
|
||||||
} else {
|
} else {
|
||||||
$this->storageId = $storage;
|
$this->storageId = $storage;
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -290,7 +290,8 @@ class FolderTest extends NodeTest {
|
||||||
->method('getUser')
|
->method('getUser')
|
||||||
->will($this->returnValue($this->user));
|
->will($this->returnValue($this->user));
|
||||||
$storage = $this->createMock(Storage::class);
|
$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())
|
$storage->expects($this->once())
|
||||||
->method('getCache')
|
->method('getCache')
|
||||||
|
@ -340,8 +341,10 @@ class FolderTest extends NodeTest {
|
||||||
$root->expects($this->any())
|
$root->expects($this->any())
|
||||||
->method('getUser')
|
->method('getUser')
|
||||||
->will($this->returnValue($this->user));
|
->will($this->returnValue($this->user));
|
||||||
|
/** @var \PHPUnit_Framework_MockObject_MockObject|Storage $storage */
|
||||||
$storage = $this->createMock(Storage::class);
|
$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 = $this->createMock(IMountPoint::class);
|
||||||
$mount->expects($this->once())
|
$mount->expects($this->once())
|
||||||
|
@ -391,7 +394,8 @@ class FolderTest extends NodeTest {
|
||||||
->method('getUser')
|
->method('getUser')
|
||||||
->will($this->returnValue($this->user));
|
->will($this->returnValue($this->user));
|
||||||
$storage = $this->createMock(Storage::class);
|
$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 = $this->createMock(IMountPoint::class);
|
||||||
$mount->expects($this->once())
|
$mount->expects($this->once())
|
||||||
|
@ -441,7 +445,8 @@ class FolderTest extends NodeTest {
|
||||||
->method('getUser')
|
->method('getUser')
|
||||||
->will($this->returnValue($this->user));
|
->will($this->returnValue($this->user));
|
||||||
$storage = $this->createMock(Storage::class);
|
$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 = $this->createMock(IMountPoint::class);
|
||||||
$mount->expects($this->once())
|
$mount->expects($this->once())
|
||||||
|
@ -491,8 +496,9 @@ class FolderTest extends NodeTest {
|
||||||
->method('getUser')
|
->method('getUser')
|
||||||
->will($this->returnValue($this->user));
|
->will($this->returnValue($this->user));
|
||||||
$storage = $this->createMock(Storage::class);
|
$storage = $this->createMock(Storage::class);
|
||||||
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
|
$storage->method('getId')->willReturn('');
|
||||||
$subCache = $this->getMockBuilder(Cache::class)->setConstructorArgs([''])->getMock();
|
$cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
|
||||||
|
$subCache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock();
|
||||||
$subStorage = $this->createMock(Storage::class);
|
$subStorage = $this->createMock(Storage::class);
|
||||||
$subMount = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock();
|
$subMount = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([null, ''])->getMock();
|
||||||
|
|
||||||
|
@ -572,7 +578,8 @@ class FolderTest extends NodeTest {
|
||||||
->getMock();
|
->getMock();
|
||||||
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
|
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
|
||||||
$mount = new MountPoint($storage, '/bar');
|
$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);
|
$fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null);
|
||||||
|
|
||||||
|
@ -625,7 +632,8 @@ class FolderTest extends NodeTest {
|
||||||
->getMock();
|
->getMock();
|
||||||
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
|
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
|
||||||
$mount = new MountPoint($storage, '/bar');
|
$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);
|
$fileInfo = new CacheEntry(['path' => '', 'mimetype' => 'text/plain'], null);
|
||||||
|
|
||||||
|
@ -673,7 +681,8 @@ class FolderTest extends NodeTest {
|
||||||
->getMock();
|
->getMock();
|
||||||
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
|
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
|
||||||
$mount = new MountPoint($storage, '/bar');
|
$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);
|
$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);
|
$storage = $this->createMock(\OC\Files\Storage\Storage::class);
|
||||||
$mount1 = new MountPoint($storage, '/bar');
|
$mount1 = new MountPoint($storage, '/bar');
|
||||||
$mount2 = new MountPoint($storage, '/bar/foo/asd');
|
$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);
|
$fileInfo = new CacheEntry(['path' => 'foo/qwerty', 'mimetype' => 'text/plain'], null);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue