A solution to the problem of high concurrent writing in the site

Source: Internet
Author: User

A solution to the problem of high concurrent writing in the site

I am responsible for the maintenance and optimization of the lamp site based on Yii, which has a table of concurrent write is very high, resulting in poor user experience, or even write failure resulting in data loss, the analysis found that the table actually concurrent reading requirements are very low, so the plan will write to log, the log is regularly written to MySQL.

Log4php used in Yii

Log4php is a log frame of Apache, and the downloaded library is placed in a directory protected with Yii. Modify configuration log4php/config.php, this is the configuration information in the PHP array format, we want to write the record to the file, and by the hour to slice, such as:

<?php    return Array(' Rootlogger '=Array(' Appenders '=Array(' Default '),    ),' Appenders '=Array(' Default '=Array(' class '=' Loggerappenderdailyfile ',' layout '=Array(' class '=' Loggerlayoutsimple '),' params '=Array(' Datepattern '=' Y-m-d.h ',' file '='./history_log/history-%s.log ',            )        )    )    );

It's easy, of course, to reference the library in Yii's index.php:

// history_log$log4php=dirname(__FILE__).‘/log4php/Logger.php‘;$log4phpconfig=dirname(__FILE__).‘/log4php/config.php‘;require_once($log4php);Logger::configure($log4phpconfig);

To use, raise a chestnut in the business office:

Logger::getRootLogger()->info(json_encode($item));

In this way, the data is recorded in the log, for example, a chestnut:

Cycle writes log to the database

I use shell and Yii's commands support to do regular scripting, stream of consciousness, for the sake of convenience.
Shell to get the time and invoke the Yii command:

logTime=$(date --date=‘1 hour ago‘ +%Y-%m-%d.%H$logTime

Yii PHP is used to read log, to write MySQL, a Cconsolecommand class that inherits Yii is good:

 class updatehistorycommand extends cconsolecommand{    ConstLogpath='/home/qec/qss/www/history_log/'; Public  function __construct() {} Public  function run($args) {        if(Count ($args) !=1){Echo "Need one time arg","\ n";return false; }$logTime=$args[0];$logFileName=' history-'.$logTime.'. Log ';Echo "LogFileName:",$logFileName,"\ n";Echo  Self:: LOGPATH.$logFileName,"\ n";$path= Self:: LOGPATH.$logFileName;$logFd= fopen ($path,' R ');if($logFd==0){Echo "Open logFile", Self:: LOGPATH,$logFileName,"failed!\n";return false; }$this->addlogtohistory ($logFd); }    ...

Finally, Crontab is added to the timer task:
5 * * * * source/home/qec/.bash_profile;cd/home/qec/qss/www/protected/commands; SH hourlyjobs.sh >>. /runtime/hourlyjobs.log

You can do it.
PS: It feels good to use markdown for the first time.

A solution to the problem of high concurrent writing in the site

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.