PDA

View Full Version : Custom items in VB datastore


MrEyes
09-30-2009, 10:22 AM
Without getting into the whys and wherefores I have coded a page that displays a lot of data that is pulled from the database, this works perfectly and all is good.

However, there is no need for this data to be re-queried on every page view so the obvious solution is the cache the data, when needed pull it from the cache and periodically purge and refresh the cache.

It seems like a good idea to reuse as much of the existing datastore system that already exists in vBulletin. I can store into the cache using:


build_datastore('mycustomdata', serialize($mycustomdata), 1);


However, how do I retrieve from the store?

I did consider putting it into something like $vbulletin->options but this would mean that the data would be loaded in on all pages which is unnecessary and would waste resources.

--------------- Added 1254311069 at 1254311069 ---------------

Problem Solved

To do this you need to give your custom datastore a name, and then pass this name to the $specialtemplates array.

Code speaks a thousand words, so...


<?php
error_reporting(E_ALL & ~E_NOTICE);
define('NO_REGISTER_GLOBALS', 1);
define('THIS_SCRIPT', 'testvbcache');

$phrasegroups = array();
$specialtemplates = array('mycustomdatastoretemplate');
$globaltemplates = array();
$actiontemplates = array();

require_once('./global.php');

if (isset($vbulletin->mycustomdatastoretemplate))
{
//The custom datastore item is available so just echo out the current
//datastore value.
echo 'RETRIEVED: ' . $vbulletin->mycustomdatastoretemplate['arraykey'];
}
else
{
//The custom datastore item is not available so create it and add to
//the datastore
$mycustomdata = array();
$mycustomdata['arraykey'] = 'This is the value of array key';
build_datastore('mycustomdatastoretemplate', serialize($mycustomdata), 1);

echo 'ADDED: ' . $mycustomdata['arraykey'];
}
?>

The part in bold red is the important bit, without this the VB backend will not load in your custom datastore values.

The first time this script is run it will output:


ADDED: This is the value of array key


The second time (and all subsequent calls) it will output


RETRIEVED: This is the value of array key

Lynne
09-30-2009, 02:38 PM
One of these days I will look into all this datastoring cuz I can think of a couple places on my site where it would be very useful to use. Thanks for taking the time out to post this because I'm sure it will help someone (like me!) when they are looking into this same thing.

Andreas
09-30-2009, 04:23 PM
define('NO_REGISTER_GLOBALS', 1);

Unneeded old code - get rid of it :)