Skip to content
Permalink
Browse files
Merge pull request #723 from tarasfrompir/patch-2
Update caching.class.php
  • Loading branch information
sergejey committed Apr 13, 2020
2 parents 95befb3 + a5f3fb6 commit d6dfa0904b0eb0d8a13a764707be92713fa0f8e4
Showing 1 changed file with 16 additions and 30 deletions.
@@ -9,24 +9,21 @@
*/
function saveToCache($key, $value, $ttl = 60)
{
if (is_array($value)) return;
if (is_array($value) || strlen($value) > 255) return;

global $db;
if (isset($_SERVER['REQUEST_METHOD'])) {
global $memory_cache;
$memory_cache[$key] = $value;
}
if (strlen($value) <= 255) {
$rec = array('KEYWORD' => $key, 'DATAVALUE' => $value, 'EXPIRE' => date('Y-m-d H:i:s', time() + $ttl));
} else {
$rec = array('KEYWORD' => $key, 'DATAVALUE' => '(too big)', 'EXPIRE' => date('Y-m-d H:i:s', time() + $ttl));
}


$rec = array('KEYWORD' => $key, 'DATAVALUE' => $value, 'EXPIRE' => date('Y-m-d H:i:s', time() + $ttl*60));
$sqlQuery = "REPLACE INTO cached_values (KEYWORD, DATAVALUE, EXPIRE) " .
" VALUES ('" . DbSafe1($rec['KEYWORD']) . "', " .
"'" . DbSafe1($rec['DATAVALUE']) . "'," .
"'" . $rec['EXPIRE'] . "')";
SQLExec($sqlQuery);
// почистим кеш
SQLExec("DELETE FROM cached_values WHERE EXPIRE < NOW()");
}

/**
@@ -36,27 +33,16 @@ function saveToCache($key, $value, $ttl = 60)
*/
function checkFromCache($key)
{
global $memory_cache;

if (isset($_SERVER['REQUEST_METHOD'])
&& !is_array($memory_cache)
) {
$tmp = SQLSelect("SELECT KEYWORD, DATAVALUE FROM cached_values");
$total = count($tmp);

for ($i = 0; $i < $total; $i++) {
if ($tmp[$i]['DATAVALUE'] != '(too big)') {
$memory_cache[$tmp[$i]['KEYWORD']] = $tmp[$i]['DATAVALUE'];
}
}
}

if (isset($memory_cache[$key]))
return $memory_cache[$key];

$rec = SQLSelectOne("SELECT * FROM cached_values WHERE KEYWORD = '" . DBSafe($key) . "'");

if ($rec['KEYWORD'] && $rec['DATAVALUE'] != '(too big)') {
if (isset($_SERVER['REQUEST_METHOD'])) {
global $memory_cache;
if (is_array($memory_cache) && isset($memory_cache[$key])) {
return $memory_cache[$key];
}
}

$rec = SQLSelectOne("SELECT * FROM cached_values WHERE KEYWORD = '" . DBSafe($key) . "'");

if ($rec['KEYWORD']) {
return $rec['DATAVALUE'];
} else {
return false;
@@ -202,4 +188,4 @@ function moveDataFromTableToMainHistory($value_id)
SQLExec("INSERT INTO phistory (VALUE_ID,ADDED,VALUE,SOURCE) SELECT VALUE_ID,ADDED,VALUE,SOURCE FROM $table_name");
SQLDropTable($table_name);
return true;
}
}

0 comments on commit d6dfa09

Please sign in to comment.