44
55use Exception ;
66use Utopia \Cache \Cache ;
7+ use Utopia \CLI \Console ;
78use Utopia \Database \Exception as DatabaseException ;
89use Utopia \Database \Exception \Authorization as AuthorizationException ;
910use Utopia \Database \Exception \Conflict as ConflictException ;
@@ -441,14 +442,20 @@ function (?string $value) {
441442
442443 /**
443444 * Add listener to events
445+ * Passing a null $callback will remove the listener
444446 *
445447 * @param string $event
446448 * @param string $name
447- * @param callable $callback
449+ * @param ? callable $callback
448450 * @return static
449451 */
450- public function on (string $ event , string $ name , callable $ callback ): static
452+ public function on (string $ event , string $ name , ? callable $ callback ): static
451453 {
454+ if (empty ($ callback )) {
455+ unset($ this ->listeners [$ event ][$ name ]);
456+ return $ this ;
457+ }
458+
452459 if (!isset ($ this ->listeners [$ event ])) {
453460 $ this ->listeners [$ event ] = [];
454461 }
@@ -2992,8 +2999,15 @@ public function getDocument(string $collection, string $id, array $queries = [],
29922999 $ documentCacheHash .= ': ' . \md5 (\implode ($ selections ));
29933000 }
29943001
2995- if ($ cache = $ this ->cache ->load ($ documentCacheKey , self ::TTL , $ documentCacheHash )) {
2996- $ document = new Document ($ cache );
3002+ try {
3003+ $ cached = $ this ->cache ->load ($ documentCacheKey , self ::TTL , $ documentCacheHash );
3004+ } catch (Exception $ e ) {
3005+ Console::warning ('Warning: Failed to get document from cache: ' . $ e ->getMessage ());
3006+ $ cached = null ;
3007+ }
3008+
3009+ if ($ cached ) {
3010+ $ document = new Document ($ cached );
29973011
29983012 if ($ collection ->getId () !== self ::METADATA ) {
29993013 if (!$ validator ->isValid ([
@@ -3042,8 +3056,12 @@ public function getDocument(string $collection, string $id, array $queries = [],
30423056
30433057 // Don't save to cache if it's part of a relationship
30443058 if (empty ($ relationships )) {
3045- $ this ->cache ->save ($ documentCacheKey , $ document ->getArrayCopy (), $ documentCacheHash );
3046- $ this ->cache ->save ($ collectionCacheKey , 'empty ' , $ documentCacheKey );
3059+ try {
3060+ $ this ->cache ->save ($ documentCacheKey , $ document ->getArrayCopy (), $ documentCacheHash );
3061+ $ this ->cache ->save ($ collectionCacheKey , 'empty ' , $ documentCacheKey );
3062+ } catch (Exception $ e ) {
3063+ Console::warning ('Failed to save document to cache: ' . $ e ->getMessage ());
3064+ }
30473065 }
30483066
30493067 // Remove internal attributes if not queried for select query
@@ -3962,6 +3980,7 @@ public function updateDocument(string $collection, string $id, Document $documen
39623980 }
39633981
39643982 $ this ->adapter ->updateDocument ($ collection ->getId (), $ id , $ document );
3983+ $ this ->purgeCachedDocument ($ collection ->getId (), $ id );
39653984
39663985 return $ document ;
39673986 });
@@ -3972,7 +3991,6 @@ public function updateDocument(string $collection, string $id, Document $documen
39723991
39733992 $ document = $ this ->decode ($ collection , $ document );
39743993
3975- $ this ->purgeCachedDocument ($ collection ->getId (), $ id );
39763994 $ this ->trigger (self ::EVENT_DOCUMENT_UPDATE , $ document );
39773995
39783996 return $ document ;
@@ -4888,10 +4906,12 @@ public function deleteDocument(string $collection, string $id): bool
48884906 $ document = $ this ->silent (fn () => $ this ->deleteDocumentRelationships ($ collection , $ document ));
48894907 }
48904908
4891- return $ this ->adapter ->deleteDocument ($ collection ->getId (), $ id );
4892- });
4909+ $ result = $ this ->adapter ->deleteDocument ($ collection ->getId (), $ id );
48934910
4894- $ this ->purgeCachedDocument ($ collection ->getId (), $ id );
4911+ $ this ->purgeCachedDocument ($ collection ->getId (), $ id );
4912+
4913+ return $ result ;
4914+ });
48954915
48964916 $ this ->trigger (self ::EVENT_DOCUMENT_DELETE , $ document );
48974917
@@ -5424,6 +5444,7 @@ public function deleteDocuments(string $collection, array $queries = [], int $ba
54245444 public function purgeCachedCollection (string $ collectionId ): bool
54255445 {
54265446 $ collectionKey = $ this ->cacheName . '-cache- ' . $ this ->getNamespace () . ': ' . $ this ->adapter ->getTenant () . ':collection: ' . $ collectionId ;
5447+
54275448 $ documentKeys = $ this ->cache ->list ($ collectionKey );
54285449 foreach ($ documentKeys as $ documentKey ) {
54295450 $ this ->cache ->purge ($ documentKey );
0 commit comments