From 8ef419f795c8ac9cc50b4c0236185939c851ca32 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 21 Jan 2014 10:16:17 +0100 Subject: [PATCH 1/3] [ADDED] Static cache with no auto-expiration --- public/include/classes/statistics.class.php | 8 +++--- public/include/classes/statscache.class.php | 27 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index a1b5ac7e..bd80dec0 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -487,7 +487,7 @@ class Statistics extends Base { while ($row = $result->fetch_assoc()) { $aData['data'][$row['id']] = $row; } - return $this->memcache->setCache(STATISTICS_ALL_USER_HASHRATES, $aData); + return $this->memcache->setStaticCache(STATISTICS_ALL_USER_HASHRATES, $aData, 600); } else { return $this->sqlError(); } @@ -501,7 +501,7 @@ class Statistics extends Base { public function getUserHashrate($account_id, $interval=180) { $this->debug->append("STA " . __METHOD__, 4); // Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL - if ($data = $this->memcache->get(STATISTICS_ALL_USER_HASHRATES)) { + if ($data = $this->memcache->getStatic(STATISTICS_ALL_USER_HASHRATES)) { if (array_key_exists($account_id, $data['data'])) return $data['data'][$account_id]['hashrate']; // We have no cached value, we return defaults @@ -563,7 +563,7 @@ class Statistics extends Base { public function getUserShareDifficulty($account_id, $interval=180) { $this->debug->append("STA " . __METHOD__, 4); // Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL - if ($data = $this->memcache->get(STATISTICS_ALL_USER_HASHRATES)) { + if ($data = $this->memcache->getStatic(STATISTICS_ALL_USER_HASHRATES)) { if (array_key_exists($account_id, $data['data'])) return $data['data'][$account_id]['avgsharediff']; // We have no cached value, we return defaults @@ -592,7 +592,7 @@ class Statistics extends Base { public function getUserSharerate($account_id, $interval=180) { $this->debug->append("STA " . __METHOD__, 4); // Dual-caching, try statistics cron first, then fallback to local, then fallbock to SQL - if ($data = $this->memcache->get(STATISTICS_ALL_USER_HASHRATES)) { + if ($data = $this->memcache->getStatic(STATISTICS_ALL_USER_HASHRATES)) { if (array_key_exists($account_id, $data['data'])) return $data['data'][$account_id]['sharerate']; // We have no cached value, we return defaults diff --git a/public/include/classes/statscache.class.php b/public/include/classes/statscache.class.php index 45dcba07..e6bd84c4 100644 --- a/public/include/classes/statscache.class.php +++ b/public/include/classes/statscache.class.php @@ -44,6 +44,18 @@ class StatsCache { return $this->cache->set($this->getRound() . '_' . $this->config['memcache']['keyprefix'] . $key, $value, $expiration); } + /** + * Special memcache->set call bypassing any auto-expiration systems + * Can be used as a static, auto-updated cache via crons + **/ + public function setStaticCache($key, $value, $expiration=NULL) { + if (! $this->config['memcache']['enabled']) return false; + if (empty($expiration)) + $expiration = $this->config['memcache']['expiration'] + rand( -$this->config['memcache']['splay'], $this->config['memcache']['splay']); + $this->debug->append("Storing " . $this->config['memcache']['keyprefix'] . "$key with expiration $expiration", 3); + return $this->cache->set($this->config['memcache']['keyprefix'] . $key, $value, $expiration); + } + /** * Wrapper around memcache->get * Always return false if memcache is disabled @@ -58,6 +70,21 @@ class StatsCache { $this->debug->append("Key not found", 3); } } + + /** + * As the static set call, we try to fetch static data here + **/ + public function getStatic($key, $cache_cb = NULL, &$cas_token = NULL) { + if (! $this->config['memcache']['enabled']) return false; + $this->debug->append("Trying to fetch key " . $this->config['memcache']['keyprefix'] . "$key from cache", 3); + if ($data = $this->cache->get($this->config['memcache']['keyprefix'].$key)) { + $this->debug->append("Found key in cache", 3); + return $data; + } else { + $this->debug->append("Key not found", 3); + } + } + /** * Another wrapper, we want to store data in memcache and return the actual data * for further processing From fe9c70602013a573cc4878e86f78fff5c6bf55ea Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 21 Jan 2014 10:56:08 +0100 Subject: [PATCH 2/3] [IMPROVED] non-auto expire caches added --- public/include/classes/statistics.class.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index bd80dec0..d2e390ae 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -219,7 +219,7 @@ class Statistics extends Base { **/ public function getCurrentHashrate($interval=180) { $this->debug->append("STA " . __METHOD__, 4); - if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__)) return $data; + if ($this->getGetCache() && $data = $this->memcache->getStatic(__FUNCTION__)) return $data; $stmt = $this->mysqli->prepare(" SELECT ( @@ -236,7 +236,7 @@ class Statistics extends Base { ) ) AS hashrate FROM DUAL"); - if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $interval, $interval, $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__, $result->fetch_object()->hashrate); + if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $interval, $interval, $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setStaticCache(__FUNCTION__, $result->fetch_object()->hashrate); return $this->sqlError(); } @@ -247,7 +247,7 @@ class Statistics extends Base { **/ public function getCurrentShareRate($interval=180) { $this->debug->append("STA " . __METHOD__, 4); - if ($data = $this->memcache->get(__FUNCTION__)) return $data; + if ($data = $this->memcache->getStatic(__FUNCTION__)) return $data; $stmt = $this->mysqli->prepare(" SELECT ( @@ -264,7 +264,7 @@ class Statistics extends Base { ) ) AS sharerate FROM DUAL"); - if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $interval, $interval, $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setCache(__FUNCTION__, $result->fetch_object()->sharerate); + if ($this->checkStmt($stmt) && $stmt->bind_param('iiii', $interval, $interval, $interval, $interval) && $stmt->execute() && $result = $stmt->get_result() ) return $this->memcache->setStaticCache(__FUNCTION__, $result->fetch_object()->sharerate); return $this->sqlError(); } From 6b463855845b942f7231351c5171049ac3be0300 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 21 Jan 2014 10:59:25 +0100 Subject: [PATCH 3/3] [IMPROVED] non-auto expire caches added --- public/include/classes/statistics.class.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/public/include/classes/statistics.class.php b/public/include/classes/statistics.class.php index d2e390ae..9cd3105a 100644 --- a/public/include/classes/statistics.class.php +++ b/public/include/classes/statistics.class.php @@ -657,9 +657,9 @@ class Statistics extends Base { **/ public function getTopContributors($type='shares', $limit=15) { $this->debug->append("STA " . __METHOD__, 4); - if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $type . $limit)) return $data; switch ($type) { case 'shares': + if ($this->getGetCache() && $data = $this->memcache->get(__FUNCTION__ . $type . $limit)) return $data; if ($data = $this->memcache->get(STATISTICS_ALL_USER_SHARES)) { // Use global cache to build data, if we have any data there if (!empty($data['data']) && is_array($data['data'])) { @@ -700,6 +700,7 @@ class Statistics extends Base { break; case 'hashes': + if ($this->getGetCache() && $data = $this->memcache->getStatic(__FUNCTION__ . $type . $limit)) return $data; $stmt = $this->mysqli->prepare(" SELECT a.username AS account, @@ -717,7 +718,7 @@ class Statistics extends Base { GROUP BY account ORDER BY hashrate DESC LIMIT ?"); if ($this->checkStmt($stmt) && $stmt->bind_param("i", $limit) && $stmt->execute() && $result = $stmt->get_result()) - return $this->memcache->setCache(__FUNCTION__ . $type . $limit, $result->fetch_all(MYSQLI_ASSOC)); + return $this->memcache->setStaticCache(__FUNCTION__ . $type . $limit, $result->fetch_all(MYSQLI_ASSOC)); return $this->sqlError(); break; }