如何让网站程序定时执行

技术原理

cron+taskqueue

有人说直接用cron就行了,为什么还要用taskqueue呢,是这样的,如果加入一个定时执行任务就要建立一个cron计划的话未免太麻烦,也有可能受到服务器资源的限制,比如我用新浪SAE只能设置15个cron计划,那假如我有一千个定时执行任务,就没办法了。

所以我采取以下策略

我把每个计划任务分成两个部分:需要请求的URL和POST数据,post数据用json格式储存。创建两个表circle表和task表,如果是隔特定时间执行一次任务就在task表中创建一个任务并把任务ID加入circle表中。如果是定时执行,比如x时x分x秒执行,只需把该任务加入task表中即可,并附上时间信息。

然后建了15个cron任务,每隔4秒执行特定的一个脚本,该脚本任务是从task表和circle表中读取时间信息,判断哪个任务需要当前执行,然后取出加入taskqueue队列。。

 

ThinkPHP框架分析

先说配置

配置有系统默认配置、项目配置、扩展配置,其中我们自定义的内容绝大多数在项目配置中,也就是Conf/config.php 扩展配置的一种写法如下

先在config.php中写

[php]

<?php

return array(

"LOAD_EXT_CONFIG"=>"db",

);
[/php]

然后我们在同目录的db.php文件中写入配置信息

如何分组

分组比较简单,现在刚才那个config.php文件中写入如下配置信息

[php]

<?php

return array(

"APP_GROUP_LIST"=>"admin,home",

"DEFAULT_GROUP"=>"home",

);

[/php]

然后在action等目录中新建admin home目录,在这些目录中写相关业务逻辑,一些人做的显示错误,可能是大小写输入错误,那可以配置,大小写不敏感

[php]

<?php

return array(

"URL_CASE_INSENSITIVE"=>true

);

[/php]

控制器

控制器比较特别的就属前置和后置操作了,例如:

[php]

function _before_index(){

}

function _after_index(){

}

[/php]

其它的就是获取系统变量,判断请求类型,比较特别的就是ajax返回

模型

数据库模型操作比较老套,亮点有个字段映射,例如

[php]

<?php

protected $_map=array("name"=>"username");

[/php]

由数据库取出的字段转换成表单字段

[php]

<?php

$User=D(‘"user");

$data=$User->find(1);

$data=$User->parseFieldsMap($data);

[/php]

让HTTP代理更加智能化

一、HTTP代理的应用环境

刷票、发帖、刷流量等需要在短时间内换大量IP的应用

二、在使用HTTP代理的难点

1.选出能有且速度快的代理较难

2.用选出的代理在实际使用中会出现不稳定情况,需要针对此IP进行智能化的再鉴定和剔除工作

3.伪造初始IP地址,如果只是简单的应用代理,服务器很简单的就能检测出原始IP

三、程序的书写

[php]
public function get($url) {
$this->curl = curl_init();
if($this->proxy){
curl_setopt ($this->curl, CURLOPT_PROXY,$this->proxy);
curl_setopt ($this->curl, CURLOPT_HTTPHEADER, array(‘CLIENT-IP:’.$this->client_ip, ‘X-FORWARDED-FOR:’.$this->client_ip));
echo $this->proxy;
}
curl_setopt($this->curl, CURLOPT_URL, $url);
curl_setopt($this->curl, CURLOPT_HEADER, 1);
curl_setopt($this->curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($this->curl, CURLOPT_COOKIE, $this->cookie);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($this->curl);
curl_close($this->curl);
preg_match_all("/Set-Cookie:(.*?);/", $data, $match, PREG_SET_ORDER);
foreach ($match as $r) {
if ($this->cookie != ”) {
$this->cookie = $this->cookie . ‘;’;
}
if (isset($r[1])) {
$this->cookie .= trim(str_replace("rn", "", $r[1]));
}
}
$this->set($this->cookie_name,$this->cookie);
return $data;

}
[/php]

贴吧综合性辅助系统开发构想

需要满足的功能

发帖、 回复、 大召唤、 获取特定贴吧所有会员信息 、删帖、爆吧、防爆吧(会员ID、标题、内容关键词过滤) 、抢楼

关键功能分析

登陆后可以把登陆信息存到文本或内存中,根据条件自己设计

免验证码是用老号点击我喜欢做到的,但是点击我喜欢的频率有限制

爆吧需要多马甲并使用代理,不然马甲被封或则IP被封

抢楼防爆吧都要对首页帖子进行扫描比对,wap版所用流量较少,扫描后按照特定条件进行识别

架构设计

memcached +php+apache+mysql+linux

mysql不是必须的,如果服务器不稳定建议用MySQL,若服务器稳定用memcached即可,效率高、速度快

Linux配置crontab任务定时执行触发脚本,比如扫描贴吧首页等

数据库设计(可选)

                                           贴吧用户表

                                               tieba

Tieba_ID Auto_increament int not null
login_name Char(30) not null
password Char(50) not null
is_guest Int(2) not null default 0

                                                 用户表

                                                  User

User_ID Auto_increament int not null
user_name Char(30) not null
password Char(50) not null
is_admin Int(2) not null default 0

                                          用户表和贴吧表

                                             User2tieba

User_id tint not null
Tieba_id Char(30) not null

                                               发帖定时器

                                               Send_Timer

Send_Id Int auto_increament  not null
Send_time Int not null  default 0
User_id Int not null
Tieba_id Int not null
title Varchar(200)
content Varchar(200)
Tieba_name Varchar(20)
Is_sent  Int not null default 0

                                           回复定时器

                                        Replay_timer

Replay_id Int auto_increament  not null
Replay_time Int not null default 0
Content Varchar(20)
tid Int
Is_sent Int not null default 0

                                               会员列表

                                               Member

Id Int not null auto_increament
Tieba_name Varcvhar(20)
Member_name Varchar(20)

网站前端、后端开发书籍推荐

网站前端、后端开发书籍推荐

前端

《精通CSS+DIV网页样式与布局》(前沿科技)

《CSS设计彻底研究》(作者:温谦)

《jquery基础教程》

《javascript高级程序设计》

后端(PHP方向)

 

《PHP与MYSQL WEB开发》

《PHP深度分析-101个核心技巧、窍门、和问题的解决方法》

学习MVC 框架 如framework YII 国内的thinkphp框架

剩下的就靠自己练习、体会了