search
HomeBackend DevelopmentPHP Tutorial[Original] PHP development in multiple memcached and mysql master-slave environments: Detailed code explanation_PHP tutorial

It’s 4 o’clock. Today is the last day in this company. I’m not in a good mood.

So I wrote something to vent. The usual practice for large websites is to use the memory as a database (memcached). And good read and write separation backup mechanism (mysql master-slave)

How can we develop PHP in such an environment. I don’t know much about it. I can talk. So let me post the code.

I just wrote a demo in VIM of linux and passed the debugging. I also hope that everyone will buy it and write it in PHP5. I am afraid that people will say that I am lagging behind if I write it in PHP4

$memcached = array( //Use memcached to simulate multiple processes Taiwanmemcached server cn en is the memory server name
'cn'=>array('192.168.254.144',11211),
'en'=> array('192.168.254.144',11212)
);
$mysql = array( // mysql master-slave my environment is: xp master linux slave mysql 5 php5
'master'=> array('192.168.254.213','root','1','mydz'),
'slave_1'=>array('192.168.254.144','root','1','mydz') //You can flexibly add multiple slave servers
);
?>

Server configuration file: very convenient to switch master and slave. When the master changes, the slave can quickly switch to the master. Supports multiple slaves Server .

[php]
class Memcached
{
private $mem;
public $pflag=''; // memcached pconnect tag
private function memConnect($serkey){
require 'config.php';
$server = $memcached;
$this->mem = new Memcache;
$link = !$this->pflag ? 'connect' : 'pconnect' ;
$this->mem-> $link($server[$serkey][0],$server[$serkey][1]) or $this->errordie('memcached connect error');

}

public function set($ser_key,$values,$flag='',$expire=''){
$this->memConnect($this-> ;tag($ser_key));
if($this->mem->set($ser_key,$values,$flag,$expire)) return true;
else return false;
}

public function get($ser_key){
$this->memConnect($this->tag($ser_key));
if($var=$this-> mem->get($ser_key)) return $var;
else return false;
}
private function tag($ser_key){
$tag=explode('_',$ser_key );
return $tag[0];
}
private function errordie($errmsg){
die($errmsg);
}
}
?>
[/php]

Simply encapsulates the operation of memcached. There is not much time for details. I am leaving the company

On multiple servers of memcached. My implementation idea is as follows: When adding information to the memory server, I chose to manually set it to add to that server. Rather than the traditional automatic allocation based on ID.
This can be more flexible.

Expressed by the name of the memory server. For example, to save the information $arr to the memory server en, I will write $mem-> ;set('en_'.$arr); Got it


[php]
class Mysql
{
private $mysqlmaster;
private $myssqlslave;
private static $auid=0;
public function __construct(){
require 'config.php';
$msg = $mysql;

$this->mysqlmaster = new mysqli($msg['master'][0],$msg['master'][1],$msg['master'][2],$msg['master'] [3]); //master mysql
$this->mysqlslave = $this->autotranscat($msg); // slave mysql

if(mysqli_connect_errno()){
printf("Connect failed: %sn",mysqli_connect_error());
exit();
}
if(!$this->mysqlmaster->set_charset("latin1") && !$ this->mysqlslave->set_charset("latin1")){
exit("set charset error");
}
}

private function autotranscat($mysql){
  session_start();
  $_SESSION['SID']!=0 || $_SESSION['SID']=0   ;
  if($_SESSION['SID'] >=count($mysql)-1) $_SESSION['SID'] = 1;
  else $_SESSION['SID']++;
  $key = 'slave_'.$_SESSION['SID'];
  echo($_SESSION['SID']);
  return new mysqli($mysql[$key][0],$mysql[$key][1],$mysql[$key][2],$mysql[$key][3]);
}

public function mquery($sql){ //insert  update 
  if(!$this->mysqlmaster->query($sql)){
   return false;
  }
}

public function squery($sql){
  if($result=$this->mysqlslave->query($sql)){
   return $result; 
  }else{
   return false;
  };
}
public function fetArray($sql){
  if($result=$this->squery($sql)){
   while($row=$result->fetch_array(MYSQLI_ASSOC)){
    $resultraa[] = $row;
   };
   return $resultraa;
  }
}
}
?>
[/php]

这个是 mysqli 的封装.  也就是   读  从  写 主  的操作的封装.

[php] 
require 'init.php';
$mem = new Memcached;
/* $mem->set('en_xx','bucuo');
echo($mem->get('en_xx'));
$mem->set('cn_jjyy','wokao');
echo($mem->get('cn_jjyy'));
*/ 
$sq = new Mysql;
$sql = "insert into mybb(pid) values(200)";
$mdsql = md5($sql);
if(!$result=$mem->get('cn_'.$mdsql)){
  $sq->mquery("insert into mybb(pid) values(200)"); //插入到主mysql
  $result = $sq->fetArray("select * from mybb"); //查询 是 从mysql
  foreach($result as $var){
   echo $var['pid'];
  }
  $mem->set('cn_'.$mdsql,$result); //添加到 名为 cn 的 memcached 服务器  
}else{
  foreach($result as $var){
   echo $var['pid'];
  }
}
?>
[/php]

这个是使用程序.   看下就大概明白了.
 大站就差不多是这样实现的了.   这种帖似乎  只有我发了.    献丑了

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/735128.htmlTechArticle4点了.今天是最后一天在这间公司.心情不是很好. 所以写下东西发泄下. 一般的大站通常做法是 拿着内存当数据库来用( memcached ). 和很好的读...
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
Memcached缓存技术对于PHP中的Session处理的优化Memcached缓存技术对于PHP中的Session处理的优化May 16, 2023 am 08:41 AM

Memcached是一种常用的缓存技术,它可以使Web应用程序的性能得到很大的提升。在PHP中,常用的Session处理方式是将Session文件存放在服务器的硬盘上。但是,这种方式并不是最优的,因为服务器的硬盘会成为性能瓶颈之一。而使用Memcached缓存技术可以对PHP中的Session处理进行优化,提高Web应用程序的性能。PHP中的Session处

PHP8.0中的缓存库:MemcachedPHP8.0中的缓存库:MemcachedMay 14, 2023 am 08:16 AM

PHP8.0中的缓存库:Memcached随着互联网的快速发展,现代应用程序需要高效可靠的缓存技术来提高性能和处理大量数据。由于PHP的流行和开源特性,PHP缓存库已经成为了Web开发社区的一个必备工具。Memcached是一种广泛使用的开源高速内存缓存系统,它能处理数百万个同时连接的缓存请求,可以用于许多不同类型的应用程序,例如社交网络、在线

华硕飞行堡垒7--原创华硕飞行堡垒7--原创Jul 14, 2023 pm 10:09 PM

  提起华硕旗下的游戏本,大家最先想起来的肯定是玩家国度,但除了玩家国度这个高端系列的游戏本之外,华硕旗下还有飞行堡垒系列的主流游戏本,下面我们就一起来看看游戏玩家呼声最高的华硕飞行堡垒7怎么样。  华硕飞行堡垒7  华硕旗下的飞行堡垒系列笔记本,定位轻松畅玩大型游戏,主打坚固与耐用,一直以来都是大众游戏玩家、学生群体的热门之选。  基本配置  首先我们来了解下这台飞行堡垒7的一些核心配置吧,从配置表中看,最值得注意的就是AMDRyzen73750H+NVIDIAGeForceGTX1660Ti

PHP与Memcached数据库备份与恢复PHP与Memcached数据库备份与恢复May 15, 2023 pm 09:12 PM

随着互联网的快速发展,大规模MySQL数据库备份和恢复成为各大企业和网站必备的技能之一。而随着Memcached的广泛应用,如何备份和恢复Memcached也成为了一个重要的问题。PHP作为Web开发的主力语言之一,在处理备份和恢复MySQL和Memcached上拥有独特的优势和技巧。本文将详细介绍PHP处理MySQL和Memcached备份与恢复的实现方法

利用Memcached缓存技术对于PHP中的音视频播放进行优化利用Memcached缓存技术对于PHP中的音视频播放进行优化May 17, 2023 pm 04:01 PM

随着互联网技术的不断发展,音视频资源已经成为了互联网上非常重要的一种内容形式,而PHP作为网络开发中使用最广泛的语言之一,也在不断地应用于视频和音频播放领域。然而,随着音视频网站的用户日益增加,许多网站已经发现了一个问题:在高并发的情况下,PHP对于音视频的处理速度明显变缓,会导致无法及时播放或者播放卡顿等问题。为了解决这个问题,Memcached缓存技术应

修改MD5后是否算为原创内容?修改MD5后是否算为原创内容?Feb 19, 2024 pm 08:04 PM

修改md5后算原创吗在互联网时代,创作原创内容成为了一种重要的价值和资源。然而,随之而来的就是对原创性的质疑和侵权行为。为了防止盗版和抄袭,很多人尝试使用不同的方法来保护自己的原创作品。其中一种常用的方法就是使用MD5算法对作品进行修改,以此来达到“算法保护”的作用。MD5(MessageDigestAlgorithm5)是一种常用的消息摘要算法,它能

使用PHP和Memcached进行缓存管理使用PHP和Memcached进行缓存管理May 23, 2023 pm 02:21 PM

随着网络应用的不断增加和数据量的不断膨胀,数据的读写效率成为影响应用性能的重要因素之一。而缓存技术的应用则可以很好地解决这个问题。在PHP应用中,Memcached是最常用的缓存服务器。Memcached是一个高性能的分布式内存对象缓存系统,可以将常用的数据存储在内存中,提高数据检索的效率。本文将介绍如何使用PHP和Memcached进行缓存管理,以及如何优

PHP与Memcached性能监控PHP与Memcached性能监控May 15, 2023 pm 09:51 PM

随着现代互联网应用的快速发展,用户体验对于一个应用的成功至关重要。如何保证应用的高性能和高可用性,成为了开发人员需要解决的重要问题之一。PHP作为一种广泛应用的编程语言之一,它的性能监控和优化也是非常重要的。Memcached是一个高性能、分布式的内存对象缓存系统,可以帮助应用提高性能和扩展性。本文将介绍如何使用PHP和Memcached实现性能监控的方法。

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools