Facebook  Twitter 

SMFHacks.com

+- +-

SMFHacks.com

+- User Information

Welcome, Guest.
Please login or register.
 
 
 
Forgot your password?

+- Forum Stats

Members
Total Members: 12172
Latest: LeaMoeller
New This Month: 14
New This Week: 1
New Today: 0
Stats
Total Posts: 39049
Total Topics: 6874
Most Online Today: 59
Most Online Ever: 2482
(April 09, 2011, 07:02:45 pm)
Users Online
Members: 0
Guests: 33
Total: 33

Author Topic: Is this the correct way to cache?  (Read 189 times)

0 Members and 1 Guest are viewing this topic.

Offline shuban

  • Hero Member
  • *****
  • Posts: 637
    • View Profile
    • Biology Forums - For All Your Science Needs
Is this the correct way to cache?
« on: June 06, 2018, 11:36:47 am »
Code: [Select]
// Let's cache the results to load faster...
if (($temp = cache_get_data('latest_topics', 480)) == null)
{

$request = db_query("
SELECT $select_clause
FROM {$db_prefix}topics AS t
INNER JOIN {$db_prefix}messages AS ml ON (ml.ID_MSG = t.ID_LAST_MSG)
INNER JOIN {$db_prefix}messages AS mso ON (mso.ID_MSG = t.ID_FIRST_MSG)
LEFT JOIN {$db_prefix}boards AS b ON (b.ID_BOARD = ml.ID_BOARD)
LEFT JOIN {$db_prefix}members AS meml ON (meml.ID_MEMBER = ml.ID_MEMBER)
LEFT JOIN {$db_prefix}log_topics AS lt ON (lt.ID_TOPIC = ml.ID_TOPIC AND lt.ID_MEMBER = " . $ID_MEMBER .")
LEFT JOIN {$db_prefix}log_mark_read AS lmr ON (lmr.ID_BOARD = t.ID_BOARD AND lmr.ID_MEMBER = " . $ID_MEMBER .")
WHERE t." . $query_this_board . "
AND t.ID_LAST_MSG >= " . $min_message ." ".$time_frame."
ORDER BY " . $_REQUEST['sort'] . ($ascending ? '' : ' DESC') ."
LIMIT $_REQUEST[start], $modSettings[defaultMaxTopics]", __FILE__, __LINE__);

$context['topics'] = array();
$topic_ids = array();
while ($row = mysql_fetch_assoc($request))
{
if ($row['ID_POLL'] > 0 && $modSettings['pollMode'] == '0')
continue;

$topic_ids[] = $row['ID_TOPIC'];

// * NiceTooltips mod
$row['nice_tooltip_first_msg'] = NiceTooltip($row['first_body'], $row['first_subject'], $row['first_smileys'], $row['ID_FIRST_MSG']);

// Limit them to 128 characters - do this FIRST because it's a lot of wasted censoring otherwise.
$row['first_body'] = strip_tags(strtr(parse_bbc($row['first_body'], $row['first_smileys'], $row['ID_FIRST_MSG']), array('<br />' => '&#10;')));
if ($func['strlen']($row['first_body']) > 128)
$row['first_body'] = $func['substr']($row['first_body'], 0, 128) . '...';
$row['last_body'] = strip_tags(strtr(parse_bbc($row['last_body'], $row['last_smileys'], $row['ID_LAST_MSG']), array('<br />' => '&#10;')));
if ($func['strlen']($row['last_body']) > 128)

$row['last_body'] = $func['substr']($row['last_body'], 0, 128) . '...';

// Censor the subject and message preview.
censorText($row['first_subject']);
censorText($row['first_body']);

// Don't censor them twice!
if ($row['ID_FIRST_MSG'] == $row['ID_LAST_MSG'])
{
$row['last_subject'] = $row['first_subject'];
$row['last_body'] = $row['first_body'];
}
else
{
censorText($row['last_subject']);
censorText($row['last_body']);
}

// Decide how many pages the topic should have.
$topic_length = $row['numReplies'] + 1;
if ($topic_length > $modSettings['defaultMaxMessages'])
{
$tmppages = array();
$tmpa = 1;
for ($tmpb = 0; $tmpb < $topic_length; $tmpb += $modSettings['defaultMaxMessages'])
{
$tmppages[] = '<a style="margin: 0 1px;" href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.' . $tmpb . '">' . $tmpa . '</a>';
$tmpa++;
}
// Show links to all the pages?
if (count($tmppages) <= 5)
$pages = '<img style="vertical-align: middle;" src="'.$settings['default_theme_url'].'/images/page-small.png"> ' . implode('', $tmppages);
// Or skip a few?
else
$pages = '<img style="vertical-align: middle;" src="'.$settings['default_theme_url'].'/images/page-small.png"> ' . $tmppages[0] . $tmppages[1] . '...' . $tmppages[count($tmppages) - 2] . $tmppages[count($tmppages) - 1];

if (!empty($modSettings['enableAllMessages']) && $topic_length < $modSettings['enableAllMessages'])
$pages .= '&nbsp;<a style="margin: 0 1px;" href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0;all">' . $txt['190'] . '</a>';
$pages .= '';
}
else
$pages = '';

// We need to check the topic icons exist... you can never be too sure!
if (empty($modSettings['messageIconChecks_disable']))
{
// First icon first... as you'd expect.
if (!isset($context['icon_sources'][$row['first_icon']]))
$context['icon_sources'][$row['first_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['first_icon'] . '.gif') ? 'images_url' : 'default_images_url';
// Last icon... last... duh.
if (!isset($context['icon_sources'][$row['last_icon']]))
$context['icon_sources'][$row['last_icon']] = file_exists($settings['theme_dir'] . '/images/post/' . $row['last_icon'] . '.gif') ? 'images_url' : 'default_images_url';
}

// And build the array.
$context['topics'][$row['ID_TOPIC']] = array(
'id' => $row['ID_TOPIC'],
'first_post' => array(
'id' => $row['id_msg_first'],
'member' => array(
'name' => $row['first_poster_name'],
'id' => $row['id_first_member'],
'href' => $scripturl . '?action=profile;u=' . $row['id_first_member'],
'link' => ($row['id_first_member'] != 0) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_first_member'] . '">' . $row['first_poster_name'] . '</a>' : $row['first_poster_name'],
),
'time' => timeformat($row['first_poster_time']),
'timestamp' => forum_time(true, $row['first_poster_time']),
'subject' => $row['first_subject'],
'preview' => $row['first_body'],
'icon' => $row['first_icon'],
'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.gif',
'href' => $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0',
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . '.0">' . $row['first_subject'] . '</a>',
'tooltip' => $row['nice_tooltip_first_msg'],
),
'last_post' => array(
'id' => $row['ID_LAST_MSG'],
'member' => array(
'name' => $row['last_poster_name'],
'id' => $row['id_last_member'],
'href' => $scripturl . '?action=profile;u=' . $row['id_last_member'],
'link' => ($row['id_last_member'] != 0) ? '<a href="' . $scripturl . '?action=profile;u=' . $row['id_last_member'] . '">' . $row['last_poster_name'] . '</a>' : $row['last_poster_name'], ),
'time' => timeformat($row['last_poster_time']),
'timestamp' => forum_time(true, $row['last_poster_time']),
'subject' => $row['last_subject'],
'preview' => $row['last_body'],
'icon' => $row['last_icon'],
'icon_url' => $settings[$context['icon_sources'][$row['last_icon']]] . '/post/' . $row['last_icon'] . '.gif',
'href' => $scripturl . '?topic=' . $row['ID_TOPIC'] . ($row['numReplies'] == 0 ? '.0' : '.msg' . $row['ID_LAST_MSG']) . '#msg' . $row['ID_LAST_MSG'],
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . ($row['numReplies'] == 0 ? '.0' : '.msg' . $row['ID_LAST_MSG']) . '#msg' . $row['ID_LAST_MSG'] . '" rel="nofollow">' . $row['last_subject'] . '</a>'
),
'is_new' => $row['is_new'] && !$user_info['is_guest'],
'new_href' => $scripturl . '?topic=' . $row['ID_TOPIC'] . '.msg' . $row['ID_MSG'] . '#new',
'href' => $scripturl . '?topic=' . $row['ID_TOPIC'] . ($row['numReplies'] == 0 ? '.0' : '.msg' . $row['ID_MSG']) . '' . ($row['numReplies'] == 0 ? '' : 'new'),
'link' => '<a href="' . $scripturl . '?topic=' . $row['ID_TOPIC'] . ($row['numReplies'] == 0 ? '.0' : '.msg' . $row['ID_MSG']) . '#msg' . $row['ID_MSG'] . '" rel="nofollow">' . $row['first_subject'] . '</a>',
'is_sticky' => !empty($modSettings['enableStickyTopics']) && !empty($row['isSticky']),
'is_locked' => !empty($row['locked']),
'is_poll' => $modSettings['pollMode'] == '1' && $row['ID_POLL'] > 0,
'is_hot' => $row['numReplies'] >= $modSettings['hotTopicPosts'],
'is_very_hot' => $row['numReplies'] >= $modSettings['hotTopicVeryPosts'],
'is_posted_in' => false,
'icon' => $row['first_icon'],
'icon_url' => $settings[$context['icon_sources'][$row['first_icon']]] . '/post/' . $row['first_icon'] . '.gif',
'subject' => $row['first_subject'],
'pages' => $pages,
'replies' => $row['numReplies'],
'views' => $row['numViews'],
'board' => array(
'id' => $row['ID_BOARD'],
'name' => $row['bname'],
'href' => $scripturl . '?board=' . $row['ID_BOARD'] . '.0',
'link' => '<a href="' . $scripturl . '?board=' . $row['ID_BOARD'] . '.0">' . $row['bname'] . '</a>'
)
);

determineTopicClass($context['topics'][$row['ID_TOPIC']]);
}

mysql_free_result($request);

cache_put_data('latest_topics', $context['topics'], 480);
}
else
$context['topics'] = $temp;

Offline SMFHacks

  • Administrator
  • Hero Member
  • *****
  • Posts: 14551
    • View Profile
Re: Is this the correct way to cache?
« Reply #1 on: June 06, 2018, 12:02:27 pm »
I wouldu get rid of the else
by $context['topics'] = $temp;

Since it will either get from the cache or run the query
Get your Forum Ranked! at https://www.forumrankings.net - find out how your forum compares with others!

Like What I do? Support me at https://www.patreon.com/vbgamer45/

Offline shuban

  • Hero Member
  • *****
  • Posts: 637
    • View Profile
    • Biology Forums - For All Your Science Needs
Re: Is this the correct way to cache?
« Reply #2 on: June 06, 2018, 12:18:05 pm »
Thank you

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
4082 Views
Last post June 15, 2007, 03:17:11 am
by julien
0 Replies
2329 Views
Last post October 31, 2007, 03:43:11 pm
by sasawawa
0 Replies
2500 Views
Last post May 23, 2009, 10:35:41 am
by Paddleducks Forum - Damien
5 Replies
5762 Views
Last post April 02, 2011, 12:34:51 pm
by neforum
2 Replies
2718 Views
Last post August 06, 2016, 01:33:58 am
by shuban

+- Recent Topics

link for users gallery from profile page by SMFHacks
August 20, 2018, 02:40:58 pm

[Mod]Tidy Child Boards 2.0 by SMFHacks
August 20, 2018, 01:32:43 pm

Admin View in Member's Profile of Dates for Accepting of PP and Reg Agreement by SMFHacks
August 18, 2018, 12:03:07 pm

Gallery2 Template Loading Error by SMFHacks
August 18, 2018, 12:02:12 pm

8: Undefined index: description by SMFHacks
August 17, 2018, 01:42:08 pm

It just does not send by SMFHacks
August 16, 2018, 11:03:06 pm

Ad beginning of post by SMFHacks
August 16, 2018, 10:22:58 pm

Agreement Date says NEVER by SMFHacks
August 14, 2018, 04:46:48 pm

HashTags system for SMF 2.0 released by Diego Andrés
August 12, 2018, 02:04:24 pm

Can the Store produce a unique code that you can sell? by SMFHacks
August 11, 2018, 11:43:32 am

Powered by EzPortal