From f56bc04b6f50d73958c6919c560dab62f67e960e Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Wed, 11 Apr 2012 23:34:04 +0200 Subject: [PATCH] #657: Use new remember() function for more compact code where cache is used. --- footer.php | 8 +++----- header.php | 8 +++----- include/common.php | 22 +++++++++------------- include/functions.php | 30 +++++++++++------------------- include/search_idx.php | 8 +++----- 5 files changed, 29 insertions(+), 47 deletions(-) diff --git a/footer.php b/footer.php index 2ca5f7914..22a89eb36 100644 --- a/footer.php +++ b/footer.php @@ -68,9 +68,7 @@ // Display the "Jump to" drop list if ($pun_config['o_quickjump'] == '1') { - $quickjump = $cache->get('quickjump'); - if ($quickjump === \fluxbb\cache\Cache::NOT_FOUND) - { + $quickjump = $cache->remember('quickjump', function() use ($db) { $quickjump = array(); // Generate the quick jump cache for all groups @@ -101,8 +99,8 @@ unset ($result, $query_forums); - $cache->set('quickjump', $quickjump); - } + return $quickjump; + }); if (!empty($quickjump[$pun_user['g_id']])) { diff --git a/header.php b/header.php index 5e23222fe..4951ff8b5 100644 --- a/header.php +++ b/header.php @@ -233,9 +233,7 @@ function process_form(the_form) { if ($pun_config['o_report_method'] == '0' || $pun_config['o_report_method'] == '2') { - $num_reports = $cache->get('num_reports'); - if ($num_reports === \fluxbb\cache\Cache::NOT_FOUND) - { + $num_reports = $cache->remember('num_reports', function() use ($db) { $query = $db->select(array('num_reports' => 'COUNT(r.id) AS num_reports'), 'reports AS r'); $query->where = 'r.zapped IS NULL'; @@ -245,8 +243,8 @@ function process_form(the_form) $num_reports = $result[0]['num_reports']; unset ($result, $query, $params); - $cache->set('num_reports', $num_reports); - } + return $num_reports; + }); if ($num_reports > 0) $page_statusinfo[] = ''; diff --git a/include/common.php b/include/common.php index f505aeb55..b010331ba 100644 --- a/include/common.php +++ b/include/common.php @@ -114,10 +114,8 @@ function stripslashes_array($array) $db->startTransaction(); // Load cached config -$pun_config = $cache->get('config'); -if ($pun_config === \fluxbb\cache\Cache::NOT_FOUND) -{ - $pun_config = array(); +$pun_config = $cache->remember('config', function() use ($db) { + $cfg = array(); // Get the forum config from the DB $query = $db->select(array('conf_name' => 'c.conf_name', 'conf_value' => 'c.conf_value'), 'config AS c'); @@ -125,12 +123,12 @@ function stripslashes_array($array) $result = $query->run($params); foreach ($result as $cur_config_item) - $pun_config[$cur_config_item['conf_name']] = $cur_config_item['conf_value']; + $cfg[$cur_config_item['conf_name']] = $cur_config_item['conf_value']; unset ($query, $params, $result); - $cache->set('config', $pun_config); -} + return $cfg; +}); // Verify that we are running the proper database schema revision /*if (!isset($pun_config['o_database_revision']) || $pun_config['o_database_revision'] < FORUM_DB_REVISION || @@ -173,18 +171,16 @@ function stripslashes_array($array) maintenance_message(); // Load cached bans -$pun_bans = $cache->get('bans'); -if ($pun_bans === \fluxbb\cache\Cache::NOT_FOUND) -{ +$pun_bans = $cache->remember('bans', function() use ($db) { // Get the ban list from the DB $query = $db->select(array('id' => 'b.id', 'username' => 'b.username', 'ip' => 'b.ip', 'email' => 'b.email', 'message' => 'b.message', 'expire' => 'b.expire', 'ban_creator' => 'b.ban_creator'), 'bans AS b'); $params = array(); - $pun_bans = $query->run($params); + $bans = $query->run($params); unset ($query, $params); - $cache->set('bans', $pun_bans); -} + return $bans; +}); // Check if current user is banned check_bans(); diff --git a/include/functions.php b/include/functions.php index c015b23a3..4fd93417d 100644 --- a/include/functions.php +++ b/include/functions.php @@ -14,9 +14,7 @@ function fetch_board_stats() { global $cache, $db; - $stats = $cache->get('boardstats'); - if ($stats === \fluxbb\cache\Cache::NOT_FOUND) - { + return $cache->remember('boardstats', function() use ($db) { $stats = array(); // Count total registered users @@ -39,10 +37,8 @@ function fetch_board_stats() $stats['last_user'] = current($query->run($params)); unset ($query, $params); - $cache->set('boardstats', $stats); - } - - return $stats; + return $stats; + }); } @@ -1014,9 +1010,7 @@ function censor_words($text) // If not already built in a previous call, build an array of censor words and their replacement text if (!isset($censors)) { - $censors = $cache->get('censors'); - if ($censors === \fluxbb\cache\Cache::NOT_FOUND) - { + $censors = $cache->remember('censors', function() use ($db) { $censors = array(); $query = $db->select(array('search_for' => 'c.search_for', 'replace_with' => 'c.replace_with'), 'censoring AS c'); @@ -1030,8 +1024,8 @@ function censor_words($text) } unset ($result, $query, $params); - $cache->set('censors', $censors); - } + return $censors; + }); } if (!empty($censors)) @@ -1062,10 +1056,8 @@ function get_title($user) // If not already loaded in a previous call, load the cached ranks if ($pun_config['o_ranks'] == '1' && !isset($pun_ranks)) { - $pun_ranks = $cache->get('ranks'); - if ($pun_ranks === \fluxbb\cache\Cache::NOT_FOUND) - { - $pun_ranks = array(); + $pun_ranks = $cache->remember('ranks', function() use ($db) { + $ranks = array(); // Get the rank list from the DB $query = $db->select(array('ranks' => 'r.*'), 'ranks AS r'); @@ -1073,11 +1065,11 @@ function get_title($user) $params = array(); - $pun_ranks = $query->run($params); + $ranks = $query->run($params); unset ($query, $params); - $cache->set('ranks', $pun_ranks); - } + return $ranks; + }); } // If the user has a custom title diff --git a/include/search_idx.php b/include/search_idx.php index e631923f1..e6f5ca835 100644 --- a/include/search_idx.php +++ b/include/search_idx.php @@ -90,9 +90,7 @@ function validate_search_word($word, $idx) { $cache_id = generate_stopwords_cache_id(); - $stopwords = $cache->get('stopwords.'.$cache_id); - if ($stopwords === \fluxbb\cache\Cache::NOT_FOUND) - { + $stopwords = $cache->remember('stopwords.'.$cache_id, function() { $stopwords = array(); $d = dir(PUN_ROOT.'lang'); @@ -110,8 +108,8 @@ function validate_search_word($word, $idx) $stopwords = array_map('pun_trim', $stopwords); $stopwords = array_filter($stopwords); - $cache->set('stopwords.'.$cache_id, $stopwords); - } + return $stopwords; + }); } // If it is a stopword it isn't valid