這篇文章主要介紹了ThinkPHP進(jìn)程計(jì)數(shù)類Process用法,以實(shí)例形式較為詳細(xì)的分析了Process類的定義及進(jìn)程計(jì)數(shù)的實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了ThinkPHP進(jìn)程計(jì)數(shù)類Process用法。分享給大家供大家參考。具體如下:
項(xiàng)目中有一個(gè)需求:由于某一后臺任務(wù)比較占帶寬,所以要限制進(jìn)程數(shù)。花了點(diǎn)時(shí)間,寫了類,目前版本功能比較簡單。
Process.class.php文件如下:
- <?php
- /**
- * Process
- *
- * @package
- * @version $id$
- * @copyright 2005-2011 SUCOP.COM
- * @author Dijia Huang <[email protected]>
- * @license PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
- */
- class Process
- {
- const PROCESS_KEY = '~Process';
- 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('count'=>1, 'lasttime'=>time());
- }else{
- if((time()-$list[$name]['time']) > 600){
- $list[$name]['count'] = 1;
- }else{
- $list[$name]['count'] += 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]['count'] <= 1){
- unset($list[$name]);
- }else{
- $list[$name]['count'] -= 1;
- $list[$name]['lasttime'] = time();
- }
- self::__setList($list);
- }
- }
- /**
- * getCount
- *
- * @static
- * @access public
- * @return void
- */
- static public function getCount(){
- $list = self::__getList();
- $name = self::__getName();
- return $list[$name]['count'];
- }
- /**
- * 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]['lasttime'];
- }
- /**
- * 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('GROUP_NAME') ? GROUP_NAME.'_' : '') . MODULE_NAME . '_' . ACTION_NAME;
- }
- }
- ?>
調(diào)用方法:
- <?php
- class IndexAction extends Action
- {
- // 初始化模塊
- public function _initialize(){
- parent::_initialize();
- import('@.Util.Process');
- Process::start();
- }
- function __destruct(){
- Process :: destory();
- }
- public function index(){
- C('Index_index', 3); // 動態(tài)更改限制數(shù), 默認(rèn)為10
- if(Process::isOvertop()) echo "超出限制";
- else "未超出限制";
- }
- }
- ?>
希望本文所述對大家基于ThinkPHP框架的php程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答