From 6ee8c0e2b1b226d6636b739cf9de872213c81e80 Mon Sep 17 00:00:00 2001 From: Jepp9350 <2jepp9350@gmail.com> Date: Wed, 18 Jun 2025 15:20:59 +0200 Subject: [PATCH] Add vehicle caching methods to XLVask: interface and implementation updates for handling vehicle cache. --- .../modules/xlvask/helpers/xlvask_cache.php | 86 +++++++++++++++++++ .../xlvask/interfaces/xlvask_cache_i.php | 43 ++++++++++ 2 files changed, 129 insertions(+) diff --git a/services/nginx/app/modules/xlvask/helpers/xlvask_cache.php b/services/nginx/app/modules/xlvask/helpers/xlvask_cache.php index 972813e0..18c4120d 100644 --- a/services/nginx/app/modules/xlvask/helpers/xlvask_cache.php +++ b/services/nginx/app/modules/xlvask/helpers/xlvask_cache.php @@ -132,4 +132,90 @@ class xlvask_cache implements xlvask_cache_i } 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; + } } \ No newline at end of file diff --git a/services/nginx/app/modules/xlvask/interfaces/xlvask_cache_i.php b/services/nginx/app/modules/xlvask/interfaces/xlvask_cache_i.php index bee7f33c..65a1bf31 100644 --- a/services/nginx/app/modules/xlvask/interfaces/xlvask_cache_i.php +++ b/services/nginx/app/modules/xlvask/interfaces/xlvask_cache_i.php @@ -4,6 +4,7 @@ namespace xlvask\interfaces; use Exception; use helpers\xlvask_customer; +use helpers\xlvask_vehicle; interface xlvask_cache_i { @@ -63,4 +64,46 @@ interface xlvask_cache_i * @example [xlvask_customer, xlvask_customer, ...] */ 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 An array of all cached vehicle objects. + * @example [xlvask_vehicle, xlvask_vehicle, ...] + * @see xlvask_vehicle + */ + public function getAllCachedVehicles(): array; } \ No newline at end of file