Enhance customer_vehicles_o: add asArray caching with expiration, implement cache invalidation in object changes, and improve vehicle update flow with cache management.

This commit is contained in:
Jepp9350
2025-06-26 12:33:38 +02:00
parent 5b88ef9dc3
commit 1bcd15fa2f
@@ -43,6 +43,11 @@ class customer_vehicles_o extends db
public function asArray(): array
{
self::requireSelected();
// Check if the vehicle is cached
$cached = $this->getCached('asArray', $this->id);
if ($cached) {
return (array)$cached;
}
$wash_subscription = (bool)$this->wash_subscription->value();
$addons = self::transformObjectsToArray(
self::getAddons()
@@ -53,7 +58,7 @@ class customer_vehicles_o extends db
$last_order_id = self::getLastOrderId();
$customer_number = (int)$this->customer_id->value();
$xlvask = $this->hasXLVask() ? $this->getXLVask() : null;
return [
$tmp = [
'id' => (int)$this->id,
'user_id' => (int)(new users_o())->getUserIdFromEconomic((int)$customer_number),
'customer_id' => (int)$this->customer_id->value(),
@@ -74,6 +79,10 @@ class customer_vehicles_o extends db
return $vehicle_type->toArray();
}, $this->getVehicleTypes()),
];
// Cache the result
$this->cache('asArray', $tmp, $this->id);
$this->setCachedExpiration('asArray', self::$asArrayCacheExpiration, $this->id);
return $tmp;
}
/**
@@ -237,7 +246,8 @@ class customer_vehicles_o extends db
public function objectChanged(): void
{
// Since the customer_vehicles object is not cached, there is no need to invalidate the cache
// Clear the cache for the object
$this->deleteCached('asArray', $this->id);
}
public function structure(): void
@@ -245,6 +255,11 @@ class customer_vehicles_o extends db
$this->setTable('customer_vehicles');
}
public function delete()
{
}
public function getObjectProperties(): void
{
$this->customer_id = new object_property($this->table, $this->id, 'customer_id', 'int', true);
@@ -345,6 +360,7 @@ class customer_vehicles_o extends db
$xlvask->updateVehicle($xlvask_vehicle);
// Cache the updated vehicle
$xlvask->getCache()->setVehicleCache($xlvask_vehicle->registrationNumber, $xlvask_vehicle);
$this->objectChanged();
}
/**
@@ -393,6 +409,7 @@ class customer_vehicles_o extends db
/** @var xlvask_vehicles $vehicles */
$xlvask_vehicle = $vehicles::getVehicleByRegistrationNumber((string)$this->reg->value());
$xlvask->getCache()->setVehicleCache($xlvask_vehicle->registrationNumber, $xlvask_vehicle);
$this->objectChanged();
return $xlvask_vehicle;
}
@@ -429,5 +446,6 @@ class customer_vehicles_o extends db
$xlvask->updateVehicle($xlvask_vehicle);
// Cache the updated vehicle
$xlvask->getCache()->setVehicleCache($xlvask_vehicle->registrationNumber, $xlvask_vehicle);
$this->objectChanged();
}
}