rabin há 3 dias atrás
pai
commit
356b1d341c
4 ficheiros alterados com 160 adições e 33 exclusões
  1. 2 2
      boot.php
  2. 131 31
      src/Dever/Helper/Redis.php
  3. 24 0
      src/Dever/Helper/Str.php
  4. 3 0
      src/Workerman/Coroutine.php

+ 2 - 2
boot.php

@@ -38,9 +38,9 @@ class Dever
         $http_worker->count = DEVER_WORKER;
         $http_worker->reloadable = true;
         $http_worker->max_request = 0;
-        $http_worker->onWorkerStart = function() {
+        $http_worker->onWorkerStart = function($worker) {
             self::get(Dever\Project::class)->register();
-            self::call(DEVER_CRON);
+            self::call(DEVER_CRON, $worker->id);
         };
         \Workerman\Worker::runAll();
     }

+ 131 - 31
src/Dever/Helper/Redis.php

@@ -1,9 +1,12 @@
 <?php namespace Dever\Helper;
+
 use Dever\Debug;
+
 class Redis
 {
     private static $handle;
     private static $expire = 3600;
+
     public static function connect()
     {
         if (!self::$handle) {
@@ -11,67 +14,86 @@ class Redis
             if (!$config) {
                 \Dever::error('redis error');
             }
+
             self::$expire = $config['expire'];
             self::$handle = new \Redis;
             self::$handle->pconnect($config["host"], $config["port"]);
-            if (isset($config['password']) && $config['password']) {
+
+            if (!empty($config['password'])) {
                 self::$handle->auth($config['password']);
             }
+
             self::$handle->setOption(\Redis::OPT_READ_TIMEOUT, -1);
         }
         return self::$handle;
     }
+
+    /** -------------------------
+     *  KV 基础操作
+     * ------------------------ */
     public static function get($key)
     {
-        return Debug::add(self::connect()->get($key), 'cache');
+        return self::connect()->get($key);
     }
+
     public static function set($key, $value, $expire = 0)
     {
         self::expire($expire);
         return self::connect()->set($key, $value, self::$expire);
     }
+
+    public static function del($key)
+    {
+        return self::connect()->del($key);
+    }
+
+    /** -------------------------
+     *  分布式锁
+     * ------------------------ */
     public static function lock($key, $value, $expire = 0)
     {
         self::expire($expire);
         return self::connect()->set($key, $value, ['NX', 'EX' => $expire]);
     }
+
     public static function unlock($key, $value)
     {
-        $script = <<< EOF
+        $script = <<<LUA
         if redis.call("get", KEYS[1]) == ARGV[1] then
             return redis.call("del", KEYS[1])
         else
             return 0
         end
-        EOF;
-        return self::connect()->eval($script, [$key, $value], 1);    
+LUA;
+        return self::connect()->eval($script, [$key, $value], 1);
     }
+
+    /** -------------------------
+     *  自增自减
+     * ------------------------ */
     public static function incr($key, $value = false)
     {
-        if ($value) {
-            return self::connect()->incrby($key, $value);
-        }
-        return self::connect()->incr($key);
+        return $value ? self::connect()->incrBy($key, $value) : self::connect()->incr($key);
     }
+
     public static function decr($key, $value = false)
     {
-        if ($value) {
-            return self::connect()->decrby($key, $value);
-        }
-        return self::connect()->decr($key);
+        return $value ? self::connect()->decrBy($key, $value) : self::connect()->decr($key);
     }
+
+    /** -------------------------
+     *  列表队列
+     * ------------------------ */
     public static function push($key, $value)
     {
         return self::connect()->lpush($key, $value);
     }
+
     public static function pop($key)
     {
         $data = self::connect()->brPop($key, 10);
         if ($data) {
-            if (isset($data[1])) {
-                $data = $data[1];
-            }
-            return $data;
+            return $data[1] ?? $data;
         } else {
             $pong = self::connect()->ping();
             if ($pong != '+PONG') {
@@ -81,47 +103,125 @@ class Redis
         }
         return false;
     }
+
     public static function len($key)
     {
-        return self::connect()->llen($key);
+        return self::connect()->lLen($key);
     }
+
+    /** -------------------------
+     *  Hash 操作
+     * ------------------------ */
     public static function hGet($key, $hkey = false)
     {
         if ($hkey) {
-            return Debug::add(self::connect()->hGet($key, $hkey), 'cache');
-        } else {
-            return Debug::add(self::connect()->hGetAll($key), 'cache');
+            return self::connect()->hGet($key, $hkey);
         }
+        return self::connect()->hGetAll($key);
     }
+
     public static function hDel($key, $hkey)
     {
         return self::connect()->hDel($key, $hkey);
     }
+
     public static function hExists($key, $hkey)
     {
         return self::connect()->hExists($key, $hkey);
     }
+
     public static function hKeys($key)
     {
         return self::connect()->hKeys($key);
     }
-    public static function hSet($key, $hkey, $value, $expire = 0)
-    {
-        self::expire($expire);
-        return self::connect()->hKeys($key, $hkey, $value, self::$expire);
-    }
-    public static function delete($key)
-    {
-        return self::connect()->delete($key, 0);
-    }
+
+    public static function hSet($key, $hkey, $value)
+    {
+        $res = self::connect()->hSet($key, $hkey, $value);
+        return $res;
+    }
+
+    public static function hMSet($key, $value)
+    {
+        $res = self::connect()->hMSet($key, $value);
+        return $res;
+    }
+
+    # 原子操作
+    public static function hOper($key, $field, $amount)
+    {
+        $lua = <<<LUA
+    local balance = redis.call("HGET", KEYS[1], KEYS[2])
+    local change = tonumber(ARGV[1])
+
+    if not balance then
+        return -1
+    end
+
+    balance = tonumber(balance)
+    local new_balance = balance + change
+
+    if new_balance < 0 then
+        return 0
+    end
+
+    redis.call("HSET", KEYS[1], KEYS[2], new_balance)
+    return new_balance
+    LUA;
+
+        return self::connect()->eval($lua, [$key, $field, $amount], 2);
+    }
+
+    public static function oper($key, $amount)
+    {
+        $lua = <<<LUA
+    local balance = redis.call("GET", KEYS[1])
+    local change = tonumber(ARGV[1])
+
+    if not balance then
+        return -1   --余额未初始化
+    end
+
+    balance = tonumber(balance)
+    local new_balance = balance + change
+
+    if new_balance < 0 then
+        return 0    --余额不足
+    end
+
+    redis.call("SET", KEYS[1], new_balance)
+    return new_balance
+    LUA;
+
+        return self::connect()->eval($lua, [$key, $amount], 1);
+    }
+
+
+    public static function xAdd($key, $col, $value)
+    {
+        return self::connect()->xAdd($key, $col, $value);
+    }
+
+    public static function xRead($search, $num, $time)
+    {
+        return self::connect()->xRead($search, $num, $time);
+    }
+
+    /** -------------------------
+     *  关闭连接
+     * ------------------------ */
     public static function close()
     {
         return self::connect()->close();
     }
+
+    /** -------------------------
+     *  设置过期
+     * ------------------------ */
     private static function expire($expire)
     {
         if ($expire) {
             self::$expire = $expire;
         }
     }
-}
+}

+ 24 - 0
src/Dever/Helper/Str.php

@@ -53,6 +53,30 @@ class Str
             }
         }
     }
+    public static function orderNum($id)
+    {
+        // 时间戳(到秒)
+        $time = date('YmdHis');
+        // 两位随机(36进制,36^2 = 1296)
+        $rand = self::base36(mt_rand(0, 1295));
+        // 转为大写,固定两位
+        $rand = str_pad($rand, 2, '0', STR_PAD_LEFT);
+        // ID 的 36 进制
+        $id36 = self::base36($id);
+        return $time . $rand . $id36;
+    }
+    public static function base36($num)
+    {
+        $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
+        $result = '';
+
+        while ($num > 0) {
+            $result = $chars[$num % 36] . $result;
+            $num = intval($num / 36);
+        }
+
+        return $result ?: '0';
+    }
     public static function code($num = 4)
     {
         $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

+ 3 - 0
src/Workerman/Coroutine.php

@@ -0,0 +1,3 @@
+<?php
+
+require_once __DIR__ . '/Coroutine/Coroutine.php';