From 8ef419f795c8ac9cc50b4c0236185939c851ca32 Mon Sep 17 00:00:00 2001 From: Sebastian Grewe Date: Tue, 21 Jan 2014 10:16:17 +0100 Subject: [PATCH] [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