Skip to content

Commit

Permalink
Add cache for new card uri<->id to fix db cluster execution
Browse files Browse the repository at this point in the history
  • Loading branch information
tomneedham committed Jul 5, 2017
1 parent 84294e1 commit f87e36a
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion apps/dav/lib/CardDAV/CardDavBackend.php
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

0 comments on commit f87e36a

Please sign in to comment.