News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Difference between cache_get_data and cache_put_data?

Started by Biology Forums, June 22, 2018, 01:46:28 PM

Previous topic - Next topic

Biology Forums

When $context['topics'] is empty, that's what it's programmed to show.

if (empty($context['topics']))
echo 'No messages';


I'm not sure why the darn thing isn't working the way I want it to work since I did exactly what @vbgamer said.

// Let's cache the results to load faster...
if (($context['topics'] = cache_get_data('latest_topics', 25)) === 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);

if (!empty($modSettings['cache_enable']))
cache_put_data('latest_topics', $context['topics'], 25);
}


When I delete the cache from APC it works for that amount of time, set to 25 secs, then the message thereafter appears.

Arantor

And as I pointed out, what vbgamer suggested would have made no difference.

So, here's the $64,000 question: how about you var_dump *exactly* what is being cached just before it is cached? I rather suspect what is getting put into the cache is not the value you think it is.

Biology Forums

Thanks for the idea, it dumped exactly what the array creates, and it's huge. Here's a truncated version of it of the dump looks like. This is *without* the caching.

[1842378]=>
  array(22) {
    ["id"]=>
    string(7) "1842378"
    ["first_post"]=>
    array(11) {
      ["id"]=>
      string(7) "4707234"
      ["member"]=>
      array(4) {
        ["name"]=>
        string(5) "derro"
        ["id"]=>
        string(6) "815703"
        ["href"]=>
        string(60) "https://biology-forums.com/index.php?action=profile;u=815703"
        ["link"]=>
        string(80) "derro"
      }
      ["time"]=>
      string(11) "An hour ago"
      ["timestamp"]=>
      int(1529773707)
      ["subject"]=>
      string(95) "Products that belong to the category of "game changers" are considered as ________ innovations."
      ["preview"]=>
      string(131) "Products that belong to the category of "game changers" are considered as ________ innovations.A) new productB) line extensionsC..."
      ["icon"]=>
      string(6) "accept"
      ["icon_url"]=>
      string(64) "https://biology-forums.com/Themes/default/images/post/accept.gif"
      ["href"]=>
      string(52) "https://biology-forums.com/index.php?topic=1842378.0"
      ["link"]=>
      string(162) "Products that belong to the category of "game changers" are considered as ________ innovations."
      ["tooltip"]=>
      string(410) "
Products that belong to the category of "game changers" are considered as ________ innovations.
Products that belong to the category of "game changers" are considered as ________ innovations.
A) new product
B) line extensions
C) continuous
D) dynamically continuous
E) discontinuous
"
    }
    ["last_post"]=>
    array(10) {
      ["id"]=>
      string(7) "4707240"
      ["member"]=>
      array(4) {
        ["name"]=>
        string(9) "proshendo"
        ["id"]=>
        string(6) "815706"
        ["href"]=>
        string(60) "https://biology-forums.com/index.php?action=profile;u=815706"
        ["link"]=>
        string(84) "proshendo"
      }
      ["time"]=>
      string(11) "An hour ago"
      ["timestamp"]=>
      int(1529773707)
      ["subject"]=>
      string(95) "Products that belong to the category of "game changers" are considered as ________ innovations."
      ["preview"]=>
      string(15) "Ma pleasure... "
      ["icon"]=>
      string(2) "xx"
      ["icon_url"]=>
      string(60) "https://biology-forums.com/Themes/default/images/post/xx.gif"
      ["href"]=>
      string(72) "https://biology-forums.com/index.php?topic=1842378.msg4707240#msg4707240"
      ["link"]=>
      string(197) "Products that belong to the category of "game changers" are considered as ________ innovations."
    }
    ["is_new"]=>
    bool(true)
    ["new_href"]=>
    string(65) "https://biology-forums.com/index.php?topic=1842378.msg4707240#new"
    ["href"]=>
    string(64) "https://biology-forums.com/index.php?topic=1842378.msg4707240new"
    ["link"]=>
    string(197) "Products that belong to the category of "game changers" are considered as ________ innovations."
    ["is_sticky"]=>
    bool(false)
    ["is_locked"]=>
    bool(false)
    ["is_poll"]=>
    bool(false)
    ["is_hot"]=>
    bool(false)
    ["is_very_hot"]=>
    bool(false)
    ["is_posted_in"]=>
    bool(false)
    ["icon"]=>
    string(6) "accept"
    ["icon_url"]=>
    string(64) "https://biology-forums.com/Themes/default/images/post/accept.gif"
    ["subject"]=>
    string(95) "Products that belong to the category of "game changers" are considered as ________ innovations."
    ["pages"]=>
    string(0) ""
    ["replies"]=>
    string(1) "3"
    ["views"]=>
    string(1) "0"
    ["board"]=>
    array(4) {
      ["id"]=>
      string(2) "82"
      ["name"]=>
      string(9) "Marketing"
      ["href"]=>
      string(47) "https://biology-forums.com/index.php?board=82.0"
      ["link"]=>
      string(71) "Marketing"
    }
    ["class"]=>
    &string(0) ""
    ["extended_class"]=>
    &string(0) ""
  }


On the contrary, when "No Messages" appears, it dumps nothing, as suspected.

Arantor

So that's the array that's getting put into the cache_put_data() call, right?

Next question: what's the value of $modSettings['cache_enable'] ?


Arantor

OK, so you've verified what the cache puts in when it does so, and you've checked it actually hits APC.

Now, let's try another interesting test. Move the $temp = cache_get_data(...) outside of the if so it runs every time (and change the if to if($temp === null) or whatever it's currently called, I gave up trying to follow what you've been doing to it)... then before the if starts, var_dump what you get out of the function.

GigaWatt

So this is what debugging in PHP looks like, huh :). Not that different from software debugging.

Can break points be added?

@Study Force: Sorry for asking and kind of ruining the topic :-\.
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

Arantor

Breakpoints? On a development server, sure, XDebug has all that, just need an IDE that can point to it - Eclipse for example.

If you're not on a development server, debug by spitting out all the variables is the way to do it.

Of course it's not that different from software debugging... it's EXACTLY software debugging, seeing how PHP is Turing complete and therefore qualifies as a proper language.

GigaWatt

Hmmm... thanks for the info ;).

It's kind of hard when you're tweaking stuff and something goes wrong and you have no idea what actually did go wrong :D.
"This is really a generic concept about human thinking - when faced with large tasks we're naturally inclined to try to break them down into a bunch of smaller tasks that together make up the whole."

"A 500 error loosely translates to the webserver saying, "WTF?"..."

Biology Forums

I did as you said:

$temp = cache_get_data('latest_topics', 480);

var_dump($temp);

// Let's cache the results to load faster...
if ($temp === null)
{

$request = db_query("
SELECT $select_clause
FROM {$db_prefix}topics AS t ...


I get bool(false) when I var_dump $temp.

@GigaWatt: It's fine.

Arantor

Interesting.

The cache routines, assuming unmodified, return null on cache miss, so it looks like you're trying to cache a value that can't be cached, or that you're caching false...


Advertisement: