Add vehicle caching methods to XLVask: interface and implementation updates for handling vehicle cache.

This commit is contained in:
Jepp9350
2025-06-18 15:20:59 +02:00
parent 9bcf52a046
commit 6ee8c0e2b1
2 changed files with 129 additions and 0 deletions
@@ -132,4 +132,90 @@ class xlvask_cache implements xlvask_cache_i
} }
throw new Exception("Customer with number $customer_number is not cached."); throw new Exception("Customer with number $customer_number is not cached.");
} }
/**
* @inheritDoc
*/
public function getVehicleCache(string $registration): xlvask_vehicle
{
if (self::isVehicleCached($registration)) {
$vehicle_data = (object)json_decode(redis->get('xlvask_vehicle_' . $registration), true);
$tmp = new xlvask_vehicle();
// Assign the properties shared by the xlvask_vehicle class.
foreach ( $vehicle_data as $key => $value ) {
if (property_exists($tmp, $key)) {
$tmp->{$key} = $value;
}
}
return $tmp; // Return the xlvask_vehicle object
}
throw new Exception("Vehicle with registration $registration is not cached.");
}
/**
* @inheritDoc
*/
public function isVehicleCached(string $registration): bool
{
return (redis->exists('xlvask_vehicle_' . $registration));
}
/**
* @inheritDoc
* @throws Exception If there is an error encoding the vehicle data to JSON.
* @see xlvask_vehicle
*/
public function setVehicleCache(string $registration, xlvask_vehicle $vehicle): void
{
$vehicle_data = json_encode($vehicle, JSON_THROW_ON_ERROR);
redis->set('xlvask_vehicle_' . $registration, $vehicle_data);
redis->expire('xlvask_vehicle_' . $registration, 3600); // Set cache expiration to 1 hour
}
/**
* @inheritDoc
*/
public function clearVehicleCache(string $registration): void
{
if (self::isVehicleCached($registration)) {
redis->delete('xlvask_vehicle_' . $registration);
} else {
throw new Exception("Vehicle with registration $registration is not cached.");
}
}
/**
* @inheritDoc
*/
public function clearAllVehicleCache(): void
{
$keys = redis->get_keys('xlvask_vehicle_*');
redis->clear_keys($keys);
}
/**
* @inheritDoc
*/
public function getAllCachedVehicles(): array
{
$keys = redis->get_keys('xlvask_vehicle_*');
$vehicles = [];
foreach ( $keys as $key ) {
$vehicle_data = redis->get($key);
if ($vehicle_data) {
$vehicle_data = json_decode($vehicle_data, true);
$tmp = new xlvask_vehicle();
// Assign the properties shared by the xlvask_vehicle class.
foreach ( $vehicle_data as $property => $value ) {
if (property_exists($tmp, $property)) {
$tmp->{$property} = $value;
}
}
$vehicles[] = $tmp; // Add the xlvask_vehicle object to the array
} else {
throw new Exception("Failed to retrieve vehicle data for key: $key");
}
}
return $vehicles;
}
} }
@@ -4,6 +4,7 @@ namespace xlvask\interfaces;
use Exception; use Exception;
use helpers\xlvask_customer; use helpers\xlvask_customer;
use helpers\xlvask_vehicle;
interface xlvask_cache_i interface xlvask_cache_i
{ {
@@ -63,4 +64,46 @@ interface xlvask_cache_i
* @example [xlvask_customer, xlvask_customer, ...] * @example [xlvask_customer, xlvask_customer, ...]
*/ */
public function getCachedCustomersByCustomerNumbers(array $customer_numbers, bool $allow_missing = false): array; public function getCachedCustomersByCustomerNumbers(array $customer_numbers, bool $allow_missing = false): array;
/**
* Check if a vehicle is cached by its registration number.
* @param string $registration The vehicle registration number to check.
* @return bool True if the vehicle is cached, false otherwise.
*/
public function isVehicleCached(string $registration): bool;
/**
* Get the cached vehicle object by its registration number.
* @param string $registration The vehicle registration number to retrieve.
* @return xlvask_vehicle The cached vehicle object.
* @throws Exception If the vehicle is not cached.
*/
public function getVehicleCache(string $registration): xlvask_vehicle;
/**
* Set the cache for a specific vehicle.
* @param string $registration The vehicle registration number to cache.
* @param xlvask_vehicle $vehicle The vehicle object to cache.
*/
public function setVehicleCache(string $registration, xlvask_vehicle $vehicle): void;
/**
* Clear the cache for a specific vehicle.
* @param string $registration The vehicle registration number to clear the cache for.
*/
public function clearVehicleCache(string $registration): void;
/**
* Clear all cached vehicles.
* This will remove all vehicle caches, so use with caution.
*/
public function clearAllVehicleCache(): void;
/**
* Get all cached vehicles.
* @return array<xlvask_vehicle> An array of all cached vehicle objects.
* @example [xlvask_vehicle, xlvask_vehicle, ...]
* @see xlvask_vehicle
*/
public function getAllCachedVehicles(): array;
} }