From 1c0ff3cdbf2b0b1d96f1ef05dd1e337e210c8ca0 Mon Sep 17 00:00:00 2001 From: ds2600 Date: Sun, 7 Jan 2024 05:58:29 +0000 Subject: [PATCH 1/2] Added Redis caching for DB queries --- .gitignore | 1 + README.md | 4 +- classes/Database.php | 78 ++++++++++++++++++++++++++++----------- classes/SearchHandler.php | 5 --- config/config.example.php | 2 + public/api/search.php | 1 - public/css/index.html | 0 public/images/index.html | 0 public/js/index.html | 0 9 files changed, 61 insertions(+), 30 deletions(-) create mode 100644 public/css/index.html create mode 100644 public/images/index.html create mode 100644 public/js/index.html diff --git a/.gitignore b/.gitignore index 043dcd2..bb594da 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ config/config.php /vendor/ .env .env.local +public/trigger.php \ No newline at end of file diff --git a/README.md b/README.md index 10b39fa..cb111cc 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,8 @@ This project is licensed under the MIT License - see the LICENSE file for detail 1. Install Apache, PHP and Extensions ```bash sudo apt update -sudo apt install apache2 -y -sudo apt install php libapache2-mod-php php-mysql composer -y +sudo apt install apache2 redis-server -y +sudo apt install php libapache2-mod-php php-mysql composer php-redis -y ``` 2. Clone the repository ```bash diff --git a/classes/Database.php b/classes/Database.php index b52fda8..485463d 100644 --- a/classes/Database.php +++ b/classes/Database.php @@ -7,18 +7,32 @@ namespace ds2600\ARWT; use PDO; +use Redis; class Database { private $conn = null; private $config = null; + private $cache = null; public function __construct($config) { $this->config = $config; + $this->initializeCache(); + } + + private function initializeCache() { + if ($this->config['redis_cache']) { + $this->cache = new Redis(); + $this->cache->connect('127.0.0.1'); + } } public function connect() { + if ($this->conn !== null) { + return $this->conn; + } + $db_host = $_ENV['DB_HOST']; $db_user = $_ENV['DB_USER']; $db_pass = $_ENV['DB_PASS']; @@ -39,34 +53,54 @@ public function connect() { return $this->conn; } - public function select($query) { - $stmt = $this->conn->prepare($query); - $stmt->execute(); + public function searchCallSign($callSign) { - return $stmt; + if ($this->config['redis_cache']) { + $cacheKey = "searchCallSign_" . md5($callSign); + $cachedResult = $this->cache->get($cacheKey); + + if ($cachedResult) { + if ($this->config['debug']) { + error_log("Cache hit for ". $cacheKey,0); + } + return json_decode($cachedResult, true); + } else { + if ($this->config['debug']) { + error_log("Cache miss for ". $cacheKey,0); + } + $result = $this->performDatabaseQuery($callSign); + // Store the result in cache with a 48-hour expiration + $this->cache->setex($cacheKey, 48 * 60 * 60, json_encode($result)); + } + } else { + $result = $this->performDatabaseQuery($callSign); + } + + return $result; } - public function searchCallSign($callSign) { + private function performDatabaseQuery($callSign) { + $this->connect(); + $query = "SELECT - EN.call_sign, - EN.entity_name, - EN.street_address, - EN.city, - EN.state, - EN.zip_code, - AM.operator_class, - AM.previous_callsign, - SF.lic_freeform_condition - FROM - PUBACC_EN AS EN - LEFT JOIN PUBACC_AM AS AM ON EN.unique_system_identifier = AM.unique_system_identifier - LEFT JOIN PUBACC_SF AS SF ON EN.unique_system_identifier = SF.unique_system_identifier - WHERE - EN.call_sign LIKE :callSign"; - + EN.call_sign, + EN.entity_name, + EN.street_address, + EN.city, + EN.state, + EN.zip_code, + AM.operator_class, + AM.previous_callsign, + SF.lic_freeform_condition + FROM + PUBACC_EN AS EN + LEFT JOIN PUBACC_AM AS AM ON EN.unique_system_identifier = AM.unique_system_identifier + LEFT JOIN PUBACC_SF AS SF ON EN.unique_system_identifier = SF.unique_system_identifier + WHERE + EN.call_sign LIKE :callSign"; + $stmt = $this->conn->prepare($query); $stmt->execute(['callSign' => "%$callSign%"]); - return $stmt->fetchAll(PDO::FETCH_ASSOC); } diff --git a/classes/SearchHandler.php b/classes/SearchHandler.php index 239f5cd..2f50771 100644 --- a/classes/SearchHandler.php +++ b/classes/SearchHandler.php @@ -9,11 +9,6 @@ class SearchHandler { public function __construct($config) { $this->config = $config; $this->db = new Database($config); - $this->db->connect(); - } - - public function connectToDatabase() { - $this->db->connect(); } public function performSearch($callSign) { diff --git a/config/config.example.php b/config/config.example.php index 8c474d6..5b157c4 100644 --- a/config/config.example.php +++ b/config/config.example.php @@ -8,6 +8,8 @@ 'uls_search' => true, // Limit the number of ULS searches per hour 'uls_search_limit' => 5, + // Enable Redis Cache + 'redis_cache' => true, // Enable or disable debug mode 'debug' => false, ]; diff --git a/public/api/search.php b/public/api/search.php index c102808..5b13b6a 100644 --- a/public/api/search.php +++ b/public/api/search.php @@ -17,7 +17,6 @@ // Load configuration $config = require __DIR__ . '/../../config/config.php'; $searchHandler = new SearchHandler($config); -$searchHandler->connectToDatabase(); $callSign = isset($_GET['call-sign']) ? $_GET['call-sign'] : ''; $results = $searchHandler->performSearch($callSign); diff --git a/public/css/index.html b/public/css/index.html new file mode 100644 index 0000000..e69de29 diff --git a/public/images/index.html b/public/images/index.html new file mode 100644 index 0000000..e69de29 diff --git a/public/js/index.html b/public/js/index.html new file mode 100644 index 0000000..e69de29 From 649174ad6aa998888a274ea8a36326881fa27198 Mon Sep 17 00:00:00 2001 From: ds2600 Date: Sun, 7 Jan 2024 06:01:30 +0000 Subject: [PATCH 2/2] Bumped version --- common/version.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/version.php b/common/version.php index b56a5a7..0532e98 100644 --- a/common/version.php +++ b/common/version.php @@ -1,2 +1,2 @@