Add caching logic to accounts_o and enhance asArray method
- Implemented cache invalidation in `objectChanged` method. - Enhanced `asArray` method with caching support and type casting. - Added cache expiration and retrieval logic to improve performance.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user