Jump to content

Recommended Posts

Hello,

 

I have just heard from someone that we can actually keep our select query into files so every time we don't have to run the query we can get that result form a file.

I have got this similar link here

 

http://www.phpfreaks.com/forums/index.php?topic=163810

 

But couldn't able to find out the useful resource for this concept. I have following queries.

 

1) How this method works actually.?(pretty :confused: But still want to learn about it)

 

2) is this applicable for only group by/order by queries? if No then how can we pass the particular ids in the text file (or only file, i don't know what we can call in this type of file)

 

Why i want to do that is because if 1000 viewer are accessing a one particular page at time then the speed of the query let say 0.5 sec for each viewer will take then how much time page/query ll take to load?

 

Some confusing random thoughts. Can any one know how to optimize your query? or can anyone tell me how can i optimize my code using files?

 

Thanks in advance.

 

 

Link to comment
https://forums.phpfreaks.com/topic/174677-optimize-my-query-using-file/
Share on other sites

CACHE CACHE CACHE...

 

If you have content dragged from a database that is not going to change that often then cache the page - next time someone requests that page all you need to do is show the cached page instead of hitting the database once more.

Thanks for your reply ToonMariner,

 

Understood. but let say i have following query

 

$sql= mysql_query("SELECT name,bdat,otherinfo from mytable where userid='20'");

 

This query every time is gonig to chage according to the userid then how can i stored this select query in CACHE?

 

and could you please show me how can i store this result into CACHE as well?

MySQL also has a query cache. So if you get the same query running many times it is stored in cache to return a resultset faster.

http://www.databasejournal.com/features/mysql/article.php/3110171/MySQLs-Query-Cache.htm

Thanks for your reply neil,

 

i so sorry i checked your both the links, but i didn't understand anything :(

could you please tell me this in simple way? may be this is simple but just because my concept is not cleared yet(about cache) because of that i am not getting it.

 

 

ok, bit of i am started getting what chacheing is actually after reading this

 

http://www.developertutorials.com/tutorials/php/php-caching/page1.html

 

ok if i have abc.php page where i am showing of my mysql result which is not going to change according to user. so i can create cache for this page.

 

but what content will come here i don't know. can anyone one explain me what ll come one this page and what i need to do here?

 

Thanks

cache.php

<?php
    ob_start(); // start the output buffer
?>

hello world!
<?php

     $cachefile = "abc.php";
     $fp = fopen($cachefile, 'w');    // open the cache file "cache/home.html" for writing
     fwrite($fp, ob_get_contents());  // save the contents of output buffer to the file
     fclose($fp); // close the file
     ob_end_flush(); // Send the output to the browser

$cachefile = "abc.php";
if (file_exists($cachefile)) {
// the page has been cached from an earlier request
include($cachefile); // output the contents of the cache file
exit; // exit the script, so that the rest isnt executed
}

      $cachetime = 5 * 60; // 5 minutes
       // Serve from the cache if it is younger than $cachetime
       if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
         include($cachefile);
         echo "<!-- From cache generated ".date('H:i', filemtime($cachefile))." -->\n";
       exit;
    }

       $cachefile = $reqfilename;
       $cachetime = 5 * 60; // 5 minutes
       // Serve from the cache if it is younger than $cachetime
       if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile))) {
         include($cachefile);
         echo "<!-- Cached ".date('jS F Y H:i', filemtime($cachefile))." -->\n";
         exit;
       }
       ob_start(); // start the output buffer

       $fp = fopen($cachefile, 'w'); // open the cache file for writing
       fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
       fclose($fp); // close the file
       ob_end_flush(); // Send the output to the browser

      $cachefile = $reqfilename;
       // Serve from the cache if it is the same age or younger than the last
       // modification time of the included file (includes/$reqfilename)
       if (file_exists($cachefile) && (filemtime("includes/".$reqfilename)) < filemtime($cachefile)){

         include($cachefile);
         echo "<!-- Cached ".date('H:i', filemtime($cachefile))." -->\n";
         exit;
       }
       ob_start(); // start the output buffer
       $fp = fopen($cachefile, 'w'); // open the cache file for writing
       fwrite($fp, ob_get_contents()); // save the contents of output buffer to the file
       fclose($fp); // close the file

    ?>
       ob_end_flush(); // Send the output to the browser

 

abc.php

hello world // it comes after i run the cache files.

 

bit confusing to me. if i want to run particular query

select info form mytable where active='1'

 

Do i need to put it cache.php?but then actually user want to see abc.php

bit confusing. can anyone check this code and tell me whats actually happening?  abc.php is my actual page.

 

 

 

 

Why try and create your own cache engine when there are so many available?

Each cache file for the users page would have a unique identifier i.e

userId 1, profile1.tpl.cache

userId 2, profile2.tpl.cache

 

Also I do not understand why you would cache such a page. If a users profile was to change then you have no means of resetting the cache other than to delete your cache files. Cache engines can detect this and deal with it in the appropriate fashion. Template caching should be used on files where the data will not change very often. Cache engines usually allow sections of a cache page to become dynamic where data changes often.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.