Skip to content

Commit

Permalink
#657: Use new remember() function for more compact code where cache i…
Browse files Browse the repository at this point in the history
…s used.
  • Loading branch information
franzliedke committed Apr 11, 2012
1 parent 320bf9e commit f56bc04
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 47 deletions.
8 changes: 3 additions & 5 deletions footer.php
Expand Up @@ -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
Expand Down Expand Up @@ -101,8 +99,8 @@

unset ($result, $query_forums);

$cache->set('quickjump', $quickjump);
}
return $quickjump;
});

if (!empty($quickjump[$pun_user['g_id']]))
{
Expand Down
8 changes: 3 additions & 5 deletions header.php
Expand Up @@ -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';

Expand All @@ -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[] = '<li class="reportlink"><span><strong><a href="admin_reports.php">'.$lang->t('New reports').'</a></strong></span></li>';
Expand Down
22 changes: 9 additions & 13 deletions include/common.php
Expand Up @@ -114,23 +114,21 @@ 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');
$params = 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 ||
Expand Down Expand Up @@ -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();
Expand Down
30 changes: 11 additions & 19 deletions include/functions.php
Expand Up @@ -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
Expand All @@ -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;
});
}


Expand Down Expand Up @@ -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');
Expand All @@ -1030,8 +1024,8 @@ function censor_words($text)
}

unset ($result, $query, $params);
$cache->set('censors', $censors);
}
return $censors;
});
}

if (!empty($censors))
Expand Down Expand Up @@ -1062,22 +1056,20 @@ 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');
$query->order = array('min_posts' => 'r.min_posts ASC');

$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
Expand Down
8 changes: 3 additions & 5 deletions include/search_idx.php
Expand Up @@ -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');
Expand All @@ -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
Expand Down

0 comments on commit f56bc04

Please sign in to comment.