Facebook  Twitter 

SMFHacks.com

+-

SMFHacks.com

+- User Information

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

+- Forum Stats

Members
Total Members: 4255
Latest: andreios
New This Month: 3
New This Week: 1
New Today: 0
Stats
Total Posts: 43259
Total Topics: 7518
Most Online Today: 297
Most Online Ever: 2482
(April 09, 2011, 07:02:45 pm)
Users Online
Members: 0
Guests: 294
Total: 294

Author Topic: Caching assistance  (Read 4287 times)

0 Members and 1 Guest are viewing this topic.

Offline shuban

  • Hero Member
  • *****
  • Posts: 665
    • View Profile
    • Biology Forums
Caching assistance
« on: December 19, 2016, 11:15:59 pm »
Hey there,

I am placing this code inside Subs.php. How would I cache it so that it doesn't load every time? In past I used the function cache_get_data. Any assistance would be appreciated.

Code: [Select]
$dbresult = db_query("
SELECT subject, article_id
FROM {$db_prefix}blog_articles
ORDER BY article_id DESC
LIMIT 5", __FILE__, __LINE__);

//Create an empty array
$data = array();

//Place database findings into the array
while ($row = mysql_fetch_assoc($dbresult))
$data[] = $row;

mysql_free_result($dbresult);

shuffle($data);

$context['blog_lines'] = '';

foreach($data as $data)
$context['blog_lines'] .= '<a href="http://biology-forums.com/index.php?article='.$data['article_id'].'">'.$data['subject'].'</a>*';

// Get some news...
$context['blog_lines'] = explode("*", str_replace("\r", '', trim(addslashes($context['blog_lines']))));

$context['fader_blog_lines'] = array();

for ($i = 0, $n = count($context['blog_lines']); $i < $n; $i++)
{
if (trim($context['blog_lines'][$i]) == '')
continue;

// Clean it up for presentation ;).
$context['blog_lines'][$i] = stripslashes(trim($context['blog_lines'][$i]));

// Gotta be special for the javascript.
$context['fader_blog_lines'][$i] = strtr(addslashes($context['blog_lines'][$i]), array('/' => '\/', '<a href=' => '<a hre" + "f='));
}

Offline SMFHacks

  • Administrator
  • Hero Member
  • *****
  • Posts: 16436
    • View Profile
Re: Caching assistance
« Reply #1 on: December 20, 2016, 12:50:54 pm »
try this
Code: [Select]
if (($data = cache_get_data('rand_blog_articles', 90)) == null)
{
$dbresult = db_query("
SELECT subject, article_id
FROM {$db_prefix}blog_articles
ORDER BY article_id DESC
LIMIT 5", __FILE__, __LINE__);

//Create an empty array
$data = array();

//Place database findings into the array
while ($row = mysql_fetch_assoc($dbresult))
$data[] = $row;

mysql_free_result($dbresult);

shuffle($data);
cache_put_data('rand_blog_articles', $data, 90);
}
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/
Useful Useful x 1 View List

Offline shuban

  • Hero Member
  • *****
  • Posts: 665
    • View Profile
    • Biology Forums
Re: Caching assistance
« Reply #2 on: December 20, 2016, 04:25:02 pm »
Thanks, SMFHacks, but it didn't work. I did the following:

Code: [Select]
if (($data = cache_get_data('rand_blog_articles', 90)) == null)
{
$dbresult = db_query("
SELECT subject, article_id
FROM {$db_prefix}blog_articles
ORDER BY article_id DESC
LIMIT 5", __FILE__, __LINE__);

//Create an empty array
$data = array();

//Place database findings into the array
while ($row = mysql_fetch_assoc($dbresult))
$data[] = $row;

mysql_free_result($dbresult);

shuffle($data);

cache_put_data('rand_blog_articles', $data, 90);

$context['blog_lines'] = '';

foreach($data as $data)
$context['blog_lines'] .= '<a href="http://biology-forums.com/index.php?article='.$data['article_id'].'">'.$data['subject'].'</a>*';

// Get some news...
$context['blog_lines'] = explode("*", str_replace("\r", '', trim(addslashes($context['blog_lines']))));

$context['fader_blog_lines'] = array();

for ($i = 0, $n = count($context['blog_lines']); $i < $n; $i++)
{
if (trim($context['blog_lines'][$i]) == '')
continue;

// Clean it up for presentation ;).
$context['blog_lines'][$i] = stripslashes(trim($context['blog_lines'][$i]));

// Gotta be special for the javascript.
$context['fader_blog_lines'][$i] = strtr(addslashes($context['blog_lines'][$i]), array('/' => '\/', '<a href=' => '<a hre" + "f='));
}
}

Any other suggestions?

Offline SMFHacks

  • Administrator
  • Hero Member
  • *****
  • Posts: 16436
    • View Profile
Re: Caching assistance
« Reply #3 on: December 20, 2016, 04:39:31 pm »
All this needs to be outside the }

Code: [Select]
$context['blog_lines'] = '';

foreach($data as $data)
$context['blog_lines'] .= '<a href="http://biology-forums.com/index.php?article='.$data['article_id'].'">'.$data['subject'].'</a>*';

// Get some news...
$context['blog_lines'] = explode("*", str_replace("\r", '', trim(addslashes($context['blog_lines']))));

$context['fader_blog_lines'] = array();

for ($i = 0, $n = count($context['blog_lines']); $i < $n; $i++)
{
if (trim($context['blog_lines'][$i]) == '')
continue;

// Clean it up for presentation ;).
$context['blog_lines'][$i] = stripslashes(trim($context['blog_lines'][$i]));

// Gotta be special for the javascript.
$context['fader_blog_lines'][$i] = strtr(addslashes($context['blog_lines'][$i]), array('/' => '\/', '<a href=' => '<a hre" + "f='));
}
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: 665
    • View Profile
    • Biology Forums
Re: Caching assistance
« Reply #4 on: December 20, 2016, 05:26:07 pm »
Working now!

How does performance get affected if I increase 90 to 150? Is it better for the server the higher the time set?

Offline SMFHacks

  • Administrator
  • Hero Member
  • *****
  • Posts: 16436
    • View Profile
Re: Caching assistance
« Reply #5 on: December 20, 2016, 05:29:45 pm »
generally yes but in this case doesn't matter as the query is only run every 90 seconds max... Only matters if the query takes a long time to run.
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/
Agree Agree x 1 View List

Offline shuban

  • Hero Member
  • *****
  • Posts: 665
    • View Profile
    • Biology Forums
Re: Caching assistance
« Reply #6 on: December 20, 2016, 05:34:39 pm »
Thank you, solved.

 

Related Topics

  Subject / Started by Replies Last post
0 Replies
2963 Views
Last post April 16, 2007, 08:00:49 pm
by vdubbia
0 Replies
3423 Views
Last post May 21, 2009, 06:57:19 am
by remi_naija
14 Replies
8175 Views
Last post October 29, 2013, 08:10:53 am
by ardvark
8 Replies
6105 Views
Last post February 27, 2015, 09:58:20 am
by SMFHacks
1 Replies
3091 Views
Last post October 30, 2015, 11:13:23 pm
by nend

+- Recent Topics

No thumbnails on new uploads by SMFHacks
March 27, 2024, 02:10:41 pm

Display the Contact Page for guests by SMFHacks
March 27, 2024, 10:55:43 am

is it possible to add support for odysee.com by fvlog19
March 21, 2024, 08:47:51 am

Request for admin notification by davejo
March 10, 2024, 01:31:59 am

I need help with torrent upload by Ineedsmfhelp
March 09, 2024, 10:01:13 pm

an idea for new mod (( content type with different display )) by SMFHacks
February 27, 2024, 01:36:27 pm

[Mod] RSS Feed Poster by SMFHacks
February 27, 2024, 11:57:18 am

find duplicate pictures by fvlog19
February 14, 2024, 02:22:40 pm

Error uploading video. by SMFHacks
February 08, 2024, 02:04:16 pm

Gallery icon as last added image by fvlog19
February 01, 2024, 01:04:56 pm

Powered by EzPortal