cakephp:强制卸载/关闭连接

时间:2012-02-10 09:14:39

标签: php cakephp

我在Cake中遇到性能问题,生成图像。问题是当调用控制器函数时,Cake与DB建立连接,并且图像以“sync”加载,等待关闭最后一个连接(每个图像加载图像,等待0.5秒)。

这是我的控制器:

class ThumbnailController extends AppController {
    var $uses       = null;
    var $components = array();
    var $autoRender = false;


    ###########################################################
    # PUBLIC
    ###########################################################

    public function thumb($folder = null, $w=0, $h=0, $filename=null){
        $this->thumbnail("default.jpg", $folder, $w, $h, $filename);
    }

    public function thumbMama($folder = null, $w=0, $h=0, $filename=null){
        $this->thumbnail("iconopersonadh.png", $folder, $w, $h, $filename);
    }

    ###########################################################
    # PRIVATE
    ###########################################################

    private function thumbnail($default_image, $folder = null, $w=0, $h=0, $filename=null){

        session_write_close();

        $img = WWW_ROOT.DS.$folder.DS.$filename;

        if(!is_file($img)){
            $img = WWW_ROOT.DS."img".DS.$default_image;
        }

        $img_info    = pathinfo($img);
                $filename    = $img_info["filename"];
                $extension   = strtolower($img_info["extension"]);
        $tn_filename = "tn-".$filename."-".$w."x".$h.".".$extension;

        switch($extension){
            case "gif":
                header('Content-type: image/gif');
            break;
            case "png":
                header('Content-type: image/png');
            break;
            default:
                header('Content-type: image/jpeg');
            break;
        }

        # COMPROBAMOS SI EXISTE UNA IMAGEN CACHEADA PARA NO GENERAR EL RECORTE AL VUELO
        if(is_file(WWW_ROOT.DS."files".DS."tn".DS.$tn_filename)){
            readfile(WWW_ROOT.DS."files".DS."tn".DS.$tn_filename);
        }else{

            $magic = new Imagick($img);
            $magic->cropThumbnailImage($w, $h);
            $magic->writeImage(WWW_ROOT.DS."files".DS."tn".DS.$tn_filename);
            echo $magic;

            //$this->log($img_info["basename"]." ---> ".$tn_filename, 'thumbnails-'.date("Ym"));
        }
    }

我需要强制CakePHP不加载任何数据库或建立连接,强制该函数只使代码没有“滞后”。

1 个答案:

答案 0 :(得分:0)

如果您将此操作放入(或已经存在)单独的Controller中,只需将以下内容放入其模型中以避免任何数据库操作:

class Thumbnail extends AppModel {

    public $useTable = false; // Don't use any db table

}

有关此问题的详细信息,请参阅the Cookbook