PDA

View Full Version : phpCache driving me crazy! help needed pls



kendothpro
04-11-2006, 06:20 AM
I reall don't understand
I've set up this simple test file (and it works):



include('../dbconnect.php'); //handles db connection
include('../phpcache.php');
if (!($et=cache_all(30))) {
$sql=mysql_query("SELECT MAX(ID) FROM Photos");
$res=mysql_fetch_array($sql);
$total=$res["MAX(ID)"];
echo 'Max ID inside of cache:'. $total.'<br>';
cache_variable("total");
endcache();}
echo 'Max ID outside of cache:'. $total.'<br>';


It outputs the correct values

BUT

I have a functions.php file that I use in my website, and inside one of the functions there's this line of code:


if (!($et=cache_all(30))) {
$sql=mysql_query("SELECT MAX(ID) FROM Photos");
$res=mysql_fetch_array($sql);
$total=$res["MAX(ID)"];
cache_variable("total");
endcache();}


which is basically the same...BUT...$total is NULL outside of the cache (e.g. after this code)
Could anyone explain why on earth the same code produces 2 different results? I'm guessing it's because of variable scope but I really don't see how it would apply here, since we're inside the function already

PS yes, I am including phpcache.php inside the function :P

Chris
04-11-2006, 07:23 AM
I've never used it within a function like that. I've used functions with it... but the caching was always done outside the functions.

I see two possibilities..

1. Total is null because total is null. There is an error unrelated to the caching that causes it to be null within your function (I'd echo it out to test it).

2. It is a variable scope issue. I can't really think of why, but its got to be something with how the function and the included phpcache functions interact. FUnctions declared within functions or something.

Also... usually people use functions when there is a set of code they need to execute repeatedly. Looking at your code It doesn't appear you're using any kind of unique key for the cached data, so are you really using the set of code multiple times? And if not do you really need to use a function to accomplish what you're doing?

kendothpro
04-11-2006, 09:10 AM
I've never used it within a function like that. I've used functions with it... but the caching was always done outside the functions.

I see two possibilities..

1. Total is null because total is null. There is an error unrelated to the caching that causes it to be null within your function (I'd echo it out to test it).

2. It is a variable scope issue. I can't really think of why, but its got to be something with how the function and the included phpcache functions interact. FUnctions declared within functions or something.

Also... usually people use functions when there is a set of code they need to execute repeatedly. Looking at your code It doesn't appear you're using any kind of unique key for the cached data, so are you really using the set of code multiple times? And if not do you really need to use a function to accomplish what you're doing?

That bit of code is actually a piece of the function, the whole being a function that essentially displays photos from the database with a paging system (&page=2 etc) + a variable that is passed to determine the ORDER BY (&ord=ID etc)

I guess my only alternative is to strip out the function from my include and paste it in the actual page, phpcache doesn't appear to like being used inside of a function :/

I've created this simple test:


include('dbconnect.php');
include('phpCache.inc');

function showme() {
if (!($et=cache_all(10))) {
$sql=mysql_query("SELECT MAX(ID) FROM Photos");
$res=mysql_fetch_array($sql);
$total=$res["MAX(ID)"];
echo 'Max ID inside of cache:'. $total.'<br>';
cache_variable("total");
endcache();}

echo 'Max ID outside of cache inside function:'. $total.'<br>';
}

showme();
echo 'Max ID outside of cache:'. $total.'<br>';


and the output is:

Max ID inside of cache:6759
Max ID outside of cache inside function:
Max ID outside of cache:

kendothpro
04-11-2006, 02:45 PM
I moved it into the page itself and it's now working

I'd like to share a technique that for me has very good performance and is actually easier and cleaner to code than the one in chris's article

Instead of caching every single variable that you want to keep (which can become tedious if you have many variables like me) enable output buffering, store all the output html in a variable and cache that single variable!!
all you have to do is



if (!($et=cache_all)) {
ob_start();

***your code here***
***echo something***
***print something else***
***really huge blocks of code can go here***

$output=ob_get_contents();
cache_variable("output");
ob_end_clean();
endcache();}
echo $output;

If you think it has drawbacks just tell me, I'll be happy to listen :)
Just remember it stores the FINAL, PROCESSED html, so don't put here anything you don't really want to cache (like ads, for instance)

Chris
04-11-2006, 05:26 PM
That is very similar to a more recent article I wrote,

http://www.websitepublisher.net/article/html-cache-php-database/