Hey guys I’m wirting a quick plugin that generates doubleclick script tags for a site I’m working on.
Its fairly startight forward (or I thought it was), basically the plugin stores the ad number in the $SESS variable and then increments it each time the plugin is called.
However I am having an issue when the plugin is called with the same params multiple times.
The count is not being incremented and I’m pretty sure this is due to chaching. I have tried cache=’no’ and cache=’yes’ refresh=’0’ but neither helped.
Any ideas? Here is my method
<pre><code> function advert() { global $TMPL, $IN, $SESS;
if(isset($SESS->cache['lg']) === FALSE)
{
$SESS->cache['lg'] = array();
}
if(isset($SESS->cache['lg'][LG_DC_addon_id]) === FALSE)
{
$SESS->cache['lg'][LG_DC_addon_id] = array(
'ad_count' => array(
'button' => 1,
'leader' => 1,
'box' => 1,
'skyscraper' => 1
),
'ord' => rand(0, time()),
'pos' => 1
);
}
$site = $TMPL->fetch_param('site');
$zone = $TMPL->fetch_param('zone');
$keywords = $TMPL->fetch_param('keywords');
$type = $TMPL->fetch_param('type');
$size = $TMPL->fetch_param('size');
if(isset($SESS->cache['lg'][LG_DC_addon_id]['ad_count'][$type]) === FALSE)
{
$SESS->cache['lg'][LG_DC_addon_id]['ad_count'][$type] = 1;
}
$dimensions = explode('x', $size);
$ret = "cache['lg'][LG_DC_addon_id]['pos']};pos={$type}{$SESS->cache['lg'][LG_DC_addon_id]['ad_count'][$type]};kw={$keywords};sz={$size};ord={$SESS->cache['lg'][LG_DC_addon_id]['ord']}?'" .
" type='text/javascript'>" .
"<noscript><a >cache['lg'][LG_DC_addon_id]['pos']};pos={$type}{$SESS->cache['lg'][LG_DC_addon_id]['ad_count'][$type]};kw={$keywords};sz={$size};ord=[timestamp]?' target='_blank'>" .
"href=cache['lg'][LG_DC_addon_id]['pos']};pos={$type}{$SESS->cache['lg'][LG_DC_addon_id]['ad_count'][$type]};kw={$keywords};sz={$size};ord=[timestamp]?' width='{$dimensions[0]}' height='{$dimensions[1]}' border='0' alt=''>" .
"</a></noscript>";
++$SESS->cache['lg'][LG_DC_addon_id]['pos'];
++$SESS->cache['lg'][LG_DC_addon_id]['ad_count'][$type];
return $ret;
}[/code]
Leevi
try using
srand((double)microtime()*1000000);
$randomnumber = rand(0,100000);
then pass $randomnumber down to the $ret strings insted of
$SESS->cache['lg'][LG_DC_addon_id]['ad_count'][$type]
that will pretty much guarantee you a random number string everytime the plug-in is called
just a idea may not work for you
I think the problem is higher up the food chain. I was outputting a random number to the screen using print(); and when I called the plugin multiple times on the same page the random number was not displayed. This lead me to believe that EE assumed the output would be the same (because the plugin tags are the same) and outputted a cached version of the results.
Anyone from the dev team can confirm this?
So my solution was to add another param to the tag called ‘rand’ where I just added a random string. Not the neatest solution but it worked
That’s right. I’ve run into that problem too, and like you I added a parameter with a random 3-character string. Another thing I discovered by looking at the template parsing code is that if the opening tag contains the string ‘random’ in any fashion it circumvents that caching. E.g.
{exp:plugin:method random=""}
{exp:plugin:method param="random"}
I’m not sure what the purpose of that ‘random’ string check is, so it’s probably safer to use the workaround you’re using. There should probably be a parameter that you can add to any plugin tag to prevent this caching, something like tag_dynamic=”yes”.
Nice score… well random is the way to go then. I’m thinking of using:
{exp:plugin:method random=""}
Although it still will be a little confusing if others need to maintain the code. I also agree with you on tag_dynamic=”yes”, although a simple cache=”no” or cache_output=”no” would be even better.
Cheers
Well the thing is, like I said, I don’t know what the actual purpose of that ‘random’ business is. At the moment it has the effect of circumventing that caching behavior (and apparently no other effect), but since I’m not sure what it’s meant for I’m kind of leery of relying on it. It’s probably safer to just tack on a useless parameter of your choosing with a random value, like you are doing. Then if something changes with ‘random’ in the future it wouldn’t break your code.
If there were going to be a legitimate parameter added to address this, my first choice probably would have been ‘dynamic’, but since that’s already used in various places with a different meaning it’s probably not a good choice. Same goes for ‘cache’. I think ‘cache_output’ would also be confusing since there is such a thing as caching of tag output that’s totally different from this issue.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.