diff --git a/services/nginx/app/objects/accounts_o.php b/services/nginx/app/objects/accounts_o.php index 8d8e482b..9771a074 100644 --- a/services/nginx/app/objects/accounts_o.php +++ b/services/nginx/app/objects/accounts_o.php @@ -100,27 +100,44 @@ class accounts_o extends db $this->deleted_at = new object_property($this->table, $this->id, 'deleted_at', 'datetime', false); } + /** + * Invalidate the cached object when it is changed + * @throws Exception If no object is selected + */ public function objectChanged(): void { - //TODO: Add cache invalidation + self::requireSelected(); + // Invalidate the cache + self::deleteCached(self::$asArrayCacheKey); } /** * Get the object as an array * @return array The object as an array + * @throws Exception If no object is selected */ public function asArray(): array { - return [ - 'id' => $this->id, - 'phone' => $this->phone->value(), - 'country_code' => $this->country_code->value(), - 'name' => $this->name->value(), - 'profile_picture' => $this->profile_picture->value(), + self::requireSelected(); + $cached = self::getCached(self::$asArrayCacheKey); + // If the object is cached, return it + if (!empty($cached)) return $cached; + // Convert the object to an array + $array = [ + 'id' => (int)$this->id, + 'phone' => (int)$this->phone->value(), + 'country_code' => (int)$this->country_code->value(), + 'name' => (string)$this->name->value(), + 'profile_picture' => (int)$this->profile_picture->value(), 'seen_at' => $this->seen_at->value(), 'created_at' => $this->created_at->value(), 'updated_at' => $this->updated_at->value(), //'deleted_at' => $this->deleted_at->value(), ]; + // Cache the object + self::cache(self::$asArrayCacheKey, $array); + self::setCachedExpiration(self::$asArrayCacheKey, self::$asArrayCacheExpiration); + // Return the object as an array + return $array; } } \ No newline at end of file