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);
|
$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
|
public function objectChanged(): void
|
||||||
{
|
{
|
||||||
//TODO: Add cache invalidation
|
self::requireSelected();
|
||||||
|
// Invalidate the cache
|
||||||
|
self::deleteCached(self::$asArrayCacheKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the object as an array
|
* Get the object as an array
|
||||||
* @return array The object as an array
|
* @return array The object as an array
|
||||||
|
* @throws Exception If no object is selected
|
||||||
*/
|
*/
|
||||||
public function asArray(): array
|
public function asArray(): array
|
||||||
{
|
{
|
||||||
return [
|
self::requireSelected();
|
||||||
'id' => $this->id,
|
$cached = self::getCached(self::$asArrayCacheKey);
|
||||||
'phone' => $this->phone->value(),
|
// If the object is cached, return it
|
||||||
'country_code' => $this->country_code->value(),
|
if (!empty($cached)) return $cached;
|
||||||
'name' => $this->name->value(),
|
// Convert the object to an array
|
||||||
'profile_picture' => $this->profile_picture->value(),
|
$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(),
|
'seen_at' => $this->seen_at->value(),
|
||||||
'created_at' => $this->created_at->value(),
|
'created_at' => $this->created_at->value(),
|
||||||
'updated_at' => $this->updated_at->value(),
|
'updated_at' => $this->updated_at->value(),
|
||||||
//'deleted_at' => $this->deleted_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