tags:

views:

63

answers:

5

I used the following code, but it is taking time. i want to cache without storing in a text file.

$file = 'cache_toppers.txt';
if (file_exists($file) &&
    filemtime($file) > (time() - $expire)) {
    $records = unserialize(file_get_contents($file));
} else {
   include("kalvidbconnect.php");

    $query = "SELECT * FROM vpfmsttoppers";
    $result = mysql_query($query)
        or die (mysql_error());
    while ($record = mysql_fetch_array($result) ) {
        $records[] = $record;
    }
    $OUTPUT = serialize($records);
    $fp = fopen($file,"w");
    fputs($fp, $OUTPUT);
    fclose($fp);
}

Thanks, Kamatchi.D

A: 

Well, use cache then, for example APC - apc_store()/ apc_fetch()

vartec
Your apc_store() ljnk has a trailing "." which is breaking it.
Oren
+2  A: 

There are some ready to use PHP extensions providing cache functionality. Some of them:

these are the ones I know of, but surely there are many more.

migajek
A: 

Just a thought, not sure, but how about using CouchDB!? Here is a good tutorial on IBM http://www.ibm.com/developerworks/opensource/library/os-php-couchdb/index.html?ca=drs-

jtanmay
A: 

If you don't want to use file-based caching, then one option is to build a wrapper and store it in shared memory,

http://se.php.net/manual/en/ref.sem.php

Maybe APC utilizes the same technique, I don't know, but if you don't want to install PECL-extensions then building your own cache-handling might be an option.

I would however consider caching rendered content to file, since that would put the least amount of load on the server.

jishi
A: 

Depending on a very long list of factors, I'd typically expect trying to unserialize the file to take longer than loading it fresh from the database.

symcbean