Files
api/classes/redis.php
T
Jepp9350 b794e95ba9 Refactor and enhance booking, customer, and caching logic
Improved booking handling with unfulfilled booking checks and Slack notifications. Enhanced customer data retrieval with caching optimizations and discount calculations. Added logging, debug capabilities, and expanded Redis functionalities for better data management.
2025-01-21 21:16:42 +01:00

316 lines
8.1 KiB
PHP

<?php
namespace classes;
use interfaces\redis_i;
class redis implements redis_i
{
use \traits\redis_t;
public function __construct()
{
global $REDIS_CONFIG;
// Make sure the Redis configuration is set
if (!isset($REDIS_CONFIG)) {
throw new \Exception('Redis configuration is not set');
}
// Apply the Redis configuration
$this->redis_host = $REDIS_CONFIG['host'];
$this->redis_database = $REDIS_CONFIG['database'];
$this->redis_password = $REDIS_CONFIG['password'];
}
/**
* @inheritDoc
*/
public function cache_department_booking_count(int $department_id, int $count): redis_i
{
// Cache the department booking count
$this->set('department_booking_count_' . $department_id, $count);
return $this;
}
/**
* @inheritDoc
*/
public function get_department_booking_count(int $department_id): int|null
{
// Get the department booking count
return $this->get('department_booking_count_' . $department_id);
}
/**
* @inheritDoc
*/
public function clear_department_booking_count(int $department_id): redis_i
{
// Clear the department booking count
$this->delete('department_booking_count_' . $department_id);
return $this;
}
/**
* @inheritDoc
*/
public function cache_economic_customer_name(int $customer_number, string $customer_name): self
{
// If the customer name is empty, or null, set it to 'IS_EMPTY_OR_NULL'
if (empty($customer_name)) {
$customer_name = 'IS_EMPTY_OR_NULL';
}
// Cache the economic customer name
$this->set('economic_customer_name_' . $customer_number, $customer_name);
return $this;
}
/**
* @inheritDoc
*/
public function get_economic_customer_name(int $customer_number): string|null
{
// Get the economic customer name
return $this->get('economic_customer_name_' . $customer_number);
}
/**
* @inheritDoc
*/
public function clear_economic_customer_name(int $customer_number): self
{
// Clear the economic customer name
$this->delete('economic_customer_name_' . $customer_number);
return $this;
}
/**
* @inheritDoc
*/
public function cache_department_webhook(int $department_id, string $webhook): self
{
// Cache the department webhook
$this->set('department_webhook_' . $department_id, $webhook);
return $this;
}
/**
* @inheritDoc
*/
public function get_department_webhook(int $department_id): string|null
{
// Get the department webhook
return $this->get('department_webhook_' . $department_id);
}
/**
* @inheritDoc
*/
public function clear_department_webhook(int $department_id): self
{
// Clear the department webhook
$this->delete('department_webhook_' . $department_id);
return $this;
}
/**
* @inheritDoc
*/
public function cache_department_name(int $department_id, string $department_name): self
{
// Cache the department name (If it is not empty or null)
if (!empty($department_name)) {
$department_name = 'IS_EMPTY_OR_NULL';
}
$this->set('department_name_' . $department_id, $department_name);
return $this;
}
/**
* @inheritDoc
*/
public function get_department_name(int $department_id): string|null
{
// Get the department name
return $this->get('department_name_' . $department_id);
}
/**
* @inheritDoc
*/
public function clear_department_name(int $department_id): self
{
// Clear the department name
$this->delete('department_name_' . $department_id);
return $this;
}
/**
* @inheritDoc
*/
public function cache_economic_customer_discount_percentage(int $customer_number, int $discount_percentage): self
{
// Cache the economic customer discount percentage
$this->set('economic_customer_discount_percentage_' . $customer_number, $discount_percentage);
return $this;
}
/**
* @inheritDoc
*/
public function get_economic_customer_discount_percentage(int $customer_number): int|null
{
// Get the economic customer discount percentage
return $this->get('economic_customer_discount_percentage_' . $customer_number);
}
/**
* @inheritDoc
*/
public function clear_economic_customer_discount_percentage(int $customer_number): self
{
// Clear the economic customer discount percentage
$this->delete('economic_customer_discount_percentage_' . $customer_number);
return $this;
}
/**
* @inheritDoc
*/
public function cache_customer_notes(int $customer_id, array $customer_notes): self
{
// Cache the customer notes
$this->set_array('customer_notes_' . $customer_id, $customer_notes);
return $this;
}
/**
* @inheritDoc
*/
public function get_customer_notes(int $customer_id): array|null
{
// Get the customer notes
return $this->get_array('customer_notes_' . $customer_id);
}
/**
* @inheritDoc
*/
public function clear_customer_notes(int $customer_id): self
{
// Clear the customer notes
$this->delete('customer_notes_' . $customer_id);
return $this;
}
/**
* @inheritDoc
*/
public function add_log(string $module, string $department, int $type, int $user_id, string $action, string $message): redis_i
{
// Get the current timestamp
$timestamp = strtotime('now');
// Get unique id for the log
$log_id = uniqid();
// Add the log to the cache
$this->set_array('log_' . $log_id, [
'module' => $module,
'department' => $department,
'type' => $type,
'user_id' => $user_id,
'action' => $action,
'message' => $message,
'timestamp' => $timestamp
]);
return $this;
}
/**
* @inheritDoc
*/
public function get_logs(): array|null
{
// Get all logs from the cache
return $this->get_arrays('log_*');
}
/**
* @inheritDoc
*/
public function clear_logs(): self
{
// Clear all logs from the cache
$keys = $this->get_keys('log_*');
foreach ( $keys as $key ) {
$this->delete($key);
}
return $this;
}
/**
* @inheritDoc
*/
public function get_department(int $department_id): array|null
{
// Get the department
return $this->get_array('department_' . $department_id);
}
/**
* @inheritDoc
*/
public function clear_department(int $department_id): self
{
// Clear the departments
$this->clear_departments();
return $this;
}
/**
* @inheritDoc
*/
public function clear_departments(): self
{
// Clear all departments from the cache
$this->delete('departments');
$keys = $this->get_keys('department_*');
foreach ( $keys as $key ) {
$this->delete($key);
}
return $this;
}
/**
* @inheritDoc
*/
public function get_departments(): array|null
{
// Get all departments from the cache
return $this->get_array('departments');
}
/**
* @inheritDoc
*/
public function cache_departments(array $departments): self
{
// Cache the departments
foreach ( $departments as $department ) {
$this->cache_department($department['id'], $department);
}
redis->set_array('departments', $departments);
return $this;
}
/**
* @inheritDoc
*/
public function cache_department(int $department_id, array $department): self
{
// Cache the department
$this->set_array('department_' . $department_id, $department);
return $this;
}
}