0

我这里有个小问题..让我介绍一下...

我有一个创建简码的脚本.. 这是脚本..

//shortcode stock
function stok($atts, $content = null) {  
    extract(shortcode_atts(array(  
        "id" => 'http://net.tutsplus.com'  
    ), $atts));  
    $result = mysql_query("SELECT SUM(quantity) AS value_sum FROM wp_wpsc_cart_contents WHERE prodid = '".$id."'"); 
    $row = mysql_fetch_assoc($result); 
    $stock = '<center><br><b>STOK TERJUAL :</b><div class="nscountdown"><table><thead><tr><td>'.$row['value_sum'].'</td></tr></thead></table></div></center>';
    return $stock;
}  
add_shortcode("stok", "stok");  

如您所见,有一个查询对列求和

$result = mysql_query("SELECT SUM(quantity) AS value_sum FROM wp_wpsc_cart_contents WHERE prodid = '".$id."'"); 

不..问题是..如何缓存结果,因为该查询使我的服务器平均负载变得尖峰...

有什么解决办法??

之前谢谢....

4

1 回答 1

0

可能瞬态 API是您所需要的。

function stok($atts, $content = null) {  
    extract(shortcode_atts(array(  
        "id" => 'http://net.tutsplus.com'  
    ), $atts));  

    if ( false === ( $query_cache = get_transient( md5($id) ) ) ) {
        $result = mysql_query("SELECT SUM(quantity) AS value_sum FROM wp_wpsc_cart_contents WHERE prodid = '".$id."'"); 
        $row = mysql_fetch_assoc($result); 
        $stock = '<center><br><b>STOK TERJUAL :</b><div class="nscountdown"><table><thead><tr><td>'.$row['value_sum'].'</td></tr></thead></table></div></center>';

        // set one hour lifetime cache with the name of md5($id)
        set_transient( md5($id), $stock, 60*60*1 );     
        return $stock;
    }
    return $query_cache;
}  
add_shortcode("stok", "stok");  
于 2012-09-19T02:49:12.920 回答