search
Homephp教程php手册php中memcache读取数据批量写入mysql

用 Memcache 可以缓解 php和数据库压力下面代码是解决高负载下数据库写入瓶颈问题,遇到最实用的:写入ip pv uv的时候,用户达到每分钟几万访问量,要记录这些数据,实时写入数据库必定奔溃.

用以下技术就能解决,还有如用户注册,同一时间断内,大量用户注册,可以缓存后一次性写入到数据库,代码如下:

<?php
public function cldata() {
    $memcache_obj = new Memcache;
    $memcache_obj->connect(&#39;127.0.0.1&#39;, &#39;11211&#39;);
    $all_items = $memcache_obj->getExtendedStats(&#39;items&#39;);
    foreach ($all_items as $option => $vall) {
        if (isset($all_items[$option][&#39;items&#39;])) {
            $items = $all_items[$option][&#39;items&#39;];
            foreach ($items as $number => $item) {
                $str = $memcache_obj->getExtendedStats(&#39;cachedump&#39;, $number, 0);
                $line = $str[$option];
                if (is_array($line) && count($line) > 0) {
                    foreach ($line as $key => $value) {
                        $keys[] = $key;
                    }
                }
            }
        }
    }
    dump(count($keys)); //获取到key
    if (count($keys) > 50) { //要写入的数据条数
        $end = 50;
    } else {
        $end = count($keys);
    }
    for ($i = 0; $i <= $end; $i++) {
        if (!strstr($keys[$i], &#39;datadb&#39;)) continue;
        $ksv = str_replace(&#39;datadb&#39;, &#39;&#39;, $keys[$i]);
        /*$logdata = unserialize(S(&#39;login&#39;.$ksv));//登录写入
        if(is_array($logdata)){
        $this->addsuidinlogin($logdata[0],$logdata[1],$logdata[2],1);
        }   */
        /*$sdata = unserialize(S(&#39;regadd&#39;.$ksv));//注册写入
        if(is_array($sdata)){
        $this->baiduad($sdata[0],$sdata[1],$sdata[2],$sdata[3],$sdata[4],1);
        }
        */
        $regdata = unserialize(S(&#39;datadb&#39; . $ksv));
        $ress[] = $regdata; //开源代码phprm.com
        S(&#39;datadb&#39; . $ksv, null);
    }
    $addb = M()->db(66, C(&#39;DB_WEB_AD&#39;)); //批量写入 addall
    $addb->table(&#39;mj_ad_count&#39;)->addall($ress);
    echo M()->getLastSql();
}

附:

可以使用的工具如:memadmin还有memadmin文档 . 


本文地址:

转载随意,但请附上文章地址:-)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)