Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySql queries #9

Closed
tkaratug opened this issue May 10, 2013 · 2 comments
Closed

MySql queries #9

tkaratug opened this issue May 10, 2013 · 2 comments

Comments

@tkaratug
Copy link

I couldn't cache mysql queries. Can you show an example pls?

@khoaofgod
Copy link
Member

You "can't" cache "a query", bro - BUT you "can" cache "a result" of query.

// Get from cache first
$result = phpFastCache::get("name");

// IF nothing in cache
if($result == null) {
    $query = mysql_query("SELECT * FROM `abc` WHERE 1");
    $result = mysql_fetch_array($query);

    // write to cache, and use it on next 600 seconds - Your PHP will not send query until cache is expired.
    phpFastCache::set("name",$result,600);
}

return $result or print_r($result);
// Get from cache first
$result = phpFastCache::get("name");

// IF nothing in cache
if($result == null) {
    // Actually, this part will be put in function, not like this.  
    $query = mysql_query("SELECT * FROM `abc` WHERE 1");
    $result = array();
    while($ht = mysql_fetch_array($query)) {
           $result[] = $ht;
    }

    // write to cache, and use it on next 600 seconds - Your PHP will not send query until cache is expired.
    phpFastCache::set("name",$result,600);
}

return $result or print_r($result);

@tkaratug
Copy link
Author

Sorry for my English. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants