Home > Backend Development > PHP Tutorial > About the usage of ThinkPHP process counting class Process

About the usage of ThinkPHP process counting class Process

不言
Release: 2023-03-30 20:42:02
Original
1587 people have browsed it

This article mainly introduces the usage of the ThinkPHP process counting class Process. It analyzes the definition of the Process class and the implementation techniques of process counting in detail in the form of examples. It has certain reference value. Friends in need can refer to it

The example in this article describes the usage of ThinkPHP process counting class Process. Share it with everyone for your reference. The details are as follows:

There is a requirement in the project: because a certain background task takes up more bandwidth, the number of processes must be limited. It took me some time to write the class, and the current version has relatively simple functions.

Process.class.php file is as follows:

<?php
/**
 * Process 
 * 
 * @package 
 * @version $id$
 * @copyright 2005-2011 SUCOP.COM
 * @author Dijia Huang <huangdijia@gmail.com> 
 * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
class Process
{
  const PROCESS_KEY = &#39;~Process&#39;;
  const PROCESS_MAXNUM = 10;
  /**
   * start 
   * 
   * @static
   * @access public
   * @return void
   */
  static public function start(){
    $list = self::__getList();
    $name = self::__getName();
    if(!isset($list[$name])){
      $list[$name] = array(&#39;count&#39;=>1, &#39;lasttime&#39;=>time());
    }else{
      if((time()-$list[$name][&#39;time&#39;]) > 600){
        $list[$name][&#39;count&#39;] = 1;
      }else{
        $list[$name][&#39;count&#39;] += 1;
      }
    }
    self::__setList($list);
  }
  /**
   * destory 
   * 
   * @static
   * @access public
   * @return void
   */
  static public function destory(){
    $list = self::__getList();
    $name = self::__getName();
    if(isset($list[$name])){
      if($list[$name][&#39;count&#39;] <= 1){
        unset($list[$name]);
      }else{
        $list[$name][&#39;count&#39;] -= 1;
        $list[$name][&#39;lasttime&#39;] = time();
      }
      self::__setList($list);
    }
  }
  /**
   * getCount 
   * 
   * @static
   * @access public
   * @return void
   */
  static public function getCount(){
    $list = self::__getList();
    $name = self::__getName();
    return $list[$name][&#39;count&#39;];
  }
  /**
   * getMaxnum 
   * 
   * @static
   * @access public
   * @return void
   */
  static public function getMaxnum(){
    $name = self::__getName();
    return C($name) ? C($name) : self::PROCESS_MAXNUM;
  }
  /**
   * getName 
   * 
   * @static
   * @access public
   * @return void
   */
  static public function getName(){
    return self::__getName();
  }
  /**
   * isOvertop 
   * 
   * @static
   * @access public
   * @return void
   */
  static public function isOvertop(){
    return (self::getCount() > self::getMaxnum());
  }
  /**
   * getLasttime 
   * 
   * @static
   * @access public
   * @return void
   */
  static public function getLasttime(){
    $list = self::__getList();
    $name = self::__getName();
    return $list[$name][&#39;lasttime&#39;];
  }
  /**
   * clear 
   * 
   * @static
   * @access public
   * @return void
   */
  static public function clear(){
    F(self::PROCESS_KEY, null);
  }
  /**
   * __setList 
   * 
   * @param mixed $list 
   * @static
   * @access private
   * @return void
   */
  static private function __setList($list=null){
    if(!is_array($list) || empty($list))
      F(self::PROCESS_KEY, null);
    else
      F(self::PROCESS_KEY, $list);
  }
  /**
   * __getList 
   * 
   * @static
   * @access private
   * @return void
   */
  static private function __getList(){
    $list = F(self::PROCESS_KEY);
    if(!is_array($list)) return array();
    else return $list;
  }
  /**
   * __getName 
   * 
   * @static
   * @access private
   * @return void
   */
  static private function __getName(){
    return (defined(&#39;GROUP_NAME&#39;) ? GROUP_NAME.&#39;_&#39; : &#39;&#39;) . MODULE_NAME . &#39;_&#39; . ACTION_NAME;
  }
}
?>
Copy after login

Calling method:

<?php
class IndexAction extends Action 
{
  // 初始化模块
  public function _initialize(){
    parent::_initialize();
    import(&#39;@.Util.Process&#39;);
    Process::start();
  }
  function __destruct(){
    Process :: destory();
  } 
  public function index(){
    C(&#39;Index_index&#39;, 3); // 动态更改限制数, 默认为10
    if(Process::isOvertop()) echo "超出限制";
    else "未超出限制";
  }
}
?>
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Usage of distinct in Thinkphp

About thinkPHP framework to add js event paging class customPage.class. Analysis of php

About automatic verification of the create() method in ThinkPHP

The above is the detailed content of About the usage of ThinkPHP process counting class Process. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template