开发者

How to do caching in php

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


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

  • memcache http://pecl.php.net/package/memcache
  • APC http://php.net/manual/en/book.apc.php
  • eAccelerator
  • XCache

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


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-


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜