Porządki
This commit is contained in:
40
api/get_table_name.php
Normal file
40
api/get_table_name.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
function getTableNameByHash($conn, $hash) {
|
||||
if (empty($hash)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$cacheFile = __DIR__ . '/cache/tables_cache.json';
|
||||
$cacheLifetime = 24 * 3600; // 24 godziny
|
||||
|
||||
$cacheData = [];
|
||||
$cacheValid = false;
|
||||
|
||||
if (file_exists($cacheFile)) {
|
||||
if (time() - filemtime($cacheFile) < $cacheLifetime) {
|
||||
$json = file_get_contents($cacheFile);
|
||||
$cacheData = json_decode($json, true);
|
||||
if (is_array($cacheData)) {
|
||||
$cacheValid = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$cacheValid) {
|
||||
$cacheData = [];
|
||||
$tsql = "SELECT ID, Nazwa FROM dbo.NGastroStolik";
|
||||
$stmt = sqlsrv_query($conn, $tsql);
|
||||
|
||||
if ($stmt !== false) {
|
||||
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
|
||||
$id = strtoupper($row['ID']);
|
||||
$nazwa = trim($row['Nazwa']);
|
||||
$cacheData[$id] = $nazwa;
|
||||
}
|
||||
file_put_contents($cacheFile, json_encode($cacheData, JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
|
||||
$hashUpper = strtoupper($hash);
|
||||
return $cacheData[$hashUpper] ?? '';
|
||||
}
|
||||
Reference in New Issue
Block a user