瀏覽代碼

配置代理

陈德本 5 年之前
父節點
當前提交
4c47fd76c5
共有 5 個文件被更改,包括 270 次插入2 次删除
  1. 7 1
      Config/Proxy.php
  2. 30 0
      Config/Redis/Proxy.php
  3. 1 0
      Library/Curl.php
  4. 231 0
      Library/Proxy.php
  5. 1 1
      Library/Redis.php

+ 7 - 1
Config/Proxy.php

@@ -10,7 +10,13 @@ class Proxy extends Config
 {
     public static function getData($section = null)
     {
-        $proxy=file_get_contents("http://www.97admin.com:5555/random");
+        $proxy=\Heanup\Library\Proxy::instance()->connect()->zRangeByScore("proxies:universal", 25, 25);
+        if ($proxy){
+            $proxy=$proxy[rand(0,count($proxy)-1)];
+        }else{
+            return "myhome.97admin.com:34185";
+        }
+//        $proxy=file_get_contents("http://www.97admin.com:5555/random");
         define("USE_HOME", $proxy);
         //        $data = json_decode($data, true);
         return $proxy;

+ 30 - 0
Config/Redis/Proxy.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace Heanup\Config\Redis;
+
+use Heanup\Frame\Config;
+
+/**
+ * 默认Redis配置
+ */
+class Proxy extends Config
+{
+    protected static $data = [
+        "master" => [
+            "host" => "www.97admin.com",
+            "port" => "63790",
+            "db" => 3,
+            "timeout" => 3
+        ],
+        "slave_list" => [
+            [
+                "host" => "www.97admin.com",
+                "port" => "63790",
+                "db" => 3,
+                "timeout" => 3
+            ]
+        ]
+    ];
+
+
+}

+ 1 - 0
Library/Curl.php

@@ -70,6 +70,7 @@ class Curl
         $curl_errno = curl_errno($curl);
         if ($curl_errno > 0) {
             Logger::setlog($error = sprintf("curl error=%s, errno=%d.使用代理%s", curl_error($curl), $curl_errno,$proxy));
+            Proxy::instance()->connect()->zIncrBy("proxies:universal", -1, $proxy);
             curl_close($curl);
             return false;
         } else {

+ 231 - 0
Library/Proxy.php

@@ -0,0 +1,231 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: dongdx
+ * Date: 2017/6/21
+ * Time: 10:25
+ */
+
+namespace Heanup\Library;
+
+use Heanup\Frame\Logger;
+
+class Proxy
+{
+
+    private $cfg;
+    private $w_redis;
+    private $r_redis;
+
+    public static $instance;
+
+    public function __construct()
+    {
+        $this->cfg = \Heanup\Config\Redis\Proxy::getData();
+    }
+
+    public static function instance(): Proxy
+    {
+        if (!isset(self::$instance)) {
+            $instance = new self();
+            self::$instance = $instance;
+        } else {
+            $instance = self::$instance;
+        }
+        return $instance;
+    }
+
+    public function get($key)
+    {
+        $redis = $this->connect(true);
+        if (empty($key)) {
+            return '';
+        } else {
+            $res = $redis->get($key);
+            $row = json_decode($res, true);
+            if ($res == '[]') $res = array();
+            return $row ?: $res;
+        }
+    }
+
+    public function set($key, $value, $life_time_limit = 0): bool
+    {
+        $redis = $this->connect(false);
+        if (is_array($value)) {
+            $value = json_encode($value);
+        }
+        if ($life_time_limit) {
+            return $redis->setex($key, $life_time_limit, $value);
+        } else {
+            return $redis->set($key, $value);
+        }
+    }
+
+    public function del($key)
+    {
+        $redis = $this->connect(false);
+        return $redis->del($key);
+    }
+
+    public function hExists($key, $hashkey)
+    {
+        $redis = $this->connect(true);
+        return boolval($redis->hExists($key, $hashkey));
+    }
+
+    public function hSet($key, $hashkey, $value)
+    {
+        $redis = $this->connect(false);
+        if (is_array($value)) {
+            $value = json_encode($value);
+        }
+        return $redis->hSet($key, $hashkey, $value);
+    }
+    public function hDel($key, $hashkey)
+    {
+        $redis = $this->connect(false);
+        return $redis->hDel($key, $hashkey);
+    }
+
+    public function hGet($key, $hashkey)
+    {
+        $redis = $this->connect(true);
+        $res = $redis->hGet($key, $hashkey);
+        $row = json_decode($res, true);
+        if ($res == '[]') $res = array();
+        return $row ?: $res;
+    }
+
+    public function hLen($key)
+    {
+        $redis = $this->connect(true);
+        $res = $redis->hLen($key);
+        return intval($res);
+    }
+
+    public function sAdd($key, $values)
+    {
+        $redis = $this->connect(false);
+        if (is_array($values)) {
+            $res = $redis->sAddArray($key,$values);
+        } else if(is_scalar($values)) {
+            $res = $redis->sAdd($key, $values);
+        }
+        return $res;
+    }
+
+    public function sRem($key,$value)
+    {
+        $redis = $this->connect(false);
+        return $redis->sRem($key,$value);
+    }
+
+    public function sMembers($key)
+    {
+        $redis = $this->connect(true);
+        return $redis->sMembers($key);
+    }
+
+    public function hMGet($key, $hashkey)
+    {
+        $redis = $this->connect(true);
+        return $redis->hMGet($key, $hashkey);
+    }
+
+    public function hGetAll($key)
+    {
+        $redis = $this->connect(true);
+        return $redis->hGetAll($key);
+    }
+
+    public function hVals($key)
+    {
+        $redis = $this->connect(true);
+        return $redis->hVals($key);
+    }
+
+    public function delete($key)
+    {
+        $redis = $this->connect(false);
+        return $redis->delete($key);
+    }
+
+    public function incr($key)
+    {
+        $redis = $this->connect(false);
+        return $redis->incr($key);
+    }
+
+    public function multi()
+    {
+        $redis = $this->connect(false);
+        $redis ->multi();
+    }
+
+    public function exec()
+    {
+        $redis = $this->connect(false);
+        try {
+            $result = $redis ->exec();
+            return $result;
+        } catch (\Exception $ex) {
+            Logger::error($ex->getMessage()."-".$ex->getTraceAsString()."(".$ex->getFile().":".$ex->getFile().")");
+//            return false;
+        }
+
+    }
+
+    /**
+     * @param bool $is_read
+     * @return bool|\Redis
+     */
+    public function connect($is_read = false)
+    {
+        try {
+            if ($is_read) {
+                //获取redis读连接
+                if ($this->r_redis) {
+                    return $this->r_redis;
+                }
+                $this->r_redis = new \Redis();
+                $config = $this->cfg['slave_list'][0];
+                if (isset($config['pconnect']) && $config['pconnect'] == 1) {
+                    $this->r_redis->pconnect($config['host'], $config['port'], $config['timeout']);
+                } else {
+                    $this->r_redis->connect($config['host'], $config['port'], $config['timeout']);
+                }
+                if (isset($config['password']) && $config['password']) {
+                    $this->r_redis->auth($config['password']);
+                }
+
+                //选择redis-db  -by edison
+                if (isset($config['db']))
+                    $this->r_redis->select(intval($config['db']));
+                return $this->r_redis;
+            } else {
+                //获取redis写连接
+                if ($this->w_redis) {
+                    return $this->w_redis;
+                }
+                $this->w_redis = new \Redis();
+                $config = $this->cfg['master'];
+                if (isset($config['pconnect']) && $config['pconnect'] == 1) {
+                    $this->w_redis->pconnect($config['host'], $config['port'], $config['timeout']);
+                } else {
+                    $this->w_redis->connect($config['host'], $config['port'], $config['timeout']);
+                }
+                if (isset($config['password']) && $config['password']) {
+                    $this->w_redis->auth($config['password']);
+                }
+                //选择redis-db  -by edison
+                if (isset($config['db']))
+                    $this->w_redis->select(intval($config['db']));
+                return $this->w_redis;
+            }
+        } catch (\Exception $ex) {
+            $message = "Redis connection failed : [" . $config['host'] . ';' . $config['port'] . ']' . $ex->getMessage();
+            Logger::error($message);
+//            return false;
+        }
+    }
+}

+ 1 - 1
Library/Redis.php

@@ -25,7 +25,7 @@ class Redis
         $this->cfg = Core::getData();
     }
 
-    public static function instance()
+    public static function instance(): Redis
     {
         if (!isset(self::$instance)) {
             $instance = new self();