The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
Cache System Explanation (datastore)
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? I'll show you way using the datastore is a better solution for you and the users who will use your hacks. Attachment 44423 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. Attachment 44424 PHP Code:
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... Code:
a:3:{i:0;a:3:{s:2:"id";s:1:"1";s:4:"name";s:3:"Dog";s:5:"value";s:3:"Dog";}i:1;a:3:{s:2:"id";s:1:"2";s:4:"name";s:3:"Cat";s:5:"value";s:3:"Cat";}i:2;a:3:{s:2:"id";s:1:"3";s:4:"name";s:3:"Cow";s:5:"value";s:3:"Cow";}} Attachment 44425 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. Code:
a:3:{ i:0; a:3:{ s:2:"id";s:1:"1"; s:4:"name";s:3:"Dog"; s:5:"value";s:3:"Dog"; } i:1; a:3:{ s:2:"id";s:1:"2"; s:4:"name";s:3:"Cat"; s:5:"value";s:3:"Cat"; } i:2; a:3:{ s:2:"id";s:1:"3"; s:4:"name";s:3:"Cow"; s:5:"value";s:3:"Cow"; } } PHP Code:
PHP Code:
(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: PHP Code:
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. |
#22
|
||||
|
||||
This was a great article and it explained the concept very well. I do have a minor quibble in your example. Rather than name= dog, value=dog, it might help people understand better if the var name and var value were different, e.g. name=dog, value=terrier.
Something I'd add to the discussion is that cron and serialize go hand in hand. The best place to use serialize is when accessing data frequently, therefore using vb's cached datastore w/o adding a query...but the updating of data is relatively infrequent. A classic way to update that data periodically is cron. https://vborg.vbsupport.ru/showthrea...highlight=cron |
#23
|
|||
|
|||
I really hope they add a hook that will allow us to add to $specialtemplates before it's actually used. It's kind of a bummer to have to hack up a file over something so simple.
Not to suggest that adding the hook is simple. I think there's some initialization for the hook system in the datastore, or something along those lines. It's just a pain and it would be cool if they stumble across some fix in the future. |
#24
|
|||
|
|||
Quote:
|
#25
|
|||
|
|||
It's because the query that uses $specialtemplates and populates the datastore variables comes before global_start. I don't really understand why, but I think it has something to do with the hook system not being able to work until the datastore info is available.
|
#26
|
|||
|
|||
Have you tried the init_startup hook? There is another Datastore fetch executed after it I think. This isn't tested but you could maybe try:
Hook Location: init_startup PHP Code:
|
#27
|
|||
|
|||
Awesome, thanks man. I haven't tested it either, but it looks like that should work.
--------------- Added [DATE]1194736320[/DATE] at [TIME]1194736320[/TIME] --------------- Ok, it did work. It added the extra query, but it works. You have to be careful and make sure it's like this though: Code:
$datastore_fetch[] = "'dropmenu'"; |
#28
|
|||
|
|||
Ooops yup you need to put it in quotes because otherwise MySQL will read it as a column name instead of a value.
I'll correct it in my original post. |
#29
|
||||
|
||||
The Function build_datastore also has a 3rd parameter which should be mentioned, as it seems all serialized data in the datastore table requires a value of 1...
PHP Code:
$vbulletin->dropmenu = unserialize($vbulletin->dropmenu); There'fore we can just access the info from the datastore like this: PHP Code:
|
#30
|
||||
|
||||
<font color="darkgreen">What if I have my own class object and I want to build cache acessible from it only and not use the $vbulletin object?
Like ... $myclass->mycache['something'] instead of $vbulletin->mycache['something'] How would I change it so its like that?</font> |
#31
|
||||
|
||||
With a couple tweaks (probably because this is 2 years old...), i've successfully got my first datastore plugin working
Thanks for the write up Ken. Never experienced with the datastore much. |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|