Skip to content
Permalink
Browse files
Merge pull request #736 from tarasfrompir/patch-1
Добавлен выбор использования КЕШ в памяти
  • Loading branch information
sergejey committed May 15, 2020
2 parents ff07078 + 5c22411 commit 3f1e9be805f53cab2903f2e86f7d6bb796c14a7e
Showing 4 changed files with 116 additions and 80 deletions.
@@ -4,10 +4,10 @@
* Summary of saveToCache
* @param mixed $key Key
* @param mixed $value Value
* @param mixed $ttl TTL (default 60)
* @param mixed $ttl TTL (532800) seconds (7 days)
* @return void
*/
function saveToCache($key, $value, $ttl = 60)
function saveToCache($key, $value, $ttl = 532800)
{
if (is_array($value) || strlen($value) > 255) return;

@@ -16,14 +16,12 @@ function saveToCache($key, $value, $ttl = 60)
$memory_cache[$key] = $value;
}

$rec = array('KEYWORD' => $key, 'DATAVALUE' => $value, 'EXPIRE' => date('Y-m-d H:i:s', time() + $ttl*60));
$rec = array('KEYWORD' => $key, 'DATAVALUE' => $value, 'EXPIRE' => date('Y-m-d H:i:s', time() + $ttl));
$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()");
}

/**
@@ -600,18 +600,15 @@ function getGlobal($varname)
} else {
$object_name = 'ThisComputer';
}
$cached_name = 'MJD:' . $object_name . '.' . $varname;
$cached_value = checkFromCache($cached_name);

if ($cached_value !== false) {
return $cached_value;
if (defined('SYSTEM_DISABLE_CACHE') && SYSTEM_DISABLE_CACHE && false !== $value = checkFromCache('MJD:' . $object_name . '.' . $property) ) {
return $value;
}

$obj = getObject($object_name);

if ($obj) {
$value = $obj->getProperty($varname);
saveToCache($cached_name, $value);
return $value;
} else {
return 0;
@@ -931,7 +928,7 @@ function setGlobal($varname, $value, $no_linked = 0, $source = '')
if ($obj) {
return $obj->setProperty($varname, $value, $no_linked, $source);
} else {
return 0;
return false;
}
}

@@ -706,6 +706,9 @@ function getProperty($property)
$property = trim($property);

if ($this->object_title) {
if (defined('SYSTEM_DISABLE_CACHE') && SYSTEM_DISABLE_CACHE && false !== $value = checkFromCache('MJD:' . $this->object_title . '.' . $property) ) {
return $value;
}
$value = SQLSelectOne("SELECT VALUE FROM pvalues WHERE PROPERTY_NAME = '" . DBSafe($this->object_title . '.' . $property) . "'");
if (isset($value['VALUE'])) {
startMeasure('getPropertyCached2');
@@ -760,32 +763,34 @@ function getProperty($property)
*
* @access public
*/
function setProperty($property, $value, $no_linked = 0, $source = '')
function setProperty($property, $value='', $no_linked = 0, $source = '')
{

if (!preg_match('/cycle/is', $property) && function_exists('verbose_log')) {
if (is_array($value)) {
DebMes ('WARNING!!! Wrong property ' . $property . ' cannot to be array', 'property');
return ;
}
if (stripos($property, 'cycle_') === false) {
verbose_log('Property [' . $this->object_title . '.' . $property . '] set to \'' . $value . '\'');
}
startMeasure('setProperty');
startMeasure('setProperty (' . $property . ')');

$property = trim($property);

if (is_null($value)) {
$value = '';
}

if (!$source && is_string($no_linked)) {
$source = $no_linked;
$no_linked = 0;
}
if (!$source && $_SERVER['REQUEST_URI']) {
$source = urldecode($_SERVER['REQUEST_URI']);
$source = substr(urldecode($_SERVER['REQUEST_URI']), 0, 100);
}
if (strlen($source) > 250) {
$source = substr($source, 0, 250) . '...';
if (strlen($source) > 247) {
$source = substr($source, 0, 247) . '...';
}

$property = trim($property);
$value = trim($value);

if (defined('TRACK_DATA_CHANGES') && TRACK_DATA_CHANGES == 1) {
$save = 1;

@@ -834,42 +839,37 @@ function setProperty($property, $value, $no_linked = 0, $source = '')
startMeasure('getPropertyByName');
$id = $this->getPropertyByName($property, $this->class_id, $this->id);
endMeasure('getPropertyByName');
$old_value = '';

$cached_name = 'MJD:' . $this->object_title . '.' . $property;

startMeasure('setproperty_update');
if ($id) {
$prop = SQLSelectOne("SELECT * FROM properties WHERE ID='" . $id . "'");

if ($prop['VALIDATION_TYPE'] == 1) {
if (!is_numeric($value)) return false;
if ($prop['VALIDATION_NUM_MIN'] != '' && (float)$value < (float)$prop['VALIDATION_NUM_MIN']) {
return false;
}
if ($prop['VALIDATION_NUM_MAX'] != '' && (float)$value > (float)$prop['VALIDATION_NUM_MAX']) {
return false;
}
}
if ($prop['VALIDATION_TYPE'] == 2) {
if ($value != '1' && $value != '0') {
return false;
}
}
if ($prop['VALIDATION_TYPE'] == 3) {
$items = explode(',', $prop['VALIDATION_LIST']);
if (!in_array(mb_strtolower($value, 'UTF-8'), $items)) return false;
}
if ($prop['VALIDATION_TYPE'] == 100) {
eval($prop['VALIDATION_CODE']);
if (is_null($value)) return false;
// proverki na validnost dannih - nothing to change
switch ($prop['VALIDATION_TYPE']) {
case 1:
if (!is_numeric($value)) return false;
if ($prop['VALIDATION_NUM_MIN'] != '' && (float)$value < (float)$prop['VALIDATION_NUM_MIN']) return false;
if ($prop['VALIDATION_NUM_MAX'] != '' && (float)$value > (float)$prop['VALIDATION_NUM_MAX']) return false;
break;
case 2:
if ($value != '1' && $value != '0') return false;
break;
case 3:
$items = explode(',', $prop['VALIDATION_LIST']);
if (!in_array(mb_strtolower($value, 'UTF-8'), $items)) return false;
break;
case 100:
eval($prop['VALIDATION_CODE']);
if (is_null($value)) return false;
break;
}

$property = $prop['TITLE'];
startMeasure('setproperty_update_getvalue');
$v = SQLSelectOne("SELECT * FROM pvalues WHERE PROPERTY_ID=" . (int)$id . " AND OBJECT_ID=" . (int)$this->id);
endMeasure('setproperty_update_getvalue');
$old_value = $v['VALUE'];

if ($v['ID']) $old_value = $v['VALUE'];

if ($prop['DATA_TYPE'] == 5 && $value != $old_value) { // image
$path_parts = pathinfo($value);
@@ -905,7 +905,7 @@ function setProperty($property, $value, $no_linked = 0, $source = '')
if ($value != '' && $old_value != '' && !$prop['KEEP_HISTORY'] && file_exists(ROOT . 'cms/images/' . $old_value)) {
@unlink(ROOT . 'cms/images/' . $old_value);
}
if ($value == '') $value = $old_value;
//if ($value == '') $value = $old_value;
}

$v['VALUE'] = $value . '';
@@ -940,7 +940,45 @@ function setProperty($property, $value, $no_linked = 0, $source = '')
}
endMeasure('setproperty_update');

saveToCache($cached_name, $value);
// esli staroe znachenie ravno novomu to - peresmotret
if (isset($old_value) && $old_value == $value) {
verbose_log('Property [' . $this->object_title . '.' . $property . '] no change');
} else {
// zapishem v kesh
if (defined('SYSTEM_DISABLE_CACHE') && SYSTEM_DISABLE_CACHE) saveToCache ('MJD:' . $this->object_title . '.' . $property, $value);

//zapostim v websockety
startMeasure('setproperty_postwebsocketqueue');
postToWebSocketQueue($this->object_title . '.' . $property, $value);
endMeasure('setproperty_postwebsocketqueue');

// esli nado sohranyat istoriy to pyshem
if (IsSet($prop['KEEP_HISTORY']) && ($prop['KEEP_HISTORY'] > 0)) {
$q_rec = array();
$q_rec['VALUE_ID'] = $v['ID'];
$q_rec['ADDED'] = date('Y-m-d H:i:s');
$q_rec['VALUE'] = $value . '';
$q_rec['SOURCE'] = $source . '';
$q_rec['OLD_VALUE'] = $old_value;
$q_rec['KEEP_HISTORY'] = $prop['KEEP_HISTORY'];
SQLInsert('phistory_queue', $q_rec);
}

if (isset($prop['ONCHANGE']) && $prop['ONCHANGE']) {
global $property_linked_history;
if (!$property_linked_history[$this->object_title . '.' . $property][$prop['ONCHANGE']]) {
$property_linked_history[$this->object_title . '.' . $property][$prop['ONCHANGE']] = 1;
$params = array();
$params['PROPERTY'] = $property;
$params['NEW_VALUE'] = (string)$value;
$params['OLD_VALUE'] = (string)$old_value;
$params['SOURCE'] = (string)$source;
//$this->callMethod($prop['ONCHANGE'], $params);
$this->callMethodSafe($prop['ONCHANGE'], $params);
unset($property_linked_history[$this->object_title . '.' . $property][$prop['ONCHANGE']]);
}
}
}

$p_lower = strtolower($property);
if (!defined('DISABLE_SIMPLE_DEVICES') &&
@@ -988,38 +1026,6 @@ function setProperty($property, $value, $no_linked = 0, $source = '')
endMeasure('linkedModulesProcessing');
}

if (function_exists('postToWebSocketQueue')) {
startMeasure('setproperty_postwebsocketqueue');
postToWebSocketQueue($this->object_title . '.' . $property, $value);
endMeasure('setproperty_postwebsocketqueue');
}

if (IsSet($prop['KEEP_HISTORY']) && ($prop['KEEP_HISTORY'] > 0)) {
$q_rec = array();
$q_rec['VALUE_ID'] = $v['ID'];
$q_rec['ADDED'] = date('Y-m-d H:i:s');
$q_rec['VALUE'] = $value . '';
$q_rec['SOURCE'] = $source . '';
$q_rec['OLD_VALUE'] = $old_value;
$q_rec['KEEP_HISTORY'] = $prop['KEEP_HISTORY'];
SQLInsert('phistory_queue', $q_rec);
}

if (isset($prop['ONCHANGE']) && $prop['ONCHANGE']) {
global $property_linked_history;
if (!$property_linked_history[$this->object_title . '.' . $property][$prop['ONCHANGE']]) {
$property_linked_history[$this->object_title . '.' . $property][$prop['ONCHANGE']] = 1;
$params = array();
$params['PROPERTY'] = $property;
$params['NEW_VALUE'] = (string)$value;
$params['OLD_VALUE'] = (string)$old_value;
$params['SOURCE'] = (string)$source;
//$this->callMethod($prop['ONCHANGE'], $params);
$this->callMethodSafe($prop['ONCHANGE'], $params);
unset($property_linked_history[$this->object_title . '.' . $property][$prop['ONCHANGE']]);
}
}

endMeasure('setProperty (' . $property . ')', 1);
endMeasure('setProperty', 1);

@@ -1060,6 +1066,19 @@ function processObject($object_id)
return $result;
}

/**
* terminals subscription events
*
* @access public
*/
function processSubscription($event, $details = '') {
if ($event == 'DAILY') {
// почистим кеш
SQLExec("DELETE FROM cached_values WHERE EXPIRE < NOW()");
}

}

/**
* Install
*
@@ -1069,6 +1088,7 @@ function processObject($object_id)
*/
function install($parent_name = "")
{
subscribeToEvent($this->name, 'DAILY');
parent::install($parent_name);
}

@@ -1081,6 +1101,7 @@ function install($parent_name = "")
*/
function uninstall()
{
unsubscribeFromEvent($this->name, 'DAILY');
SQLDropTable('objects');
parent::uninstall();
}
@@ -95,6 +95,26 @@
}
}

if ($this->filter_name == 'system' && !defined('SYSTEM_DISABLE_CACHE')) {
$options = array(
'SYSTEM_DISABLE_CACHE' => 'Disable caching for objects and properties',
);
foreach ($options as $k => $v) {
$tmp = SQLSelectOne("SELECT ID FROM settings WHERE NAME LIKE '" . $k . "'");
if (!$tmp['ID']) {
$tmp = array();
$tmp['NAME'] = $k;
$tmp['TITLE'] = $v;
$tmp['TYPE'] = 'onoff';
$tmp['DEFAULTVALUE'] = '1';
$tmp['NOTES'] = '';
$tmp['VALUE'] = '1';
$tmp['DATA'] = '';
SQLInsert('settings', $tmp);
}
}
}

if ($this->filter_name == 'system' && !defined('SETTINGS_SYSTEM_DEBMES_PATH')) {
$options = array(
'SYSTEM_DEBMES_PATH' => 'Path to DebMes logs'

3 comments on commit 3f1e9be

@Alexandr-logrus
Copy link
Contributor

@Alexandr-logrus Alexandr-logrus commented on 3f1e9be May 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

У себя на малинке погонял без кеша, плохого ничего не было, но возрос трафик и количество запросов - не понял почему, откатил назад
идея то хорошая но проверять

@Alexandr-logrus
Copy link
Contributor

@Alexandr-logrus Alexandr-logrus commented on 3f1e9be May 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

очистка кеша по времени спорная идея имхо

@Alexandr-logrus
Copy link
Contributor

@Alexandr-logrus Alexandr-logrus commented on 3f1e9be May 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

олд валуе перенести ранее свич, что б можно было его использовать
не постить в веб сокеты время обновления циклов
подумать над соурс в историю

и у датчиков движения приходит только 1 и старое всегда равно новому значению и привязанный метод нужно запускать, спорно и налететь могут

Please sign in to comment.