stanley-king 4 роки тому
батько
коміт
b1a69f8277

+ 33 - 7
core/framework/cache/cache.redis.php

@@ -51,10 +51,10 @@ class CacheRedis extends Cache
         return $this->enable;
     }
 
-	public function get($key, $type = ''){
+	public function get($key, $prefix = ''){
 		$this->init_slave();
 		if (!$this->enable) return false;
-		$this->type = $type;
+		$this->type = $prefix;
 		$value = $this->handler->get($this->_key($key));
 
         return unserialize($value);
@@ -84,6 +84,16 @@ class CacheRedis extends Cache
         return $result;
     }
 
+    public function get_org($key,$prefix = '')
+    {
+        $this->init_master();
+        if (!$this->enable) return false;
+        $this->type = $prefix;
+        $result = $this->handler->get($this->_key($key));
+
+        return $result;
+    }
+
     public function incr($key,$prefix = '')
     {
         $this->init_master();
@@ -94,18 +104,34 @@ class CacheRedis extends Cache
         return $value;
     }
 
-    public function incrby($key,$val,$prefix = '') {
-
+    public function incrby($key,$val,$prefix = '')
+    {
+        $this->init_master();
+        if (!$this->enable) return false;
+        $this->type = $prefix;
+        $key = $this->_key($key);
+        $value = $this->handler->incrby($key,$val);
+        return $value;
     }
 
-    public function decr($key,$prefix)
+    public function decr($key,$prefix = '')
     {
-
+        $this->init_master();
+        if (!$this->enable) return false;
+        $this->type = $prefix;
+        $key = $this->_key($key);
+        $value = $this->handler->decr($key);
+        return $value;
     }
 
     public function decrby($key,$val,$prefix = '')
     {
-
+        $this->init_master();
+        if (!$this->enable) return false;
+        $this->type = $prefix;
+        $key = $this->_key($key);
+        $value = $this->handler->decrby($key,$val);
+        return $value;
     }
 
     public function zIncrBy($key, $value, $member,$prefix='')

+ 1 - 1
helper/refill/RefillBase.php

@@ -273,7 +273,7 @@ class RefillBase
             }
 
             $order_success = true;
-            util::incr_order($channel_name,$card_type,$amount,$quality);
+            util::incr_order($channel_name,$card_type,$amount);
             $params = ['order_sn' => $order_sn, 'idcard' => $idcard, 'card_name' => $card_name];
             [$state, $errmsg,$neterr] = $provider->add($card_no, $card_type, $amount, $params);
 

+ 1 - 1
helper/refill/XYZRefillFactory.php

@@ -5,7 +5,7 @@ namespace refill;
 
 require_once(BASE_HELPER_PATH . '/refill/RefillBase.php');
 require_once(BASE_HELPER_PATH . '/refill/ProviderManager.php');
-require_once(BASE_HELPER_PATH . '/refill/IPolicy.php');
+require_once(BASE_HELPER_PATH . '/refill/policy/IPolicy.php');
 require_once(BASE_HELPER_PATH . '/refill/policy/xyz/policy.php');
 
 use Log;

+ 12 - 0
helper/refill/policy/lingzh/chctl.php

@@ -0,0 +1,12 @@
+<?php
+
+
+namespace refill;
+
+
+
+class chctl
+{
+    protected $mSpeedtable;
+
+}

+ 84 - 0
helper/refill/policy/lingzh/ctl_item.php

@@ -0,0 +1,84 @@
+<?php
+
+
+class ctl_item
+{
+    const speed_secs = 60;
+
+    private $mName;
+    private $mCardType;
+    private $mAmount;
+
+
+    private $mMaxSpeed;
+    private $mPrefix;
+    private $mLastReadTime;
+    private $mRecords;
+
+    private $mPriority;
+    private $mStorge;
+    private $mOpened;
+
+    public function __construct($chname,$card_type,$amout,$max_speed,$priority,$storge,$opened)
+    {
+        $this->mPrefix = "speed-{$chname}-{$card_type}-{$amout}-";
+
+        $this->mName = $chname;
+        $this->mCardType = $card_type;
+        $this->mAmount = $amout;
+        $this->mMaxSpeed = $max_speed;
+        $this->mPriority = $priority;
+        $this->mStorge = $storge;
+        $this->mOpened = $opened;
+        $this->mRecords = [];
+
+        $this->load(time() - ctl_item::speed_secs + 1);
+    }
+
+    private function load($start_time)
+    {
+        $ins = Cache::getInstance('cacheredis');
+        $end = time();
+
+        for($time = $start_time; $time <= $end; $time++) {
+            $key = $this->mPrefix . "-{$time}";
+            $val = $ins->get_org($key);
+            $val = $val == false ? 0 : $val;
+            $this->mRecords[$time] = $val;
+        }
+        $this->mLastReadTime = $end;
+
+        $min = $end - ctl_item::speed_secs + 1;
+        $rmkeys = [];
+        foreach ($this->mRecords as $time => $val)
+        {
+            if($time < $min){
+                $rmkeys[] = $time;
+            }
+        }
+
+        foreach ($rmkeys as $key) {
+            unset($this->mRecords[$key]);
+        }
+    }
+
+    public function cur_speed() : int
+    {
+        $this->load($this->mLastReadTime);
+        $speed = array_sum($this->mRecords);
+
+        return $speed;
+    }
+
+    public function speed_overload() : bool {
+        return $this->cur_speed() > $this->mMaxSpeed;
+    }
+
+    public function priority() {
+        return $this->mPriority;
+    }
+
+    public function storge() {
+        return $this->mStorge;
+    }
+}

+ 7 - 0
helper/refill/policy/lingzh/policy.php

@@ -12,6 +12,13 @@ class policy extends ProviderManager implements IPolicy
     const high_times = 5;
     const high_time_out = 30;
 
+    public function find_providers(int $amount, int $card_type,int $quality): array
+    {
+        $providers = parent::find_providers($amount,$card_type,$quality);
+
+
+    }
+
     public function can_retry(array $refill_info, array $order_info): bool
     {
         return false;

+ 0 - 10
helper/refill/policy/lingzh/speed.php

@@ -1,10 +0,0 @@
-<?php
-
-
-namespace refill;
-
-
-class speed
-{
-
-}

+ 0 - 1
helper/refill/policy/lingzh/speed_item.php

@@ -1 +0,0 @@
-<?php

+ 21 - 4
helper/refill/util.php

@@ -171,11 +171,28 @@ class util
         queue\DispatcherClient::instance()->push('notify_mechant',$params);
     }
 
-    public static function incr_order($chname,$card_type,$amout,$quality)
+    public static function incr_order($chname,$card_type,$amout)
     {
-        $time = time();
-        $key = "{$chname}-{$card_type}-{$amout}-{$quality}-{$time}";
         $ins = Cache::getInstance('cacheredis');
-        return $ins->incr($key);
+
+        $sec = time();
+        $min = $sec - $sec % 60;
+
+        $key = "storge-push-{$chname}-{$card_type}-{$amout}-{$min}";
+        $ins->incr($key);
+
+        $key = "speed-{$chname}-{$card_type}-{$amout}-{$sec}";
+        $ins->incr($key);
+    }
+
+    public static function decr_order($chname,$card_type,$amout)
+    {
+        $ins = Cache::getInstance('cacheredis');
+
+        $sec = time();
+        $min = $sec - $sec % 60;
+
+        $key = "storge-pop-{$chname}-{$card_type}-{$amout}-{$min}";
+        $ins->decr($key);
     }
 }

+ 1 - 1
racc/control/lzrefill.php

@@ -143,7 +143,7 @@ class lzrefillControl extends lzbaseControl
         $order_sn = $_GET['ord']; //用户方的订单号,对应数据库中的mch_order
 
         if(empty($order_sn)) {
-            return self::outerr(201,);
+            return self::outerr(201);
         }
 
         $mod_refill = Model('refill_order');

+ 5 - 1
test/TestRedis.php

@@ -35,8 +35,12 @@ class TestRedis extends TestCase
     {
         $key = '100905';
         $ins = Cache::getInstance('cacheredis');
-//        $ins->set_org($key,0);
         $ret = $ins->incr($key);
+        $ret = $ins->incrby($key,6);
+        $ret = $ins->decr($key);
+        $ret = $ins->decrby($key,10);
+        $ret = $ins->get_org($key);
+        $ret = $ins->get_org('yyyyyyxxxx');
     }
 
     public function testzIncr()