cache calendar objects from calendarQuery

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2023-08-14 16:19:41 +02:00 committed by Anna
parent 9a3add4de2
commit ea49a95d5a
2 changed files with 35 additions and 10 deletions

View file

@ -215,6 +215,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
private IConfig $config;
private bool $legacyEndpoint;
private string $dbObjectPropertiesTable = 'calendarobjects_props';
private array $cachedObjects = [];
public function __construct(IDBConnection $db,
Principal $principalBackend,
@ -1117,6 +1118,10 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @return array|null
*/
public function getCalendarObject($calendarId, $objectUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR) {
$key = $calendarId . '::' . $objectUri . '::' . $calendarType;
if (isset($this->cachedObjects[$key])) {
return $this->cachedObjects[$key];
}
$query = $this->db->getQueryBuilder();
$query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification', 'deleted_at'])
->from('calendarobjects')
@ -1131,6 +1136,12 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
return null;
}
$object = $this->rowToCalendarObject($row);
$this->cachedObjects[$key] = $object;
return $object;
}
private function rowToCalendarObject(array $row): array {
return [
'id' => $row['id'],
'uri' => $row['uri'],
@ -1217,6 +1228,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @return string
*/
public function createCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) {
$this->cachedObjects = [];
$extraData = $this->getDenormalizedData($calendarData);
// Try to detect duplicates
@ -1309,6 +1321,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @return string
*/
public function updateCalendarObject($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) {
$this->cachedObjects = [];
$extraData = $this->getDenormalizedData($calendarData);
$query = $this->db->getQueryBuilder();
$query->update('calendarobjects')
@ -1359,6 +1372,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @throws Exception
*/
public function moveCalendarObject(int $sourceCalendarId, int $targetCalendarId, int $objectId, string $oldPrincipalUri, string $newPrincipalUri, int $calendarType = self::CALENDAR_TYPE_CALENDAR): bool {
$this->cachedObjects = [];
$object = $this->getCalendarObjectById($oldPrincipalUri, $objectId);
if (empty($object)) {
return false;
@ -1404,6 +1418,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @param int $classification
*/
public function setClassification($calendarObjectId, $classification) {
$this->cachedObjects = [];
if (!in_array($classification, [
self::CLASSIFICATION_PUBLIC, self::CLASSIFICATION_PRIVATE, self::CLASSIFICATION_CONFIDENTIAL
])) {
@ -1428,6 +1443,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @return void
*/
public function deleteCalendarObject($calendarId, $objectUri, $calendarType = self::CALENDAR_TYPE_CALENDAR, bool $forceDeletePermanently = false) {
$this->cachedObjects = [];
$data = $this->getCalendarObject($calendarId, $objectUri, $calendarType);
if ($data === null) {
@ -1507,6 +1523,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @throws Forbidden
*/
public function restoreCalendarObject(array $objectData): void {
$this->cachedObjects = [];
$id = (int) $objectData['id'];
$restoreUri = str_replace("-deleted.ics", ".ics", $objectData['uri']);
$targetObject = $this->getCalendarObject(
@ -1632,12 +1649,8 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
}
}
}
$columns = ['uri'];
if ($requirePostFilter) {
$columns = ['uri', 'calendardata'];
}
$query = $this->db->getQueryBuilder();
$query->select($columns)
$query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification', 'deleted_at'])
->from('calendarobjects')
->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId)))
->andWhere($query->expr()->eq('calendartype', $query->createNamedParameter($calendarType)))
@ -1658,6 +1671,11 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
$result = [];
while ($row = $stmt->fetch()) {
// if we leave it as a blob we can't read it both from the post filter and the rowToCalendarObject
if (isset($row['calendardata'])) {
$row['calendardata'] = $this->readBlob($row['calendardata']);
}
if ($requirePostFilter) {
// validateFilterForObject will parse the calendar data
// catch parsing errors
@ -1682,6 +1700,8 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
}
}
$result[] = $row['uri'];
$key = $calendarId . '::' . $row['uri'] . '::' . $calendarType;
$this->cachedObjects[$key] = $this->rowToCalendarObject($row);
}
return $result;
@ -2631,6 +2651,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @return void
*/
public function deleteSchedulingObject($principalUri, $objectUri) {
$this->cachedObjects = [];
$query = $this->db->getQueryBuilder();
$query->delete('schedulingobjects')
->where($query->expr()->eq('principaluri', $query->createNamedParameter($principalUri)))
@ -2647,6 +2668,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @return void
*/
public function createSchedulingObject($principalUri, $objectUri, $objectData) {
$this->cachedObjects = [];
$query = $this->db->getQueryBuilder();
$query->insert('schedulingobjects')
->values([
@ -2670,6 +2692,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @return void
*/
protected function addChange($calendarId, $objectUri, $operation, $calendarType = self::CALENDAR_TYPE_CALENDAR) {
$this->cachedObjects = [];
$table = $calendarType === self::CALENDAR_TYPE_CALENDAR ? 'calendars': 'calendarsubscriptions';
$query = $this->db->getQueryBuilder();
@ -2836,7 +2859,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
return $this->calendarSharingBackend->getShares($resourceId);
}
public function preloadShares(array $resourceIds) {
public function preloadShares(array $resourceIds): void {
$this->calendarSharingBackend->preloadShares($resourceIds);
}
@ -2909,6 +2932,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @param int $calendarType
*/
public function updateProperties($calendarId, $objectUri, $calendarData, $calendarType = self::CALENDAR_TYPE_CALENDAR) {
$this->cachedObjects = [];
$objectId = $this->getCalendarObjectId($calendarId, $objectUri, $calendarType);
try {
@ -3067,6 +3091,7 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
* @param int $objectId
*/
protected function purgeProperties($calendarId, $objectId) {
$this->cachedObjects = [];
$query = $this->db->getQueryBuilder();
$query->delete($this->dbObjectPropertiesTable)
->where($query->expr()->eq('objectid', $query->createNamedParameter($objectId)))

View file

@ -201,13 +201,13 @@ class Backend {
];
}
$this->shareCache->set($resourceId, $shares);
$this->shareCache->set((string) $resourceId, $shares);
return $shares;
}
public function preloadShares(array $resourceIds) {
public function preloadShares(array $resourceIds): void {
$resourceIds = array_filter($resourceIds, function(int $resourceId) {
return !isset($this->shareCache[$resourceId]);
return !isset($this->shareCache[(string) $resourceId]);
});
if (count($resourceIds) === 0) {
return;
@ -235,7 +235,7 @@ class Backend {
}
foreach ($resourceIds as $resourceId) {
$this->shareCache->set($resourceId, $sharesByResource[$resourceId]);
$this->shareCache->set((string) $resourceId, $sharesByResource[$resourceId]);
}
}