vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 2.x Full Releases (https://vborg.vbsupport.ru/forumdisplay.php?f=4)
-   -   Homepage Statistics Cache (https://vborg.vbsupport.ru/showthread.php?t=51163)

Tigga 04-03-2003 10:00 PM

Homepage Statistics Cache
 
A little while ago I ran into this problem... I like having a lot of statistics on my forum's homepage, but I don't like having a lot of queries on that page. That got me to start thinking of a better way to do it, hence the creation of this hack.

How does this hack work?
It's pretty simple really. Instead of counting every single thread, post, member and etc every time someone loads your forum, it only counts these after a specified amount of time and updates one table that the information will be pulled from. This hack is especially useful for high traffic sites and can reduce the time it takes to load your forum's homepage.

Statistics Included:
Total Threads
Threads Today
Total Posts
Posts Today
Total Members
Newest Member
Top Poster
Top Thread Starter
If you are an admin, it will show you the last time the stats were updated as well.

(Please note that some of these started as hacks from other members at vB.org. Most of them are so simple though I don't see much of a point in trying to figure out which one's I got from here X months ago. If your hack was included here and you would like credit given please post here and I will add it.)

Normally these stats would add 8 queries to your forum's homepage. With this hack installed it will only perform 1 query most of the time, and 9 queries when it needs to update the information. I have found this very useful to cut down on the number of queries on my homepage, decrease the page's loading time, and put less overall stress on my server.

Well that's about it. I hope some others will find this hack useful and if anyone has some suggestions for other stats they would like to include in the stats cache please let me know and I will try to implement them.

Tigga 04-03-2003 10:56 PM

There's not much to it, but here's a screen shot as well. :)

Lethal 04-04-2003 01:08 AM

looks nice, ill try this out ty

SgtSling 04-04-2003 02:04 AM

this also makes my last post on forum home hack update every ten mins along with the rest of the stuff you included above.
Is there anyway I can change this?

SgtSling 04-04-2003 02:05 AM

clicksm install

SgtSling 04-04-2003 02:18 AM

nevermind.. my fault
It saves 8 queries per load for me

SgtSling 04-04-2003 02:26 AM

How can we also cache the moderatorlist ?
XENON has a hack for this.. but it isn't compatable with this hack
https://vborg.vbsupport.ru/showthrea...=&pagenumber=1

Tigga 04-04-2003 03:02 AM

Thanks for pointing that out SgSling. I had forgotten about his modifications (I have Xenon's hack installed too). He saves another query by counting the posts in the thread query, so to use that with this hack you would just replace this (in the hack itself, or index page if you've already modified it):
PHP Code:

$countthreads=$DB_site->query_first('SELECT COUNT(*) AS threads FROM thread');

// get total posts
$countposts=$DB_site->query_first('SELECT COUNT(*) AS posts FROM post');
$totalposts=number_format($countposts['posts']); 

With:
PHP Code:

// get total posts
$counters=$DB_site->query_first('SELECT SUM(replycount) AS posts, SUM(threadcount) AS threads FROM forum WHERE parentid=-1');
$countposts[posts]=$counters['posts'];
$countthreads[threads]=$counters['threads']; 

That should be the only conflict with the 2 hacks, and changing that saves yet another query when it updates the cache. :)

Snapperhaed 04-04-2003 03:08 AM

Lets say I wanted to add a few more items to be cached ...

1 - Total Thread Views
2 - New Members Today
3 - Most popular thread by # of Replies

These 3 items I have installed, but they dont show now since I added this hack. (A nice one might I add).

I know (or i think rather) I need to add them into the table via a query, then adjust accordingly on the forumhome template ... But since im new to it, (i already ran the intial query), how can I add to the exist newly created table? (IE: add this to the statscache table) ...

Any/All help appreciated!!

Tigga 04-04-2003 03:13 AM

Snapperhaed - The first 2 wouldn't be hard to add at all. If you'll give me a link to the 3rd one you're talking about I'll have a look at it to see what would need to be done to add it as well. :)
Oh, and those things should still work fine without the stats cache as long as you didn't modify any of the code that was for those hacks...

Snapperhaed 04-04-2003 03:17 AM

Tigga, #3 can be found at:
https://vborg.vbsupport.ru/showthrea...threadid=25755

Thanks!! I'll also peep to see what I may have altered. :D

Tigga 04-04-2003 03:53 AM

Well here's the first two for you. The 3rd looks like it would take a little more work so I'll have to post that for you tomorrow.

For members today and total thread views, first run this query via phpMyAdmin:

ALTER TABLE `statscache` ADD `threadviews` INT( 10 ) UNSIGNED DEFAULT '0' NOT NULL ;
ALTER TABLE `statscache` ADD `memberstoday` INT( 10 ) UNSIGNED DEFAULT '0' NOT NULL ;

Then open your index.php file (assuming you've already installed the hack) and look for:
PHP Code:

$getstats[posttoday]=number_format($getstats['posttoday']); 

Below that Add:
PHP Code:

$getstats[threadviews]=number_format($getstats['threadviews']);
$getstats[memberstoday]=number_format($getstats['memberstoday']); 

Then look for:
PHP Code:

$poststoday=$getpoststoday[count]; 

Below that Add:
PHP Code:

// members today
$getmemstoday=$DB_site->query_first("SELECT count(*) AS count FROM user WHERE joindate>='$datecut'");

// thread views
$getthreadviews=$DB_site->query_first("SELECT SUM(views) AS tviews FROM thread"); 

Then look for:
PHP Code:

posttoday='$poststoday'lastupdate='".time()."'"); 

Replace that with:
PHP Code:

posttoday='$poststoday'lastupdate='".time()."'threadviews='$getthreadviews[tviews]',memberstoday='$getmemstoday[count]'"); 

Then just add the variables $getstats[threadviews] and $getstats[memberstoday] in your forumhome template where you would like those to appear. (Remember it will take 10 minutes, or however long you set between updates, before those will appear.)

Snapperhaed 04-04-2003 04:01 AM

Thank You, Thank You, Thank You!!

Installed and worked perfectly. Many thanks for the assistance!
As a side note, I checked to make sure none of the original code was altered, before install, and it wasnt. But soon as I made the changes you provided, cured the problem right up!

Again, Thank you! Your the greatest. :p

Logician 04-04-2003 04:41 AM

caching such stats are always a good idea, especially in forum home, so very good job! :)

Tigga 04-04-2003 04:49 AM

Snapperhaed - No problem at all. I've added those stats to my forum page as well. :)
By the way, I noticed that you have mYvBindex installed on your site. You can use the same code there to pull the info and save 4 queries on your homepage (and add more stats) as well. ;)

Logician - Thank you. :D

mossyuk 04-04-2003 06:21 AM

1997 PMs?!?! Is that in total or just yours? Wow :)

Boofo 04-04-2003 11:33 AM

Ok, I got the other 2 figured out (finally). This is what I have left. Can anyone please tell me how to incorporate this, too?

Quote:

//Page Counter Code
$mycounter = $DB_site->query_first("SELECT count FROM mycounter");
$DB_site->query("UPDATE mycounter SET count = count + 1");
$mycounter = number_format($mycounter['count']);
//Page Counter Code

Kars10 04-04-2003 11:50 AM

Works like a charm and saves me about 5 Querys on Forumhome / and vBindex!!

Thanks
Kars

[high]* Kars10 kicks install! :)[/high]

Tigga 04-04-2003 04:23 PM

mossyuk - Yep, those are all mine. The board's been up for a little over a year and I rarely delete anything, so I guess they add up pretty quickly. :)

Boofo - I don't think that's something that should really be integrated with this... Since it's a page counter, updating it only every ten minutes wouldn't allow it to be accurate. It appears that it would only be 2 very small queries and a small table though, so it shouldn't affect performance much at all.

Kars10 - Glad you like it. :)

Boofo 04-04-2003 04:31 PM

You're right. I got to thinking about it after I wrote the message. I went from 31 queries down to 23 (and at 35 when the cache updates) and I have a couple of more stats than I had before. I did the Top Profile Views with this, too, if anyone is interested. ;)

Lethal 04-04-2003 09:53 PM

did u update the attachment with the add-ons?

Lethal 04-04-2003 09:53 PM

great hack, saves me 7 queries nice

Austin Dea 04-05-2003 02:37 AM

Very nice hack, man. Here's updated instrucs with the add-ons.

Dean C 04-05-2003 09:46 AM

Awesome idea Tigga :)!

- miSt

Prankster 04-05-2003 10:30 PM

Quote:

04-04-03 at 11:53 PM Lethal said this in Post #22
great hack, saves me 7 queries nice

[high]* Prankster too
[/high]

great idea
thanks

greets Prankster

Areku 04-11-2003 06:31 PM

OK, what should we do if we have some or all of the old hacks your new hack is based on so we can truly save queries instead of adding 9 new to the 8 old ones? ;)

Tigga 04-11-2003 06:36 PM

If you have all (or some) of these stats installed already, you would pretty much just replace the coding for those stats with the new code for the stats cache. It shouldn't be too hard to figure out what to replace, but if you have problems with it let me know.

Mijae 04-18-2003 04:01 PM

Database error in vBulletin 2.3.0:

Invalid SQL: UPDATE statscache SET members='1617',threads='2145',posts='39246',topthr ead='Mijae',topthreadid='1',to pthreadnumber='186',topposter='Jim',topposterid='1 53',toppostnumber='2496',newes tmember='umar'newestmemberid='1617',threadtoday='0 ',posttoday='7',lastupdate='10 50682471', threadviews='259042',memberstoday='2'
mysql error: You have an error in your SQL syntax near 'newestmemberid='1617',threadtoday='0',posttoday=' 7',lastupdate='1050682471', thr' at line 1

mysql error number: 1064

Tigga 04-18-2003 05:48 PM

It looks like a comma is somehow missing in that query. Make sure you have a comma right before 'newestmemberid' in the query that updates the cache.

Areku 04-23-2003 11:36 AM

Up and running in v2.2.0!!!

Areku 04-23-2003 12:43 PM

Btw,

- how do i change ',' char to '.' char (eg.: 325,500,934 to read 325.500.934)
- how do i know how many queries currently my homepage loads?

Ideas?

Areku 04-23-2003 05:09 PM

DO NOT cache the SINCE YOUR LAST VISIT! hack by MrLister (https://vborg.vbsupport.ru/showthrea...threadid=31957). Those of you who installed the WELCOME HACK [FINAL] are also affected, like me!

:P

Tigga 04-23-2003 08:31 PM

Areku - If you'll refer to a post I made here, that should help you. It's for a different hack, but it should be enough to help you figure out how to change the commas to periods. ;)
As for figuring out how many queries a page has, I would recommend installing Teck's Microstats hack. Alternatly you could open your admin/config.php file and put the code $debug="1"; right at the end of that file before the ?>. Then you would go to the url http://yoursite.com/forum/index.php?explain=1. At the bottom of that page it will show you the number of queries and the time it took to execute them.

Areku 04-24-2003 10:32 AM

Does any1 know what's wrong with the SINCEYOURLASTVISIT addon I published 2 posts ago (https://vborg.vbsupport.ru/showthrea...005#post386005)?

Yesterday was working fine but today it displays Han habido 5,770 hilos y 33,486 mensajes desde tu ?ltima visita!

that is, 33 thousand messages since my last visit, 30 seconds ago!!!!


Wth is going wrong??

Tigga 04-24-2003 04:40 PM

Yea, actually I just realized what you did with the new posts since a users last visit. You should not have done that... Every time it updates the cache, it will display the number of new posts for that particular user, so all users will see the same number of new posts and threads. There's really not a way to cache those since they are specific for each member.

Areku 04-24-2003 09:10 PM

Damn, I really messed it up! :P

OK I'll edit the previous post.

Can you now explain me when does the update happen actually?

It's 0:01am here (server time) and update has been done for this new day, but it's still counting/displaying yesterday's new members/posts/threads...

Tigga 04-24-2003 09:37 PM

Well actually it should go by the time that is set on the server to determine when a new day starts. Then it should update that table every 10 minutes (assuming you didn't change the value), so within 10 minutes it *should* be correct. I'll work on changing it so that it will go by your forums timeoffset values as well though since that would make a little more sense.

Boofo 04-24-2003 09:45 PM

Quote:

Today at 04:37 PM Tigga said this in Post #37
Well actually it should go by the time that is set on the server to determine when a new day starts. Then it should update that table every 10 minutes (assuming you didn't change the value), so within 10 minutes it *should* be correct. I'll work on changing it so that it will go by your forums timeoffset values as well though since that would make a little more sense.
Couldn't you just change this:

PHP Code:

lastupdate='".time()."' 

to something like this?

PHP Code:

lastupdate='".$ourtimenow."' 


Tigga 04-24-2003 10:23 PM

Bofo - Actually the only thing that would affect is where it shows an admin the last time the cache was updated. The $datecut variable is what would need to be modified. ;)

Areku 04-25-2003 08:53 AM

I later realized you're right... server is on PST and I'm on GMT so it used server's time to update, presenting us "today registered users" when that was not true for our time zone ;)

Will wait for your fix then ;)


All times are GMT. The time now is 06:55 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01392 seconds
  • Memory Usage 1,838KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (10)bbcode_php_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (40)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete