Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion apps/dav/lib/CardDAV/CardDavBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace OCA\DAV\CardDAV;

use OC\Cache\CappedMemoryCache;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCA\DAV\DAV\Sharing\Backend;
Expand Down Expand Up @@ -59,6 +60,9 @@ class CardDavBackend implements BackendInterface, SyncSupport {
/** @var Backend */
private $sharingBackend;

/** @var CappedMemoryCache Cache of card URI to db row ids */
private $idCache;

/** @var array properties to index */
public static $indexProperties = [
'BDAY', 'UID', 'N', 'FN', 'TITLE', 'ROLE', 'NOTE', 'NICKNAME',
Expand All @@ -85,6 +89,7 @@ public function __construct(IDBConnection $db,
$this->dispatcher = $dispatcher;
$this->sharingBackend = new Backend($this->db, $principalBackend, 'addressbook');
$this->legacyMode = $legacyMode;
$this->idCache = new CappedMemoryCache();
}

/**
Expand Down Expand Up @@ -428,7 +433,7 @@ function getCards($addressBookId) {
*
* @param mixed $addressBookId
* @param string $cardUri
* @return array
* @return array|false
*/
function getCard($addressBookId, $cardUri) {
$query = $this->db->getQueryBuilder();
Expand Down Expand Up @@ -522,6 +527,9 @@ function createCard($addressBookId, $cardUri, $cardData) {
])
->execute();

// Cache the ID so that it can be used for the updateProperties method
$this->idCache->set($addressBookId.$cardUri, $query->getLastInsertId());

$this->addChange($addressBookId, $cardUri, 1);
$this->updateProperties($addressBookId, $cardUri, $cardData);

Expand Down Expand Up @@ -982,6 +990,11 @@ protected function purgeProperties($addressBookId, $cardId) {
* @return int
*/
protected function getCardId($addressBookId, $uri) {
// Try to find cardId from own cache to avoid issue with db cluster
if($this->idCache->hasKey($addressBookId.$uri)) {
return $this->idCache->get($addressBookId.$uri);
}

$query = $this->db->getQueryBuilder();
$query->select('id')->from($this->dbCardsTable)
->where($query->expr()->eq('uri', $query->createNamedParameter($uri)))
Expand Down