码迷,mamicode.com
首页 > 数据库 > 详细

优化ECStore mongodb大数据 读写效率

时间:2014-12-15 15:16:01      阅读:506      评论:0      收藏:0      [点我收藏+]

标签: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 ecos
show collections
db.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大数据 读写效率

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://www.cnblogs.com/jimingsong/p/4164688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!