Register Members List Search Today's Posts Mark Forums Read

Reply
 
Article Options
Cache System Explanation (datastore)
Logikos
Join Date: Jan 2003
Posts: 2,924

by Logikos Logikos is offline 17 Mar 2006

I'm going to try and explain the datastore system used in vBulltin. This tutorial is based around users who are already familar with PHP and MySQL.
What is Cache?
A memory area where frequently accessed data can be stored for rapid access. (View Definitions). Basicly, you can store information into the database, and grab all this information with just using 1 query. Some may question this and say: "Isn't all information in the database retrived by queires?". Yup, all information in the database is retrived using MySQL queries, though the method used is diffrent when using the datastore system in vBulletin.
I'll show you way using the datastore is a better solution for you and the users who will use your hacks.

Datastore Method

Lets say you want to use a drop down form with 3 options to choose from. Obviously you would need to create alittle script to add, delete, and edit the information; and when you make this script. Where does the info go? Well it would go into the database. Lets call the MySQL table "dropdownoptions" with 3 fields. ID, Name, and Value.

data1.gif
Now everytime you add, edit, or delete your information, it will be changed in the database. Here is an example of the database tree. We have 3 rows of information.
  • ID = (1) Name = (Dog) Value(Dog)
  • ID = (2) Name = (Cat) Value(Cat)
  • ID = (3) Name = (Cow) Value(Cow)
data2.gif

After you insert this information into the database, the next step would be to insert this info in the datastore. You can do that like this.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Let me go ahead and tell you exacly what the above does. What we did was just grab all in the information in the database from the table dropdownmenu. The while() function will loop the information from the begging to the end. While it's getting all this information, It's storing it all into the variable named $variable_array.

Then you need to use the function called build_datastore(). This function requires the following. build_datastore(1, 2). #1 is the name of your datastore item, and number two is the serialize information to store. This function will create a new datastore item with the name and all your info.

The serialize info will look like this...

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

So now we have all our infomation stored in another area of the database, in one row. Heres a picture so you can compair it to the other ones.

data3.gif

Don't let the serialize data freak you out. You don't really NEED to read that data. Though I kinda like doing it, so I'll break it down alittle for you. Here is a tree of the serialize data.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

In PHP it would look like this.

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

So now we have the info stored, lets get it back out in a readable formate. This is the easy part. This is the code I use.


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

This will print the following:
(ID: 1) (Title: Dog) (Value: Dog)
(ID: 2) (Title: Cat) (Value: Cat)
(ID: 3) (Title: Cow) (Value: Cow)

What your doing is simple. The variable $vbulletin->dropmenu holds the information though it's still serialize. The reason why $vbulletin->dropmenu is the variable is because you need to tell vBulletin which datastore row to get. In this case it is dropmenu because remember we stored it in there like this: build_datastore('dropmenu', serialize($variable_array));

Now in order for vBulletin to know about that specific row, you need to add it to the $specialtemplates array. Example:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

So now that you understand where the variable is coming from, we need to unserialize the data, cause it looks all ugly and weird.

So $vbulletin->dropmenu = unserialize($vbulletin->dropmenu); baiscly gets the serialize info and unserialize's it using the function called unserialize() Once that is finished you want to loop the info using a foreach() function. foreach ($vbulletin->dropmenu AS $dropmenu). Doing that is storing the array info into $dropmenu and you can get each one by using the following vaiables. $dropmenu['id'], $dropmenu['title'], and $dropmenu['value'].

The reason why this is better is because vBulletin allready runs one global query to get all the datatore information. So instead of running a query everytime you want to get the drop down information, you just get it from the datastore and save that query. Some may think this is kinda extreme when you can just run a query, though as your site grows; you want to save as many queires as possiable.

If your interested in saving queries and bandwidth. I would suggest taking a look at Trigunflames profile. He has released many hacks to help in these areas.

Copyright 2004-2006 vBHackers.com All Rights Reserved.
This tutorial may not be redistributed in whole or significant part.

Last edited by Logikos; 17 Mar 2006 at 07:24..
Views: 19936
Reply With Quote
Comments
  #2  
Old 17 Mar 2006, 12:53
Princeton's Avatar
Princeton Princeton is offline
 
Join Date: Nov 2001
Real name: Joe Velez
Excellent article!

The only thing I have to say, "Damn, these thumbnail size!"
__________________
Former vBulletin.org Staff Member

Latest Articles:
Liquid Layout = Less Ad Revenue?
How to Monetize Your Site
Improve Web Page Performance
How To Write For The Web


If it needs instructions, there's room for improvement.
Give users what they actually want, not what they say they want. And whatever you do, don't give them new features just because your competitors have them!
Reply With Quote
  #3  
Old 25 Mar 2006, 17:16
-=Sniper=-'s Avatar
-=Sniper=- -=Sniper=- is offline
 
Join Date: May 2002
great, now I can understand how it can be used
Reply With Quote
  #4  
Old 25 Mar 2006, 21:50
akanevsky akanevsky is offline
 
Join Date: Apr 2005
Real name: Anton Kanevsky
Great article. Thank you, LiveWire.
__________________
I can no longer support any of my hacks. Please do not contact me for that. Feel free to create and post new versions of my hacks, as long as you give me credit for the original work.
Reply With Quote
  #5  
Old 16 Apr 2006, 13:40
Developer's Avatar
Developer Developer is offline
 
Join Date: Sep 2005
Real name: Ahmed || Devo.
very, very, great tutorial 'ken'
__________________
Developer,,
"Know thing about everything ... Know everything about thing"

I was here as a coder, and i am there as a student
Reply With Quote
  #6  
Old 13 Sep 2006, 13:45
Wild-Wing Wild-Wing is offline
 
Join Date: Dec 2005
huh now i get what the a:3 means its the size of the array. damn i feel dumb now.
__________________
I dont code for the hell of it:rambo:
Reply With Quote
  #7  
Old 07 Jan 2007, 11:38
netwind netwind is offline
 
Join Date: Feb 2005
Nice. Is it useful write my own items to file-based datastore cache?
If yes, how I can do this ?
Reply With Quote
  #8  
Old 12 Jan 2007, 01:00
SDB SDB is offline
 
Join Date: Feb 2004
right..

I'm being thick here, clearly :

I want to show $numbermembers and $totalposts at the top of each page of my forum, from within the header template.

As these two values are not available unless you've already parsed the forumhome stats, I've successfully cached these values just after they are calculated at the end of index.php.

I have added it to special templates on the index.php page.

The problem I'm having is getting the data back out again, and into a useable format so I can insert it into the template.

I'm missing a step somewhere..

If I stick $vbulletin->cachedstats into the header template I see the full serialised data in there, but I can't work out from the examples exactly how (and probably more importantly WHERE) to unserialise the data and turn it into eval variables that I can use in the header template.

If anyone could give me a bit of code and tell me where to put it, I would be eternally greatful!

Thanks in advance.

Simon
Reply With Quote
  #9  
Old 12 Jan 2007, 08:59
netwind netwind is offline
 
Join Date: Feb 2005
You need $vbulletin->cachedstats = unserialize($vbulletin->cachedstats);
for all data (except loaded from file based datastore in my case).
store full parsed html code for maximum speed.
If not, use like this in templates :
Total users : {$vbulletin->cachedstats[totalusers]}
Reply With Quote
  #10  
Old 12 Jan 2007, 09:30
SDB SDB is offline
 
Join Date: Feb 2004
Thank you netwind.

Could you please tell me where I can put the unserialise code?

I've tried lots of places, but wherever I'm placing it seems to be either before the cache memory is populated, or after the header has been built.

cheers
Reply With Quote
  #11  
Old 12 Jan 2007, 10:04
netwind netwind is offline
 
Join Date: Feb 2005
Read beginning from words
Now in order for vBulletin to know about that specific row, you need to add it to the $specialtemplates array.
Try parse_templates hook.
Reply With Quote
  #12  
Old 12 Jan 2007, 10:35
SDB SDB is offline
 
Join Date: Feb 2004
LOL

I'd already added it to $specialtemplates, as stated in my original post

Thank you very much for your help, the parse_templates hook worked a treat.

The problem I have now, is having to manually add 'cachestats' to the special templates array of every single page. Is there a way to maybe add to this array from within global.php or something?

Thanks again

Simon

aah..

answered my own question (wonders will never cease!)

I've added :
$specialtemplates[] = 'cachestats';

to the top of the global.php file which works. So will play around to find the right hook to put it in.

--

additional :
It seems that there isn't a hook early enough in global.php
I'm really playing around in the dark here, so if anyone knows of a hook which runs early enough, could you please let me know?

Cheers
Simon

Last edited by SDB; 12 Jan 2007 at 10:47. Reason: Automerged Doublepost
Reply With Quote
  #13  
Old 12 Jan 2007, 10:46
netwind netwind is offline
 
Join Date: Feb 2005
Hook global_start for $specialtempates[] .=
and parse_templates (and later called specific hooks) for unserialize and other jobs
Reply With Quote
  #14  
Old 12 Jan 2007, 10:47
SDB SDB is offline
 
Join Date: Feb 2004
LOL

what a muppet.. thanks mate

correction..

global_start appears to be way too late. There are two that seem to run earlier than this, and neither of those are early enough either.

ok.. I've whacked the $specialtemplates[] = ... into global_start..

but the variable is empty when the header template is built.

when I had identical set-up, but instead of global_start, I hacked the line into the start of the global.php file it worked fine.

Last edited by SDB; 12 Jan 2007 at 10:52. Reason: Automerged Doublepost
Reply With Quote
  #15  
Old 12 Jan 2007, 11:01
netwind netwind is offline
 
Join Date: Feb 2005
Sorry, this my tip was for custom stand-alone scripts like this :

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

I think modifying global.php only one way..
Reply With Quote
Reply

Similar Article
Article Author Type Replies Last Post
Board Optimization datastore cache to XCache - boost your vBulletin phpxcache vBulletin 3.6 Add-ons 243 09 Jan 2010 20:36
Cache the datastore in the filesystem Stadler vBulletin 3.0 Beta Releases 61 01 Feb 2006 08:11



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Article Options

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


New To Site? Need Help?

All times are GMT. The time now is 19:03.

Layout Options | Width: Wide Color: