不用架构,再烂的PHP代码,再烂的SQL语句,一样能承受5W人同时访问,上一个帖子不知道怎么回事,资源上传后就没了。
这次再发一次。
地址:http://download.csdn.net/detail/q184996833/5800367PHP并发架构SQL

解决方案 »

  1.   

    你们自己评价一下,值不值得化 5 分<?php
    class WebSupervene{
    private $filename = "";
    private $count = 50;

    /**
     * @return the $filename
     */
    public function getFilename() {
    return $this->filename;
    } /**
     * 设置并发访问人数
     * @param number $count
     */
    public function setCount($count) {
    $this->count = $count;
    } function __construct(){
    $dir="./_runlist";
    $this->filename=$dir."/~Supervene";
    if(!is_dir($dir)){
    mkdir($dir);
    }
    }

    /**
     * 缓存队列
     */
    private function saveCache($value){
    $cache=self::read();
    $time = date("YmdHis",time());
    $wcache="";
    if(!$cache){
    $warr[$value]=$time;
    $wcache = serialize($warr);
    }else{
    $warr=unserialize($cache);
    $warr_rs=array_keys($warr,$time);
    if(empty($warr_rs)){
    unset($warr);
    $warr[$value]=$time;
    $wcache = serialize($warr);
    }else{
    if(count($warr_rs)<$this->count){
    $warr[$value]=$time;
    $wcache = serialize($warr);
    }else{
    return false;
    }
    }
    }
    if($wcache!==""){
    self::write($wcache);
    return true;
    }else{
    return false;
    }
    }

    /**
     * 读取缓存
     */
    private function read(){
    $fread = fopen(self::getFilename(), "r");
    if($fread){
    $contents = fread($fread, filesize (self::getFilename()));
    fclose($fread);
    return $contents;
    }else{
    return false;
    }
    }
    /**
     * 写入缓存
     */
    private function write($value){
    $fwrite = fopen(self::getFilename(), 'w+');
    fwrite($fwrite, $value);
    fclose($fwrite);
    }
    /**
     * 主入口
     * @param unknown_type $ip
     */
    public function Supervene($ip){
    if(!self::saveCache($ip)){
    echo "<script language=JavaScript> location.replace(location.href);</script>";
    exit;
    }
    }
    }
    ?>