最新消息:觉得本站不错的话 记得收藏哦 博客内某些功能仅供测试 讨论群:135931704 快养不起小站了 各位有闲钱就打赏下把 My Email weicots#gmail.com Please replace # with @

Redis 的多种用途 任务记忆 任务多开 计数 以及任务协调 去重等

LINX ajiang-tuzi 3354浏览

最开始接触 redis 是几年前用来做发邮件的规则处理器 用来模拟手动发邮件的 时间区间 还有任务断点记忆 和去重等功能
那个 邮件规则处理代码 就不放了 那玩意还在用
我放个采集的

redis
1.短点记忆 也就的在运行中 你把那个运行的脚本给他×了 然后重新运行脚本 还会从上次的运行的地方开始
2.多个脚本同时 运行 同时记忆 这个功能我使用了 前缀来来保证 个脚本的 数据空间不重叠
3.计数器 计数规则
4.去重

采集
1.自动手动采集切换
2.错误次数 收集
3.根据错误次数 自动更换头部 和cookie 文件
4.根据错误信息 判断错误原因
5.根据错误信息 判断是否重启任务
6.根据错误信息 自动检查网络
7.根据错误信息 自动拨号
8.根据错误信息 自动延时
9.根据错误信息 定时多开采集
10.离线采集
备注 本采集器 可以通过text 指定采集数据 也可以 通过数据库指定采集源

用途
1.快速采集亚马逊产品的具体信息 并过滤数据 存入数据库
2.按照规制文件导入 Magento 或 亚马逊 中

规则引擎 部分代码

  /**
 * WeiCot Min Framework  SP
 * User: ajiang
 * WebSite: WWW.WEICOT.COM
 * Date: 2017/2/23 0023
 * Time: 17:08
 */
    function cleanRedis()
    {
        $redis = new Redis();  //Redis
        $redis->connect('127.0.0.1', 6379);
        $redis->set(RPrefix . 'useAget', "0");    //使用 请求头的次数
        $redis->set(RPrefix . 'useAget_content', "1");    //使用 请求头的次数
        $redis->set(RPrefix . 'total', "0");
        $redis->set(RPrefix . 'fileName', "");
        $redis->set(RPrefix . 'netStatusFag', "0");
        $redis->set(RPrefix . 'ip', "0");
        $redis->set(RPrefix . 'clearFag', "0");
        echo "缓存清理完成 请重新运行\r\n";
    }

    /**
     * @param $SP
     * @param $service
     * @param $rule
     * @param bool|true $automatic 自动采集
     */

    function  SpRuleEn($version, $service, $rule, $automatic = true, $datasql = "", $limit = "")
    {
        echo "WEICOT SP  " . $version . " \t Servicr Id " . $service . " \t  Rule Id " . $rule . "\r\n";
        $redis = new Redis();  //Redis
        $redis->connect('127.0.0.1', 6379);
        $rand = (rand(21, 50)); //随机换头
        $collection = $this->Collection();
        $path = ROOT . "/log/text/";
        //判断是否是上次的任务  文件 是否完成
        if ($redis->get(RPrefix . 'total') < 2) {
            // $automatic  自动采集
            if (!$automatic) {
                $redis->set(RPrefix . 'fileName', 'other.tmp.txt');
            } else {
                //自动采集
                $fileInfo = $this->Collection()->putText(
                    $this->filter()->getDataDb($service, $datasql, $limit), $path
                );
                $redis->set(RPrefix . 'fileName', $fileInfo["name"]);
            }
            $fileName = $redis->get(RPrefix . 'fileName');
        } else {
            $fileName = $redis->get(RPrefix . 'fileName');
        }
        /************************需要采集的数据********************/


        $cookie_file = ROOT . "/log/session/cookie_" . RPrefix . md5(basename(__FILE__)) . ".txt"; // 设置Cookie文件保存路径及文件名
        $user_agent = "Mozilla/5.0 (Windows NT 5.1; U; en-us; 		rv:39.0b6) Gecko/20100101 Firefox/39.0b6";  //模拟浏览器
        //$proxy = "61.135.169.125";
        $Sku = ROOT . "/log/text/" . $fileName;
        $headerPath = ROOT . "/log/header/fileFox.txt";
        $curlIni = array(
            "isProxy" => false,
            "userAgent" => $user_agent,
            "cookieFile" => $cookie_file,
        );


        //判断是否是上上次的任务
        if ($redis->get(RPrefix . 'total') < 2) {
            //初始化
            $redis->set(RPrefix . 'useAget', "0");    //使用 请求头的次数
            $redis->set(RPrefix . 'useAget_content', "1");    //请求头的位置
            $redis->set(RPrefix . 'total', "0");
            $redis->set(RPrefix . 'ip', "0");
            $redis->set(RPrefix . 'clearFag', "0");
            $array = $this->Collection()->getFileLines($Sku, $startLine = 1, $endLine = 5000, $method = 'rb');   //文件 地址
            $header = $this->Collection()->getFileLines($headerPath, $startLine = 1, $endLine = 50, $method = 'rb');//请求头
        } else {
            echo "继续上次任务\r\n";
            $array = $this->Collection()->getFileLines($Sku, $redis->get(RPrefix . 'total') + 1, $endLine = 5000, $method = 'rb');   //文件 地址
            $header = $this->Collection()->getFileLines($headerPath, $redis->get(RPrefix . 'useAget_content'), $endLine = 50, $method = 'rb');//请求头

        }


        $i = 1; //循环次数
        foreach ($array as $value) {
            $total_i = "[TOTAL]" . $redis->get(RPrefix . 'total') . "||" . $i . "\r\n";
            file_put_contents(ROOT . "/log/text/" . $fileName . ".log", $total_i, FILE_APPEND);
            $redis->incr(RPrefix . 'useAget');

            //更换请求头
            if ($redis->get(RPrefix . 'useAget') == $rand) {  //到了  指定使用 次数
                $redis->set(RPrefix . 'useAget', "0");    //使用 请求头的次数
                if ($redis->get(RPrefix . 'useAget_content') > 48) {
                    $redis->set(RPrefix . 'useAget_content', "1");    //使用 请求头的次数
                }
                $user_agent = $header[$redis->get(RPrefix . 'useAget_content')];  //更换请求头
                unlink($cookie_file);  //删除 cookie
                $curlIni = array(
                    "isProxy" => false,
                    "userAgent" => $user_agent,
                    "cookieFile" => $cookie_file,
                );

                $redis->incr(RPrefix . 'useAget_content');
            }

            //接收清理关联信息的请求
            if ($redis->get(RPrefix . 'clearFag') == 1) {
                $user_agent = $header[rand(20, 48)];   //随机更换请求头
                unlink($cookie_file);  //删除 cookie
                $curlIni = array(
                    "isProxy" => false,
                    "userAgent" => $user_agent,
                    "cookieFile" => $cookie_file,
                );
                $redis->set(RPrefix . 'clearFag', "0");
            }

            //遇到不符合要求的  跳出循环
            if (strlen($value) < 5) {
                break;
            }
            $sku = explode("\t", $value);
            $redis->incr(RPrefix . 'total');

            // 采集开始
            echo "\r\n";
            echo $i."\t".$fileName . "\t" . $sku[0] . "\t" . $sku[1] . "\r\n";
            $this->Collection()->ini= $curlIni;  //引入配置文件
            $this->SpRun($sku[1],$sku[0], $fileName, $redis,$collection);
            //采集结束
            $i++;
        }
        //fclose($fp);
        //重置
        $redis->set(RPrefix . 'useAget', "0");    //使用 请求头的次数
        $redis->set(RPrefix . 'useAget_content', "1");    //使用 请求头的次数
        $redis->set(RPrefix . 'total', "0");
        $redis->set(RPrefix . 'netStatusFag', "0");
        $redis->set(RPrefix . 'ip', "0");


    }

使用 redis 做的产品导人 主副产品去重
redis

转载请注明:(●--●) Hello.My Weicot » Redis 的多种用途 任务记忆 任务多开 计数 以及任务协调 去重等

蜀ICP备15020253号-1