From ae17f4d1b5d1949a907d0c89d011c717fe32fd1d Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Mon, 26 Apr 2021 17:57:10 -0400 Subject: [PATCH 01/18] Fix typo --- tests/Database/Adapter/MariaDBTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/Adapter/MariaDBTest.php b/tests/Database/Adapter/MariaDBTest.php index 4a01997c5..042c1ae57 100644 --- a/tests/Database/Adapter/MariaDBTest.php +++ b/tests/Database/Adapter/MariaDBTest.php @@ -15,7 +15,7 @@ class MariaDBTest extends Base static $database = null; /** - * @reture Adapter + * @return Adapter */ static function getDatabase(): Database { From a4e720adcece40b729648bde94283207a3d89606 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Mon, 26 Apr 2021 17:57:38 -0400 Subject: [PATCH 02/18] Upgrade utopia-php/cache to 0.4.0 --- composer.json | 2 +- composer.lock | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 085230290..870f363d5 100755 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "php": ">=7.1", "ext-pdo": "*", "utopia-php/framework": "0.*.*", - "utopia-php/cache": "0.3.*", + "utopia-php/cache": "0.4.0", "mongodb/mongodb": "1.8.0" }, "require-dev": { diff --git a/composer.lock b/composer.lock index 3327487b3..2bf15be73 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "dea2465791a58ea901b688ea9eae5b72", + "content-hash": "fa7d9c6d206e196d739c246ce67aac67", "packages": [ { "name": "composer/package-versions-deprecated", @@ -287,16 +287,16 @@ }, { "name": "utopia-php/cache", - "version": "0.3.0", + "version": "0.4.0", "source": { "type": "git", "url": "https://github.com/utopia-php/cache.git", - "reference": "e6b8b99d445c4bba5f51857e3d2a3c39a42d73a2" + "reference": "81c7806e13091a9d585ad9bba57fae625a2cf26c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/cache/zipball/e6b8b99d445c4bba5f51857e3d2a3c39a42d73a2", - "reference": "e6b8b99d445c4bba5f51857e3d2a3c39a42d73a2", + "url": "https://api.github.com/repos/utopia-php/cache/zipball/81c7806e13091a9d585ad9bba57fae625a2cf26c", + "reference": "81c7806e13091a9d585ad9bba57fae625a2cf26c", "shasum": "" }, "require": { @@ -334,9 +334,9 @@ ], "support": { "issues": "https://github.com/utopia-php/cache/issues", - "source": "https://github.com/utopia-php/cache/tree/0.3.0" + "source": "https://github.com/utopia-php/cache/tree/0.4.0" }, - "time": "2021-04-20T21:37:21+00:00" + "time": "2021-04-22T14:28:14+00:00" }, { "name": "utopia-php/framework", From aab22016e1be23a4d1e15b471fe2acc23686746b Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Mon, 26 Apr 2021 18:06:39 -0400 Subject: [PATCH 03/18] Use redis cache in testing --- docker-compose.yml | 6 ++++++ tests/Database/Adapter/MariaDBTest.php | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index fb1bdc1ed..71e36278c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -67,5 +67,11 @@ services: MYSQL_PASSWORD: password MYSQL_TCP_PORT: 3307 + redis: + image: redis:6.0-alpine + container_name: redis + networks: + - database + networks: database: \ No newline at end of file diff --git a/tests/Database/Adapter/MariaDBTest.php b/tests/Database/Adapter/MariaDBTest.php index 042c1ae57..85b7a791e 100644 --- a/tests/Database/Adapter/MariaDBTest.php +++ b/tests/Database/Adapter/MariaDBTest.php @@ -3,8 +3,11 @@ namespace Utopia\Tests\Adapter; use PDO; +use Redis; use Utopia\Database\Database; use Utopia\Database\Adapter\MariaDB; +use Utopia\Cache\Cache; +use Utopia\Cache\Adapter\Redis as RedisAdapter; use Utopia\Tests\Base; class MariaDBTest extends Base @@ -36,7 +39,11 @@ static function getDatabase(): Database PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ]); - $database = new Database(new MariaDB($pdo)); + $redis = new Redis(); + $redis->connect('redis', 6379); + $cache = new Cache(new RedisAdapter($redis)); + + $database = new Database(new MariaDB($pdo), $cache); $database->setNamespace('myapp_'.uniqid()); return self::$database = $database; From 3eed65fbf86fef92ae492feea42149b2080d79a5 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Mon, 26 Apr 2021 18:08:03 -0400 Subject: [PATCH 04/18] Check cache first on getDocument --- src/Database/Database.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index f9b176b9c..ec855c7e4 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -7,6 +7,7 @@ use Utopia\Database\Validator\Structure; use Utopia\Database\Exception\Authorization as AuthorizationException; use Utopia\Database\Exception\Structure as StructureException; +use Utopia\Cache\Cache; class Database { @@ -36,11 +37,19 @@ class Database // Collections const COLLECTIONS = 'collections'; + // Cache + const TTL = 60 * 60 * 24; // 24 hours + /** * @var Adapter */ protected $adapter; + /** + * @var Cache + */ + protected $cache; + /** * Parent Collection * Defines the structure for both system and custom collections @@ -90,10 +99,12 @@ class Database /** * @param Adapter $adapter + * @param Cache $cache (optional) */ - public function __construct(Adapter $adapter) + public function __construct(Adapter $adapter, Cache $cache = null) { $this->adapter = $adapter; + $this->cache = $cache; self::addFilter('json', function($value) { @@ -440,7 +451,16 @@ public function getDocument(string $collection, string $id): Document } $collection = $this->getCollection($collection); - $document = $this->adapter->getDocument($collection->getId(), $id); + + if ($cache) { + $document = $this->cache->load($id, self::TTL); + } + + if (!$document) { + $document = $this->adapter->getDocument($collection->getId(), $id); + + $this->cache->save($id, $document); // save to cache after fetching from db + } $document->setAttribute('$collection', $collection->getId()); From 23d23f29d995c4796176814dd7d2a27ad6fc0264 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 27 Apr 2021 07:38:02 -0400 Subject: [PATCH 05/18] Update cache on document update or delete --- src/Database/Database.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index ec855c7e4..e9a819e35 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -452,7 +452,7 @@ public function getDocument(string $collection, string $id): Document $collection = $this->getCollection($collection); - if ($cache) { + if ($this->cache) { $document = $this->cache->load($id, self::TTL); } @@ -564,6 +564,11 @@ public function updateDocument(string $collection, string $id, Document $documen } $document = $this->adapter->updateDocument($collection->getId(), $document); + + if ($this->cache) { + $this->cache->purge($id); + $this->cache->save($id, $document); + } $document = $this->decode($collection, $document); @@ -588,6 +593,10 @@ public function deleteDocument(string $collection, string $id): bool throw new AuthorizationException($validator->getDescription()); } + if ($this->cache) { + $this->cache->purge($id); + } + return $this->adapter->deleteDocument($collection, $id); } From d70b393c28baf26473924d396bed60bf77b5c1d9 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 27 Apr 2021 08:26:25 -0400 Subject: [PATCH 06/18] Install redis lib in testing container --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index f3f281f43..44df6dba4 100755 --- a/Dockerfile +++ b/Dockerfile @@ -20,8 +20,8 @@ RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN \ apk update \ && apk add --no-cache postgresql-libs postgresql-dev make automake autoconf gcc g++ \ - && pecl install mongodb \ - && docker-php-ext-enable mongodb \ + && pecl install mongodb redis \ + && docker-php-ext-enable mongodb redis \ && docker-php-ext-install opcache pgsql pdo_mysql pdo_pgsql \ && rm -rf /var/cache/apk/* From bf3dd6ccf387686f49128e4638e17d71bc3140e1 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 27 Apr 2021 08:33:45 -0400 Subject: [PATCH 07/18] Properly assign $document --- src/Database/Database.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Database/Database.php b/src/Database/Database.php index e9a819e35..acfc169ab 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -451,6 +451,7 @@ public function getDocument(string $collection, string $id): Document } $collection = $this->getCollection($collection); + $document = null; if ($this->cache) { $document = $this->cache->load($id, self::TTL); From 71119bfd8cec3fc89b7d0e746f69ca63e4ab0c3e Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 27 Apr 2021 10:10:21 -0400 Subject: [PATCH 08/18] Hydrate document string to Document object --- src/Database/Database.php | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index acfc169ab..91d4df287 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -452,17 +452,22 @@ public function getDocument(string $collection, string $id): Document $collection = $this->getCollection($collection); $document = null; + $cache = null; if ($this->cache) { - $document = $this->cache->load($id, self::TTL); + $cache = json_decode($this->cache->load($id, self::TTL), true); + $document = ($cache) ? new Document() : null; + + if (!$document) { + foreach ($cache as $key => $value) $document->{$key} = $value; + } } if (!$document) { - $document = $this->adapter->getDocument($collection->getId(), $id); - - $this->cache->save($id, $document); // save to cache after fetching from db + $document = $this->adapter->getDocument($collection->getId(), $id); } + // $document = $this->adapter->getDocument($collection->getId(), $id); $document->setAttribute('$collection', $collection->getId()); $validator = new Authorization($document, self::PERMISSION_READ); @@ -475,6 +480,10 @@ public function getDocument(string $collection, string $id): Document return $document; } + if($cache) { + $this->cache->save($id, json_encode($document)); // save to cache after fetching from db + } + $document = $this->casting($collection, $document); $document = $this->decode($collection, $document); @@ -565,13 +574,12 @@ public function updateDocument(string $collection, string $id, Document $documen } $document = $this->adapter->updateDocument($collection->getId(), $document); - + $document = $this->decode($collection, $document); + if ($this->cache) { $this->cache->purge($id); - $this->cache->save($id, $document); + $this->cache->save($id, json_encode($document)); } - - $document = $this->decode($collection, $document); return $document; } From bb055abdbd67d931d457092fa834360758c0df27 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 27 Apr 2021 11:38:44 -0400 Subject: [PATCH 09/18] Fix call to adapter --- src/Database/Database.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 91d4df287..1d02e9a68 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -456,18 +456,13 @@ public function getDocument(string $collection, string $id): Document if ($this->cache) { $cache = json_decode($this->cache->load($id, self::TTL), true); - $document = ($cache) ? new Document() : null; - - if (!$document) { - foreach ($cache as $key => $value) $document->{$key} = $value; - } + $document = new Document($cache ?? []); } - if (!$document) { + if ($document->isEmpty()) { $document = $this->adapter->getDocument($collection->getId(), $id); } - // $document = $this->adapter->getDocument($collection->getId(), $id); $document->setAttribute('$collection', $collection->getId()); $validator = new Authorization($document, self::PERMISSION_READ); From 4df301e7150a8014dbca491daa1b446b1e47ac88 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 27 Apr 2021 11:45:27 -0400 Subject: [PATCH 10/18] Save array copy of Document object --- src/Database/Database.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 1d02e9a68..34a77b46f 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -476,7 +476,7 @@ public function getDocument(string $collection, string $id): Document } if($cache) { - $this->cache->save($id, json_encode($document)); // save to cache after fetching from db + $this->cache->save($id, json_encode($document->getArrayCopy())); // save to cache after fetching from db } $document = $this->casting($collection, $document); @@ -573,7 +573,7 @@ public function updateDocument(string $collection, string $id, Document $documen if ($this->cache) { $this->cache->purge($id); - $this->cache->save($id, json_encode($document)); + $this->cache->save($id, json_encode($document->getArrayCopy())); } return $document; From 286a74b21901293a247cbbf3091a3e6891b4ca42 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 27 Apr 2021 15:26:31 -0400 Subject: [PATCH 11/18] Require cache as contructor param --- src/Database/Database.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 34a77b46f..8678ef06a 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -99,9 +99,9 @@ class Database /** * @param Adapter $adapter - * @param Cache $cache (optional) + * @param Cache $cache */ - public function __construct(Adapter $adapter, Cache $cache = null) + public function __construct(Adapter $adapter, Cache $cache) { $this->adapter = $adapter; $this->cache = $cache; @@ -571,10 +571,8 @@ public function updateDocument(string $collection, string $id, Document $documen $document = $this->adapter->updateDocument($collection->getId(), $document); $document = $this->decode($collection, $document); - if ($this->cache) { - $this->cache->purge($id); - $this->cache->save($id, json_encode($document->getArrayCopy())); - } + // $this->cache->purge($id); + // $this->cache->save($id, $document->getArrayCopy()); return $document; } @@ -597,9 +595,7 @@ public function deleteDocument(string $collection, string $id): bool throw new AuthorizationException($validator->getDescription()); } - if ($this->cache) { - $this->cache->purge($id); - } + // $this->cache->purge($id); return $this->adapter->deleteDocument($collection, $id); } From 3f30840fba75f09b6860c79bb830c7e01561ae57 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 27 Apr 2021 15:29:44 -0400 Subject: [PATCH 12/18] Ensure cache is empty before testing --- tests/Database/Adapter/MariaDBTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Database/Adapter/MariaDBTest.php b/tests/Database/Adapter/MariaDBTest.php index 85b7a791e..27dac6fb5 100644 --- a/tests/Database/Adapter/MariaDBTest.php +++ b/tests/Database/Adapter/MariaDBTest.php @@ -41,6 +41,7 @@ static function getDatabase(): Database $redis = new Redis(); $redis->connect('redis', 6379); + $redis->flushAll(); $cache = new Cache(new RedisAdapter($redis)); $database = new Database(new MariaDB($pdo), $cache); From cf18aa1ab2e3b5dadffd910233bb5c8b98504768 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 27 Apr 2021 15:38:06 -0400 Subject: [PATCH 13/18] Ensure tests pass without saving to cache --- src/Database/Database.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 8678ef06a..d71f87b2b 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -454,12 +454,12 @@ public function getDocument(string $collection, string $id): Document $document = null; $cache = null; - if ($this->cache) { - $cache = json_decode($this->cache->load($id, self::TTL), true); - $document = new Document($cache ?? []); + // TODO@kodumbeats Check if returned cache id matches request + if ($cache = $this->cache->load($id, self::TTL)) { + $document = new Document($cache); } - if ($document->isEmpty()) { + if (!$document) { $document = $this->adapter->getDocument($collection->getId(), $id); } @@ -475,9 +475,7 @@ public function getDocument(string $collection, string $id): Document return $document; } - if($cache) { - $this->cache->save($id, json_encode($document->getArrayCopy())); // save to cache after fetching from db - } + // $this->cache->save($id, $document->getArrayCopy()); // save to cache after fetching from db $document = $this->casting($collection, $document); $document = $this->decode($collection, $document); From 66c9f7069fb9b311587cde6c41632cb18e035f6a Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Tue, 27 Apr 2021 16:18:35 -0400 Subject: [PATCH 14/18] Debugging getArrayCopy method --- src/Database/Database.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Database/Database.php b/src/Database/Database.php index d71f87b2b..27568ca81 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -480,6 +480,8 @@ public function getDocument(string $collection, string $id): Document $document = $this->casting($collection, $document); $document = $this->decode($collection, $document); + // var_dump($document, new Document($document->getArrayCopy())); + return $document; } From 57be09e34939a1f48fe27f47203878db74f3f8b5 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Wed, 28 Apr 2021 12:38:07 -0400 Subject: [PATCH 15/18] Legacy code that considers arrays as nested documents --- src/Database/Document.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Database/Document.php b/src/Database/Document.php index efed817c7..69127fab7 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -32,19 +32,19 @@ public function __construct(array $input = []) throw new Exception('$write permission must be of type array'); } - foreach ($input as $key => &$value) { - if (\is_array($value)) { - if ((isset($value['$id']) || isset($value['$collection']))) { - $input[$key] = new self($value); - } else { - foreach ($value as $childKey => $child) { - if ((isset($child['$id']) || isset($child['$collection'])) && (!$child instanceof self)) { - $value[$childKey] = new self($child); - } - } - } - } - } + // foreach ($input as $key => &$value) { + // if (\is_array($value)) { + // if ((isset($value['$id']) || isset($value['$collection']))) { + // $input[$key] = new self($value); + // } else { + // foreach ($value as $childKey => $child) { + // if ((isset($child['$id']) || isset($child['$collection'])) && (!$child instanceof self)) { + // $value[$childKey] = new self($child); + // } + // } + // } + // } + // } parent::__construct($input); } From 53d521c9917e7cd4a91f3d6cf3103dacc5e54572 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Wed, 28 Apr 2021 12:45:26 -0400 Subject: [PATCH 16/18] Validate cache read access with authorization validator --- src/Database/Database.php | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 27568ca81..98fdcf268 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -457,12 +457,21 @@ public function getDocument(string $collection, string $id): Document // TODO@kodumbeats Check if returned cache id matches request if ($cache = $this->cache->load($id, self::TTL)) { $document = new Document($cache); - } + $validator = new Authorization($document, self::PERMISSION_READ); + + if (!$validator->isValid($document->getRead()) && $collection->getId() !== self::COLLECTIONS) { // Check if user has read access to this document + return new Document(); + } + + if($document->isEmpty()) { + return $document; + } - if (!$document) { - $document = $this->adapter->getDocument($collection->getId(), $id); + return $document; } + $document = $this->adapter->getDocument($collection->getId(), $id); + $document->setAttribute('$collection', $collection->getId()); $validator = new Authorization($document, self::PERMISSION_READ); @@ -475,12 +484,10 @@ public function getDocument(string $collection, string $id): Document return $document; } - // $this->cache->save($id, $document->getArrayCopy()); // save to cache after fetching from db - $document = $this->casting($collection, $document); $document = $this->decode($collection, $document); - // var_dump($document, new Document($document->getArrayCopy())); + $this->cache->save($id, $document->getArrayCopy()); // save to cache after fetching from db return $document; } @@ -571,8 +578,8 @@ public function updateDocument(string $collection, string $id, Document $documen $document = $this->adapter->updateDocument($collection->getId(), $document); $document = $this->decode($collection, $document); - // $this->cache->purge($id); - // $this->cache->save($id, $document->getArrayCopy()); + $this->cache->purge($id); + $this->cache->save($id, $document->getArrayCopy()); return $document; } @@ -595,7 +602,7 @@ public function deleteDocument(string $collection, string $id): bool throw new AuthorizationException($validator->getDescription()); } - // $this->cache->purge($id); + $this->cache->purge($id); return $this->adapter->deleteDocument($collection, $id); } From f2cdd98bc2da7658f83e03bd413dde4e6229793c Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Wed, 28 Apr 2021 13:21:03 -0400 Subject: [PATCH 17/18] Add cache to readme example --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2b3a91064..71486a7ce 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,8 @@ Some examples to help you get started. use PDO; use Utopia\Database\Database; use Utopia\Database\Adapter\MariaDB; +use Utopia\Cache\Cache; +use Utopia\Cache\Adapter\None as NoCache; $dbHost = 'mariadb'; $dbPort = '3306'; @@ -71,7 +73,9 @@ $pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ]); -$database = new Database(new MariaDB($pdo)); +$cache = new Cache(new NoCache()); + +$database = new Database(new MariaDB($pdo), $cache); $database->setNamespace('myscope'); $database->create(); // Creates a new schema named `myscope` From 2350e5c3312e848114efea3bd319e5e43aaabfc9 Mon Sep 17 00:00:00 2001 From: kodumbeats Date: Thu, 29 Apr 2021 11:02:33 -0400 Subject: [PATCH 18/18] Cache updated documentId on updateDocument --- src/Database/Database.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 98fdcf268..ad0d43bfc 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -579,7 +579,7 @@ public function updateDocument(string $collection, string $id, Document $documen $document = $this->decode($collection, $document); $this->cache->purge($id); - $this->cache->save($id, $document->getArrayCopy()); + $this->cache->save($document->getId(), $document->getArrayCopy()); return $document; }