php从memcache读取数据再批量写入mysql的方法_PHP
本文实例讲述了php从memcache读取数据再批量写入mysql的方法。分享给大家供大家参考。具体分析如下:
用 Memcache 可以缓解 php和数据库压力下面代码是解决高负载下数据库写入瓶颈问题,遇到最实用的:写入ip pv uv的时候,用户达到每分钟几万访问量,要记录这些数据,实时写入数据库必定奔溃.
用以下技术就能解决,还有如用户注册,同一时间断内,大量用户注册,可以缓存后一次性写入到数据库,代码如下:
代码如下:
public function cldata(){
$memcache_obj = new Memcache;
$memcache_obj->connect('127.0.0.1', '11211');
$all_items = $memcache_obj->getExtendedStats('items');
foreach($all_items as $option=>$vall){
if (isset($all_items[$option]['items'])) {
$items = $all_items[$option]['items'];
foreach ($items as $number => $item) {
$str = $memcache_obj->getExtendedStats('cachedump', $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
if(!strstr($keys[$i],'datadb')) continue;
$ksv = str_replace('datadb','',$keys[$i]);
/*$logdata = unserialize(S('login'.$ksv));//登录写入
if(is_array($logdata)){
$this->addsuidinlogin($logdata[0],$logdata[1],$logdata[2],1);
} */
/*$sdata = unserialize(S('regadd'.$ksv));//注册写入
if(is_array($sdata)){
$this->baiduad($sdata[0],$sdata[1],$sdata[2],$sdata[3],$sdata[4],1);
}
*/
$regdata = unserialize(S('datadb'.$ksv));
$ress[]=$regdata;
S('datadb'.$ksv,null);
}
$addb = M()->db(66,C('DB_WEB_AD'));//批量写入 addall
$addb->table('mj_ad_count')->addall($ress);
echo M()->getLastSql();
}
补充:可以使用的工具如:memadmin 还有memadmin 文档.
希望本文所述对大家的php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
