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
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