Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

PHP Dynamic Cache

$
0
0

Hey Guys,

 

So Currently i have a website that was my first web development project ( learning how to program and web design ). That now has generated a good amount of traffic and users, and although i am not greatly educated in Computer Science, i am learning my way to getting a better product to my end users.

 

The problem:

 I see my bounce rate is not so well, and that my users are coming to a page that is slow in speed. The website handles images and text that are fed from queries to a database then spit out. 

 

Solution:

My Users were coming on a page, and each user was having to load the same page and data over and over for each user. I learned that using a technique called dynamic caching that i would be able to solve this problem, by storing the query and data, then being able to feed that content one time to all the users who are viewing the site. This would accelerate your web speed dramatically. 

 

So i understand what my problem is and what the solution is. Now my

 

Issue:

 

I don't fully understand how to implement this from what i keep reading and researching on. 

 

I do know your suppose to work with ob() function which would help with buffering on the website.

 

My problem is that from the beginning i was using ob_start() on the header of my website due to redirect issues when i would use header(). To be honest i didn't fully understand why using ob_start solved that issue but now i find myself having problems again with the error of : header has already been sent.

 

I tried looking into platforms like Varnish or phpfastcache which led to some issues due to my lack of OOP progamming skill and was not able to solve this issue.

 

I then tired to create a simple code that would create a cache for the page that the user was on. Store that data in tmp file then look for that file when a user first comes on, if they find that file in say 5 min span load that file, if not load a new one. 

 

All in all i think i am just failing at this due to my lack of understanding the core concept of what i am actually trying to do. 

 

what i have so far:

 

function cache_file() {
    // something to (hopefully) uniquely identify the resource
    $cache_key = md5($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING']);
    $cache_dir = '/tmp/phpcache';
$cache_combo = $cache_dir . '/' . $cache_key; 
    return $cache_combo;
 
}
 
$cache_file = cache_file();
 
echo $cache_file;

die();

 

RESULTING IN :

 

/tmp/phpcache/38bf4baa8867cb2feec947ac3c893fe9

 

 

ISSUE : I AM NOT RETURNING A QUERY STRING, I UNDERSTAND SOMETIMES ONE WONT BE PRESENT AND I AM NOT SURE IF THIS IS EVEN A PROBLEM.

 

So now each page is returning its own $cache_result

 

// if we have a cache file, deliver it
if( is_file( $cache_file = cache_file() ) ) {
readfile( $cache_file );
exit;
} else {

   echo 'NO FILE FOUND START A NEW 1';

}

 

ISSUE : NO FILE IS BEING FOUND SO THE CONDITIONAL IS NOT PASSING AS TRUE FOR is_file()

 

// cache via output buffering, with callback
ob_start( 'cache_output' );

 

ISSUE : UNLESS ob_start() IS THE FIRST THING OFF MY CODE I GET THE HEADER RELOCATE ERROR OF:

 

so if i have something like:

echo cache_file();

ob_start();

resulting in:

 

/tmp/phpcache/38bf4baa8867cb2feec947ac3c893fe9
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/justin/public_html/include/init.php:20) in/home/justin/public_html/include/init.php on line 27

 

 

 

//
// expensive processing happens here, along with page output.
//

 

Now at this point here i am guessing that i need to  return the temp file and the content specific to the data return of the cache. I have been struggling to get the cache to setup correctly i have not even dealt with this part yet. I am somewhat lost on what is meant by expensive processing, i am assuming this is the part that takes all the server speed and processing that is needed for you to host with speed.

 

All feedback and suggestions are very much appreciated, i am not so much looking for a answer per say, i would just like to get a better idea if i am on the right track , or what i am lacking or missing fundamentally or just code wise.

 

thanks guys

 

-Justin7410

 


Viewing all articles
Browse latest Browse all 13200

Latest Images

Trending Articles



Latest Images