116 lines
3.1 KiB
PHP
116 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace helpers;
|
|
|
|
require_once WD . '/modules/xlvask/interfaces/xlvask_cache_i.php';
|
|
|
|
use Exception;
|
|
use xlvask\interfaces\xlvask_cache_i;
|
|
|
|
class xlvask_cache implements xlvask_cache_i
|
|
{
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function setCustomerCache(int $customer_number, xlvask_customer $customer): void
|
|
{
|
|
$customer_data = json_encode($customer);
|
|
redis->set(self::getCustomerCacheKey($customer_number), $customer_data);
|
|
redis->expire(self::getCustomerCacheKey($customer_number), 3600); // Set cache expiration to 1 hour
|
|
}
|
|
|
|
protected function getCustomerCacheKey(int $customer_number): string
|
|
{
|
|
return 'xlvask_customer_' . $customer_number;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
* @throws Exception
|
|
*/
|
|
public function clearCustomerCache(int $customer_number): void
|
|
{
|
|
if (self::isCustomerCached($customer_number)) {
|
|
redis->delete(self::getCustomerCacheKey($customer_number));
|
|
} else {
|
|
throw new Exception("Customer with number $customer_number is not cached.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function isCustomerCached(int $customer_number): bool
|
|
{
|
|
return (redis->exists(self::getCustomerCacheKey($customer_number)));
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function clearAllCustomerCache(): void
|
|
{
|
|
$keys = redis->get_keys('xlvask_customer_*');
|
|
redis->clear_keys($keys);
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getAllCachedCustomers(): array
|
|
{
|
|
$keys = redis->get_keys('xlvask_customer_*');
|
|
$customers = [];
|
|
foreach ( $keys as $key ) {
|
|
$customer_data = redis->get($key);
|
|
if ($customer_data) {
|
|
$customers[] = json_decode($customer_data);
|
|
}
|
|
}
|
|
return $customers;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getCachedCustomerCount(): int
|
|
{
|
|
$keys = redis->get_keys('xlvask_customer_*');
|
|
return count($keys);
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
* @throws Exception
|
|
*/
|
|
public function getCachedCustomersByCustomerNumbers(array $customer_numbers, bool $allow_missing = false): array
|
|
{
|
|
$result = [];
|
|
foreach ( $customer_numbers as $customer_number ) {
|
|
if (self::isCustomerCached($customer_number)) {
|
|
$result[$customer_number] = $this->getCustomerCache($customer_number);
|
|
} elseif ($allow_missing) {
|
|
$result[$customer_number] = null; // or handle as needed
|
|
} else {
|
|
throw new Exception("Customer with number $customer_number is not cached.");
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getCustomerCache(int $customer_number): xlvask_customer
|
|
{
|
|
if (self::isCustomerCached($customer_number)) {
|
|
$customer_data = redis->get(self::getCustomerCacheKey($customer_number));
|
|
return json_decode($customer_data);
|
|
}
|
|
throw new Exception("Customer with number $customer_number is not cached.");
|
|
}
|
|
} |