本文实例讲述了ThinkPHP进程计数类Process用法。分享给大家供大家参考。具体如下:
项目中有一个需求:由于某一后台任务比较占带宽,所以要限制进程数。花了点时间,写了类,目前版本功能比较简单。
Process.class.php文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
<?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 = '~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;
}
}
?>
|
调用方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?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); // 动态更改限制数, 默认为10
if (Process::isOvertop()) echo "超出限制" ;
else "未超出限制" ;
}
}
?>
|
希望本文所述对大家基于ThinkPHP框架的php程序设计有所帮助。