Skip to content

Commit

Permalink
Merge pull request #4 from ds2600/dev
Browse files Browse the repository at this point in the history
Added Redis Caching Capability
  • Loading branch information
ds2600 authored Jan 7, 2024
2 parents c13de47 + 649174a commit 657116c
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ config/config.php
/vendor/
.env
.env.local
public/trigger.php
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
78 changes: 56 additions & 22 deletions classes/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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);
}

Expand Down
5 changes: 0 additions & 5 deletions classes/SearchHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion common/version.php
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<?php
define('VERSION', '0.2.1');
define('VERSION', '0.3.0');
2 changes: 2 additions & 0 deletions config/config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
Expand Down
1 change: 0 additions & 1 deletion public/api/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Empty file added public/css/index.html
Empty file.
Empty file added public/images/index.html
Empty file.
Empty file added public/js/index.html
Empty file.

0 comments on commit 657116c

Please sign in to comment.