Results 1 to 5 of 5

Thread: phpCache driving me crazy! help needed pls

  1. #1
    Junior Registered
    Join Date
    Apr 2006
    Posts
    3

    phpCache driving me crazy! help needed pls

    I reall don't understand
    I've set up this simple test file (and it works):

    PHP Code:
    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:

    PHP 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

  2. #2
    Administrator Chris's Avatar
    Join Date
    Feb 2003
    Location
    East Lansing, MI USA
    Posts
    7,055
    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?
    Chris Beasley - My Guide to Building a Successful Website[size=1]
    Content Sites: ABCDFGHIJKLMNOP|Forums: ABCD EF|Ecommerce: Swords Knives

  3. #3
    Junior Registered
    Join Date
    Apr 2006
    Posts
    3
    Quote Originally Posted by Chris
    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:

    PHP Code:
    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:

  4. #4
    Junior Registered
    Join Date
    Apr 2006
    Posts
    3
    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

    PHP Code:
    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)

  5. #5
    Administrator Chris's Avatar
    Join Date
    Feb 2003
    Location
    East Lansing, MI USA
    Posts
    7,055
    That is very similar to a more recent article I wrote,

    http://www.websitepublisher.net/arti...-php-database/
    Chris Beasley - My Guide to Building a Successful Website[size=1]
    Content Sites: ABCDFGHIJKLMNOP|Forums: ABCD EF|Ecommerce: Swords Knives

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •