From 25960c71aa2552e2241e92149cabd21bf41bace6 Mon Sep 17 00:00:00 2001 From: jan Date: Sat, 29 Mar 2008 11:11:47 +0000 Subject: [PATCH] frontend: caching added git-svn-id: file:///home/jan/tmp/wetterstation/trunk@247 dd492736-c11a-0410-ad51-8c26713eaf7f --- webstuff/frontend/frontend.conf | 7 ++ webstuff/frontend/php_inc/cacher.inc.php | 109 ++++++++++++++++++ webstuff/frontend/php_inc/config.inc.php | 15 +++ webstuff/frontend/php_inc/connection.inc.php | 3 + webstuff/frontend/php_inc/modules/hum.inc.php | 18 ++- .../frontend/php_inc/modules/press.inc.php | 20 +++- .../frontend/php_inc/modules/rain.inc.php | 17 ++- .../frontend/php_inc/modules/sensor.inc.php | 78 +++++++------ .../frontend/php_inc/modules/temp.inc.php | 22 ++-- .../frontend/php_inc/modules/wind.inc.php | 28 +++-- 10 files changed, 254 insertions(+), 63 deletions(-) create mode 100644 webstuff/frontend/php_inc/cacher.inc.php diff --git a/webstuff/frontend/frontend.conf b/webstuff/frontend/frontend.conf index 2d3bc16..7ce8526 100644 --- a/webstuff/frontend/frontend.conf +++ b/webstuff/frontend/frontend.conf @@ -9,6 +9,13 @@ $pg_user = "weatherstation"; $pg_pass = ""; +/* Caching */ + $performCaching = true; + $cachePath = "/tmp/weatherstation-frontend/cache/"; + +/* Debug */ + $isDebug = false; + /* Default-Werte */ $default_set = "small"; // Default Set welches angezeigt wird $default_chart_dir = "images/chart"; // Verzeichnis in dem die Bilder liegen diff --git a/webstuff/frontend/php_inc/cacher.inc.php b/webstuff/frontend/php_inc/cacher.inc.php new file mode 100644 index 0000000..a5dcf51 --- /dev/null +++ b/webstuff/frontend/php_inc/cacher.inc.php @@ -0,0 +1,109 @@ + (time() - ($expire *60))){ + return true; + } + } + return false; + } + + function _lockRead($identifier){ + $handle = fopen(Cacher::_lockFileName($identifier), "w"); + $i = 0; + while (flock($handle, LOCK_SH) != true){ + usleep(10); + if ($i > 10){ + fclose($handle); + echo "bla1"; + return false; + } + $i ++; + } + return $handle; + } + + + function _unlockRead($handle){ + flock($handle, LOCK_UN); + fclose($handle); + } + + + function _lockWrite($identifier){ + $handle = fopen(Cacher::_lockFileName($identifier), "a+"); + $i = 0; + while (flock($handle, LOCK_EX) != true){ + usleep(10); + if ($i > 10){ + fclose($handle); + echo "bla"; + return false; + } + $i++; + } + return $handle; + } + + + function _unlockWrite($handle){ + flock($handle, LOCK_UN); + fclose($handle); + } + + function getCache($identifier, $expire){ + if(!Config::performCaching()){ + return false; + } + $lock = null; + if (Cacher::_checkCache($identifier, $expire) && (($lock = Cacher::_lockRead($identifier)) != false)){ + $file = Cacher::_cacheFileName($identifier); + $handle = fopen($file, "r"); + $serialized = fread ($handle, filesize ($file)); + Cacher::_unlockRead($lock); + return unserialize($serialized); + } else { + return false; + } + } + function setCache($identifier, $var){ + if(!Config::performCaching()){ + return false; + } + $lock = null; + if (($lock = Cacher::_lockWrite($identifier)) != false){ + $file = Cacher::_cacheFileName($identifier); + $handle = fopen($file, "w"); + fwrite($handle, serialize($var)); + fclose($handle); + Cacher::_unlockWrite($lock); + } + } + + +} diff --git a/webstuff/frontend/php_inc/config.inc.php b/webstuff/frontend/php_inc/config.inc.php index f92a1d5..b324180 100644 --- a/webstuff/frontend/php_inc/config.inc.php +++ b/webstuff/frontend/php_inc/config.inc.php @@ -58,5 +58,20 @@ class Config{ global $notStandard; return $notStandard; } + + function getCachePath(){ + global $cachePath; + return $cachePath; + } + + function performCaching(){ + global $performCaching; + return $performCaching; + } + + function isDebug(){ + global $isDebug; + return $isDebug; + } } ?> diff --git a/webstuff/frontend/php_inc/connection.inc.php b/webstuff/frontend/php_inc/connection.inc.php index 32197a3..e8bd13b 100644 --- a/webstuff/frontend/php_inc/connection.inc.php +++ b/webstuff/frontend/php_inc/connection.inc.php @@ -37,6 +37,7 @@ class Connection{ /* Eine Zeile holen */ function fetchQueryResultLine($query){ + if (Config::isDebug()) echo $query."\n"; $this->_createConn(); $result = pg_query($this->conn, $query) or die('Abfrage fehlgeschlagen: ' . pg_last_error(). "\n
\nquery: '".$query."'"); @@ -47,6 +48,7 @@ class Connection{ /* mehrere Zeilen holen */ function fetchQueryResultSet($query){ + if (Config::isDebug()) echo $query."\n"; $returnArray = array(); $this->_createConn(); $result = pg_query($this->conn, $query) @@ -58,6 +60,7 @@ class Connection{ /* Result roh zurueckgeben */ function &getRawResult($query){ + if (Config::isDebug()) echo $query."\n"; $this->_createConn(); $result = pg_query($this->conn, $query) or die('Abfrage fehlgeschlagen: ' . pg_last_error(). "\n
\nquery: '".$query."'"); diff --git a/webstuff/frontend/php_inc/modules/hum.inc.php b/webstuff/frontend/php_inc/modules/hum.inc.php index c619dff..fb83093 100644 --- a/webstuff/frontend/php_inc/modules/hum.inc.php +++ b/webstuff/frontend/php_inc/modules/hum.inc.php @@ -33,9 +33,13 @@ class Hum{ /* Funktion, die die Klasse mit den Weten initialisiert */ function _fetchHumData(){ - /* Aktuelle Luftfeuchtigkeit bestimmen */ - $nowQuery = "SELECT hum, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1"; - $nowData = $this->connection->fetchQueryResultLine($nowQuery); + $nowData = null; + if (($nowData = Cacher::getCache("HumNow_ID_".$this->sensId, 3)) == false){ + /* Aktuelle Luftfeuchtigkeit bestimmen */ + $nowQuery = "SELECT hum, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1"; + $nowData = $this->connection->fetchQueryResultLine($nowQuery); + Cacher::setCache("HumNow_ID_".$this->sensId, $nowData); + } /* Bestimmte Werte den Klassenvariablen zuordnen */ $this->nowHum = $nowData['hum']; @@ -52,8 +56,12 @@ class Hum{ if($this->maxHum == "nc" || $this->minHum == "nc"){ $this->_fetchMinMax(); } - $Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND hum=".$this->maxHum." OR hum=".$this->minHum." GROUP BY hum ORDER BY hum ASC LIMIT 2"; - $Data = $this->connection->fetchQueryResultSet($Query); + $Data = null; + if (($Data = Cacher::getCache("HumExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, 30)) == false){ + $Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND hum=".$this->maxHum." OR hum=".$this->minHum." GROUP BY hum ORDER BY hum ASC LIMIT 2"; + $Data = $this->connection->fetchQueryResultSet($Query); + Cacher::setCache("HumExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, $Data); + } $this->minDate = $Data[0]['text_timestamp']; $this->maxDate = $Data[1]['text_timestamp']; } diff --git a/webstuff/frontend/php_inc/modules/press.inc.php b/webstuff/frontend/php_inc/modules/press.inc.php index 44ce4eb..b985990 100644 --- a/webstuff/frontend/php_inc/modules/press.inc.php +++ b/webstuff/frontend/php_inc/modules/press.inc.php @@ -32,9 +32,13 @@ class Press{ /* Funktion, die die Klasse mit den Weten initialisiert */ function _fetchPressData(){ - /* Aktuelle Luftdruck bestimmen */ - $nowQuery = "SELECT press, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1"; - $nowData = $this->connection->fetchQueryResultLine($nowQuery); + $nowData = null; + if (($nowData = Cacher::getCache("PressNow_ID_".$this->sensId, 3)) == false){ + /* Aktuelle Luftdruck bestimmen */ + $nowQuery = "SELECT press, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1"; + $nowData = $this->connection->fetchQueryResultLine($nowQuery); + Cacher::setCache("PressNow_ID_".$this->sensId, $nowData); + } /* Bestimmte Werte den Klassenvariablen zuordnen */ $this->nowPress = $nowData['press']; @@ -48,11 +52,15 @@ class Press{ } function _fetchMinMaxDate(){ - if($this->maxHum == "nc" || $this->minHum == "nc"){ + if($this->maxPress == "nc" || $this->minPress == "nc"){ $this->_fetchMinMax(); } - $Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND press=".$this->maxPress." OR press=".$this->minPress." GROUP BY press ORDER BY press ASC LIMIT 2"; - $Data = $this->connection->fetchQueryResultSet($Query); + $Data = null; + if (($Data = Cacher::getCache("PressExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, 130)) == false){ + $Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND press=".$this->maxPress." OR press=".$this->minPress." GROUP BY press ORDER BY press ASC LIMIT 2"; + $Data = $this->connection->fetchQueryResultSet($Query); + Cacher::setCache("PressExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, $Data); + } $this->minDate = $Data[0]['text_timestamp']; $this->maxDate = $Data[1]['text_timestamp']; } diff --git a/webstuff/frontend/php_inc/modules/rain.inc.php b/webstuff/frontend/php_inc/modules/rain.inc.php index a9b6f08..6ab2af4 100644 --- a/webstuff/frontend/php_inc/modules/rain.inc.php +++ b/webstuff/frontend/php_inc/modules/rain.inc.php @@ -34,13 +34,21 @@ class Rain{ /* Momentane Werte aus der Datenbank holen */ function _getNowValues($interval){ - $result = $this->connection->fetchQueryResultLine("SELECT sum(count) as rain FROM ".$this->table." WHERE sens_id=".$this->sensId." AND timestamp>(select (current_timestamp - INTERVAL '".$interval."'))"); + if (($result = Cacher::getCache("NowRain_ID_".$this->sensId."_Interval_".$interval, 7)) == false){ + $result = $this->connection->fetchQueryResultLine("SELECT sum(count) as rain FROM ".$this->table." WHERE sens_id=".$this->sensId." AND timestamp>(select (current_timestamp - INTERVAL '".$interval."'))"); + Cacher::setCache("NowRain_ID_".$this->sensId."_Interval_".$interval, $result); + } return $result['rain']; } /* Maximal gemessene Werte aus der Datenbank holen */ function _getMaxValues($unit, $dateFormat){ // unit = hour, minute, ... - return $this->connection->fetchQueryResultLine("SELECT to_char(ts, '".$dateFormat."') as date, val FROM ".$this->table."_".$unit." WHERE sens_id=".$this->sensId." ORDER BY val DESC, ts DESC LIMIT 1"); + $res = null; + if (($res = Cacher::getCache("MaxRain_ID_".$this->sensId."_Unit_".$unit."_Format_".$dateFormat, 30)) == false ){ + $res = $this->connection->fetchQueryResultLine("SELECT to_char(ts, '".$dateFormat."') as date, val FROM ".$this->table."_".$unit." WHERE sens_id=".$this->sensId." ORDER BY val DESC, ts DESC LIMIT 1"); + Cacher::setCache("MaxRain_ID_".$this->sensId."_Unit_".$unit."_Format_".$dateFormat, $res); + } + return $res; } @@ -71,7 +79,10 @@ class Rain{ function get_now_all(){ if($this->nowAll == "nc"); - $this->nowAll = $this->connection->fetchQueryResultLine("SELECT sum(count) as rain FROM ".$this->table." WHERE sens_id=".$this->sensId); + if (( $this->nowAll = Cacher::getCache("AllRainNow_ID_".$this->sensId, 20)) == false){ + $this->nowAll = $this->connection->fetchQueryResultLine("SELECT sum(count) as rain FROM ".$this->table." WHERE sens_id=".$this->sensId); + Cacher::setCache("AllRainNow_ID_".$this->sensId, $this->nowAll); + } return round($this->nowAll['rain'] * 0.001,3); } diff --git a/webstuff/frontend/php_inc/modules/sensor.inc.php b/webstuff/frontend/php_inc/modules/sensor.inc.php index 263e3f7..4323d8c 100644 --- a/webstuff/frontend/php_inc/modules/sensor.inc.php +++ b/webstuff/frontend/php_inc/modules/sensor.inc.php @@ -8,6 +8,7 @@ include_once($path."php_inc/connection.inc.php"); +include_once($path."php_inc/cacher.inc.php"); $sensor_data = NULL; $extrema_fields = array( @@ -47,10 +48,13 @@ class Sensor{ function _fetchSensorData($sensId, &$connection){ global $sensor_data; if ($sensor_data == NULL){ - $query = "SELECT sensoren.*, typen.tabelle FROM sensoren, typen where typen.typ = sensoren.typ"; - $result = $connection->getRawResult($query); - while ($array = pg_fetch_assoc($result)){ - $sensor_data[$array['id']] = $array; + if (($sensor_data = Cacher::getCache("SensorData", 20)) == false){ + $query = "SELECT sensoren.*, typen.tabelle FROM sensoren, typen where typen.typ = sensoren.typ"; + $result = $connection->getRawResult($query); + while ($array = pg_fetch_assoc($result)){ + $sensor_data[$array['id']] = $array; + } + Cacher::setCache("SensorData", $sensor_data); } } return $sensor_data[$sensId]; @@ -87,17 +91,20 @@ class Sensor{ function get_sensor_extrema($sensId, &$connection, $field){ global $extrema_fields, $sensor_extrema; if (! array_key_exists($sensId, $sensor_extrema)){ - $data = Sensor::_fetchSensorData($sensId, &$connection); - $query = "SELECT "; - for ($i = 0; $i < count($extrema_fields[$data['typ']]); $i++){ - if ($i != 0) - $query .= ", "; - $query .= "min(".$extrema_fields[$data['typ']][$i].") AS min_".$extrema_fields[$data['typ']][$i].", "; - $query .= "max(".$extrema_fields[$data['typ']][$i].") AS max_".$extrema_fields[$data['typ']][$i]." "; + if (($sensor_extrema[$sensId] = Cacher::getCache("SensorExtrema_ID_".$sensId, 10)) == false){ + $data = Sensor::_fetchSensorData($sensId, &$connection); + $query = "SELECT "; + for ($i = 0; $i < count($extrema_fields[$data['typ']]); $i++){ + if ($i != 0) + $query .= ", "; + $query .= "min(".$extrema_fields[$data['typ']][$i].") AS min_".$extrema_fields[$data['typ']][$i].", "; + $query .= "max(".$extrema_fields[$data['typ']][$i].") AS max_".$extrema_fields[$data['typ']][$i]." "; + } + $query .= "FROM ".$data['tabelle']." WHERE sens_id = ".$sensId; + $res = $connection->fetchQueryResultLine($query); + $sensor_extrema[$sensId] = $res; + Cacher::setCache("SensorExtrema_ID_".$sensId, $sensor_extrema[$sensId]); } - $query .= "FROM ".$data['tabelle']." WHERE sens_id = ".$sensId; - $res = $connection->fetchQueryResultLine($query); - $sensor_extrema[$sensId] = $res; } return array('min' => $sensor_extrema[$sensId]['min_'.$field], 'max' => $sensor_extrema[$sensId]['max_'.$field]); } @@ -110,28 +117,31 @@ class Sensor{ function get_sensor_average($sensId, &$connection, $field, $init_interval = 0){ global $extrema_fields, $sensor_average; if ((! array_key_exists($init_interval, $sensor_average)) || (! array_key_exists($sensId, $sensor_average[$init_interval]))){ - $interval = $init_interval; - $data = Sensor::_fetchSensorData($sensId, &$connection); - $query = "SELECT count(".$extrema_fields[$data['typ']][0].") AS count, "; - for ($i = 0; $i < count($extrema_fields[$data['typ']]); $i++){ - if ($i != 0) - $query .= ", "; - $query .= "avg(".$extrema_fields[$data['typ']][$i].") AS ".$extrema_fields[$data['typ']][$i]." "; - } - $query .= "FROM ".$data['tabelle']." WHERE sens_id = ".$sensId." AND timestamp>(select(current_timestamp - INTERVAL '"; - $res = array('count' => 0, 'average' => 0); - $m = 0; - if ($interval == 0){ - while ($res['count'] < 4) { - $m++; - $interval = $m*(Config::getAvInterval()); + if (($sensor_average[$init_interval][$sensId] = Cacher::getCache("SensorAverage_ID_".$sensId."_Interval_".$init_interval, 10)) == false){ + $interval = $init_interval; + $data = Sensor::_fetchSensorData($sensId, &$connection); + $query = "SELECT count(".$extrema_fields[$data['typ']][0].") AS count, "; + for ($i = 0; $i < count($extrema_fields[$data['typ']]); $i++){ + if ($i != 0) + $query .= ", "; + $query .= "avg(".$extrema_fields[$data['typ']][$i].") AS ".$extrema_fields[$data['typ']][$i]." "; + } + $query .= "FROM ".$data['tabelle']." WHERE sens_id = ".$sensId." AND timestamp>(select(current_timestamp - INTERVAL '"; + $res = array('count' => 0, 'average' => 0); + $m = 0; + if ($interval == 0){ + while ($res['count'] < 4) { + $m++; + $interval = $m*(Config::getAvInterval()); + $res = $connection->fetchQueryResultLine($query.$interval." minutes'))"); + } + } else { $res = $connection->fetchQueryResultLine($query.$interval." minutes'))"); - } - } else { - $res = $connection->fetchQueryResultLine($query.$interval." minutes'))"); + } + $res['interval'] = $interval; + $sensor_average[$init_interval][$sensId] = $res; + Cacher::setCache("SensorAverage_ID_".$sensId."_Interval_".$init_interval, $sensor_average[$init_interval][$sensId]); } - $res['interval'] = $interval; - $sensor_average[$init_interval][$sensId] = $res; } return array('average' => $sensor_average[$init_interval][$sensId][$field], 'count' => $sensor_average[$init_interval][$sensId]['count'], 'interval' => $sensor_average[$init_interval][$sensId]['interval']); } diff --git a/webstuff/frontend/php_inc/modules/temp.inc.php b/webstuff/frontend/php_inc/modules/temp.inc.php index 9c7015e..cf923b3 100644 --- a/webstuff/frontend/php_inc/modules/temp.inc.php +++ b/webstuff/frontend/php_inc/modules/temp.inc.php @@ -32,10 +32,14 @@ class Temp{ /* Funktion, die die Klasse mit den Weten initialisiert */ function _fetchTempData(){ - /* Aktuelle Temperatur bestimmen */ - $nowQuery = "SELECT temp, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1"; - $nowData = $this->connection->fetchQueryResultLine($nowQuery); - + $nowData = null; + if (($nowData = Cacher::getCache("TempNow_ID_".$this->sensId, 3)) == false){ + /* Aktuelle Temperatur bestimmen */ + $nowQuery = "SELECT temp, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1"; + $nowData = $this->connection->fetchQueryResultLine($nowQuery); + Cacher::setCache("TempNow_ID_".$this->sensId, $nowData); + } + /* Bestimmte Werte den Klassenvariablen zuordnen */ $this->nowTemp = $nowData['temp']; $this->nowDate = $nowData['text_timestamp']; @@ -48,11 +52,15 @@ class Temp{ } function _fetchMinMaxDate(){ - if($this->maxHum == "nc" || $this->minHum == "nc"){ + if($this->maxTemp == "nc" || $this->minTemp == "nc"){ $this->_fetchMinMax(); } - $Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND temp=".$this->maxTemp." OR temp=".$this->minTemp." GROUP BY temp ORDER BY temp ASC LIMIT 2"; - $Data = $this->connection->fetchQueryResultSet($Query); + $Data = null; + if (($Data = Cacher::getCache("TempExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, 30)) == false){ + $Query = "SELECT to_char(max(timestamp), 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND temp=".$this->maxTemp." OR temp=".$this->minTemp." GROUP BY temp ORDER BY temp ASC LIMIT 2"; + $Data = $this->connection->fetchQueryResultSet($Query); + Cacher::setCache("TempExtremDate_ID_".$this->sensId."_MINMAX_".$this->maxTemp."_".$this->minTemp, $Data); + } $this->minDate = $Data[0]['text_timestamp']; $this->maxDate = $Data[1]['text_timestamp']; } diff --git a/webstuff/frontend/php_inc/modules/wind.inc.php b/webstuff/frontend/php_inc/modules/wind.inc.php index 61ddb61..726688f 100644 --- a/webstuff/frontend/php_inc/modules/wind.inc.php +++ b/webstuff/frontend/php_inc/modules/wind.inc.php @@ -32,13 +32,21 @@ class Wind{ /* Funktion, die die Klasse mit den Weten initialisiert */ function _fetchWindData(){ - /* Aktuelle Wind bestimmen */ - $nowQuery = "SELECT geschw as wind, richt as dir, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1"; - $nowData = $this->connection->fetchQueryResultLine($nowQuery); + $nowData = null; + if (($nowData = Cacher::getCache("WindNow_ID_".$this->sensId, 3)) == false){ + /* Aktuelle Wind bestimmen */ + $nowQuery = "SELECT geschw as wind, richt as dir, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." ORDER BY timestamp DESC LIMIT 1"; + $nowData = $this->connection->fetchQueryResultLine($nowQuery); + Cacher::setCache("WindNow_ID_".$this->sensId, $nowData); + } - /* Max und Min-Werte bestimmen */ - $maxQuery = "SELECT geschw as wind, richt as dir, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND geschw=(SELECT max(geschw) FROM ".$this->table." WHERE sens_id=".$this->sensId.") ORDER BY timestamp DESC LIMIT 1"; - $maxData = $this->connection->fetchQueryResultLine($maxQuery); + $maxData = null; + if (($nowData = Cacher::getCache("WindMax_ID_".$this->sensId, 3)) == false){ + /* Max und Min-Werte bestimmen */ + $maxQuery = "SELECT geschw as wind, richt as dir, to_char(timestamp, 'DD.MM.YYYY HH24:MI') as text_timestamp FROM ".$this->table." WHERE sens_id=".$this->sensId." AND geschw=(SELECT max(geschw) FROM ".$this->table." WHERE sens_id=".$this->sensId.") ORDER BY timestamp DESC LIMIT 1"; + $maxData = $this->connection->fetchQueryResultLine($maxQuery); + Cacher::setCache("WindMax_ID_".$this->sensId, $maxData); + } /* Bestimmte Werte den Klassenvariablen zuordnen */ $this->nowWind = $nowData['wind']; @@ -126,8 +134,12 @@ class Wind{ /* liefert den Durchschnittswert in einem bestimmtem Interval */ function _getAverage($interval){ - $avQuery = "SELECT avg(geschw) as average, count(geschw) as count FROM ".$this->table." WHERE sens_id=".$this->sensId." AND timestamp>(select (current_timestamp - INTERVAL '".$interval."'))"; - $avData = $this->connection->fetchQueryResultLine($avQuery); + $avData = null; + if(($avData = Cacher::getCache("WindAv_ID_".$this->sensId."_INTERVAL_".$interval, 10)) == false){ + $avQuery = "SELECT avg(geschw) as average, count(geschw) as count FROM ".$this->table." WHERE sens_id=".$this->sensId." AND timestamp>(select (current_timestamp - INTERVAL '".$interval."'))"; + $avData = $this->connection->fetchQueryResultLine($avQuery); + Cacher::setCache("WindAv_ID_".$this->sensId."_INTERVAL_".$interval, $avData); + } return $avData; }