|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace Module\Vendor\Provider\LazyValue; |
| 5 | + |
| 6 | + |
| 7 | +use ModStart\Core\Dao\ModelUtil; |
| 8 | +use ModStart\Core\Exception\BizException; |
| 9 | +use ModStart\Core\Input\Response; |
| 10 | +use ModStart\Core\Util\SerializeUtil; |
| 11 | +use Module\Vendor\Model\LazyValue; |
| 12 | +use Module\Vendor\Provider\BizTrait; |
| 13 | + |
| 14 | +/** |
| 15 | + * @method static AbstractLazyValueBiz[] listAll() |
| 16 | + * @method static AbstractLazyValueBiz getByName($name) |
| 17 | + */ |
| 18 | +class LazyValueBiz |
| 19 | +{ |
| 20 | + use BizTrait; |
| 21 | + |
| 22 | + public static function dispatch($biz, $param) |
| 23 | + { |
| 24 | + $job = new LazyValueJob(); |
| 25 | + $job->key = $biz; |
| 26 | + $job->param = $param; |
| 27 | + $job->onQueue('LazyValue'); |
| 28 | + app('Illuminate\Contracts\Bus\Dispatcher')->dispatch($job); |
| 29 | + } |
| 30 | + |
| 31 | + public static function dispatchRefresh($biz, $param) |
| 32 | + { |
| 33 | + $job = new LazyValueJob(); |
| 34 | + $job->key = $biz; |
| 35 | + $job->param = $param; |
| 36 | + $job->onQueue('LazyValueRefresh'); |
| 37 | + app('Illuminate\Contracts\Bus\Dispatcher')->dispatch($job); |
| 38 | + } |
| 39 | + |
| 40 | + public static function get($biz, $param = [], $expireLife = 86400) |
| 41 | + { |
| 42 | + $bizer = self::getByName($biz); |
| 43 | + BizException::throwsIfEmpty("BizNotFound", $bizer); |
| 44 | + $where = [ |
| 45 | + 'key' => $bizer->name(), |
| 46 | + 'param' => SerializeUtil::jsonEncode($param) |
| 47 | + ]; |
| 48 | + $record = ModelUtil::get(LazyValue::class, $where); |
| 49 | + if (empty($record)) { |
| 50 | + ModelUtil::insert(LazyValue::class, array_merge([ |
| 51 | + 'expire' => time() + $bizer->cacheSeconds(), |
| 52 | + 'lifeExpire' => time() + $expireLife, |
| 53 | + 'cacheSeconds' => $bizer->cacheSeconds(), |
| 54 | + 'value' => SerializeUtil::jsonEncode($bizer->defaultValue()), |
| 55 | + ], $where)); |
| 56 | + LazyValueBiz::dispatch($bizer->name(), $param); |
| 57 | + return [ |
| 58 | + 'status' => 'running', |
| 59 | + 'value' => $bizer->defaultValue(), |
| 60 | + ]; |
| 61 | + } |
| 62 | + if ($record['expire'] < time()) { |
| 63 | + LazyValueBiz::dispatchRefresh($bizer->name(), $param); |
| 64 | + } |
| 65 | + $value = @json_decode($record['value'], true); |
| 66 | + if (empty($value)) { |
| 67 | + return [ |
| 68 | + 'status' => 'running', |
| 69 | + 'value' => $bizer->defaultValue(), |
| 70 | + ]; |
| 71 | + } |
| 72 | + return [ |
| 73 | + 'status' => 'finish', |
| 74 | + 'value' => $value, |
| 75 | + ]; |
| 76 | + } |
| 77 | + |
| 78 | + private static function status(&$value) |
| 79 | + { |
| 80 | + foreach ($value as $v) { |
| 81 | + if (null === $v) { |
| 82 | + return 'running'; |
| 83 | + } |
| 84 | + } |
| 85 | + return 'finish'; |
| 86 | + } |
| 87 | + |
| 88 | + public static function fetch($bizParamList = []) |
| 89 | + { |
| 90 | + $status = 'finish'; |
| 91 | + $value = []; |
| 92 | + foreach ($bizParamList as $k => $v) { |
| 93 | + if (!isset($v['param'])) { |
| 94 | + $v['param'] = []; |
| 95 | + } |
| 96 | + $ret = LazyValueBiz::get($v['biz'], $v['param']); |
| 97 | + if ($ret['status'] == 'running') { |
| 98 | + $status = 'running'; |
| 99 | + } |
| 100 | + $value[$k] = $ret; |
| 101 | + } |
| 102 | + return Response::generate(0, 'ok', [ |
| 103 | + 'status' => $status, |
| 104 | + 'value' => $value, |
| 105 | + ]); |
| 106 | + } |
| 107 | +} |
0 commit comments