最佳化ECStore mongodb大資料 讀寫效率

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   sp   

轉自同功BBS

拆表存取kv
<?php/*經過拆變最佳化的ECStore mongodb 類base/lib/kvstore/mongodb.php*/class base_kvstore_mongodb extends base_kvstore_abstract implements base_interface_kvstore_base {    static private $_mongodb = null;    function __construct($prefix)     {        $prefix = is_string($prefix) ? preg_replace("(\/|\\|\-|-|-|\_|_)",‘.‘, $prefix):md5(var_export($prefix ,true));        $this->prefix = $prefix;        if(!isset(self::$_mongodb)){            $server = defined(‘MONGODB_SERVER_CONFIG‘)?MONGODB_SERVER_CONFIG:"mongodb://localhost:27017";            $option = defined(‘MONGODB_OPTION_CONFIG‘)?eval(MONGODB_OPEION_CONFIG):array("connect" => TRUE);            self::$_mongodb = new Mongo($server,$option);        }         $this->mongodb = self::$_mongodb->selectCollection(‘ecos‘,$this->prefix);    }//End Function    public function fetch($key, &$value, $timeout_version=null)     {        $store = $this->mongodb->findOne(array(‘key‘=>$this->create_key($key)));        if(!is_null($store) && $timeout_version < $store[‘dateline‘]){            if($store[‘ttl‘] > 0 && ($store[‘dateline‘]+$store[‘ttl‘]) < time()){                return false;            }            $value = $store[‘value‘];            return true;        }        return false;    }//End Function    public function store($key, $value, $ttl=0)     {        $store[‘value‘] = $value;        $store[‘dateline‘] = time();        $store[‘ttl‘] = $ttl;        $store[‘key‘] = $this->create_key($key);        $store[‘prefix‘] = $this->prefix;        $res = $this->mongodb->update(array(‘key‘=>$store[‘key‘]), $store, array("upsert" => true));        return $res;    }//End Function    public function delete($key)     {        return $this->mongodb->remove(array(‘key‘=>$this->create_key($key)));    }//End Function    public function recovery($record)     {        $key = $record[‘key‘];        $store[‘key‘] = $this->create_key($key);        $store[‘value‘] = $record[‘value‘];        $store[‘dateline‘] = $record[‘dateline‘];        $store[‘ttl‘] = $record[‘ttl‘];        $res = $this->mongodb->update(array(‘key‘=>$store[‘key‘]), $store, array("upsert" => true));        return $res;    }//End Function}//End Class
保證各關鍵表索引情況

每當mongodb 資料量激增,讀寫頻繁,鎖表嚴重,基本就是索引未加,或未經過拆表方案。

/*mongo 命令列操作*/use ecosshow collectionsdb.printCollectionStats()db.default.ensureIndex({key:1})db.b2c.cart.ensureIndex({key:1})db.tbdefine.ensureIndex({ke:1})db.cache.content.nodes.ensureIndex({key:1})db.b2c.goods.ensureIndex({key:1})

 

最佳化ECStore mongodb大資料 讀寫效率

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.