Commit 7f0c9062 by 杨树贤

redis错误可以被全局函数捕获,这边不需要手动去捕获

parent d2f95f4a
Showing with 12 additions and 113 deletions
......@@ -7,7 +7,7 @@ namespace App\Models;
use App\Http\Filters\QueryFilter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\DocBlock\Tags\Reference\Fqsen;
use Common\Model\RedisModel;
//活动红包模型
class Integral extends Model
......@@ -53,7 +53,7 @@ class Integral extends Model
} else {
$count = $redis->hlen('ic_welfare_integrals');
}
$integrals = arraySequence($integrals,'id','SORT_ASC');
$integrals = arraySequence($integrals, 'id', 'SORT_ASC');
return ['data' => array_values($integrals), 'count' => $count];
}
......@@ -84,12 +84,10 @@ class Integral extends Model
//这里如果没有数据放到缓存了,直接删除key
if ($data) {
$result = $redis->hmset('ic_welfare_integrals', $data);
$redis->hmset('ic_welfare_integrals', $data);
} else {
$result = $redis->del('ic_welfare_integrals');
$redis->del('ic_welfare_integrals');
}
return $result;
}
//添加单个红包
......@@ -102,10 +100,7 @@ class Integral extends Model
if (!$id) {
return false;
}
$result = $this->addIntegralToRedis($id, $data);
if ($result === false) {
throw new \Exception('新增红包活动缓存数据失败', ErrorCode(19, 5));
}
$this->addIntegralToRedis($id, $data);
return true;
}, 5);
......@@ -117,9 +112,7 @@ class Integral extends Model
public function addIntegralToRedis($id, $data)
{
$redis = new RedisModel();
$result = $redis->hset('ic_welfare_integrals', $id, json_encode($data));
return $result;
$redis->hset('ic_welfare_integrals', $id, json_encode($data));
}
//更新单个红包活动
......@@ -133,13 +126,10 @@ class Integral extends Model
}
if ($result) {
$result = $this->changeIntegralsFromRedis([$id]);
if ($result === false) {
throw new \Exception('更新红包活动缓存数据失败', ErrorCode(20, 5));
}
$this->changeIntegralsFromRedis([$id]);
}
return $result ? true : false;
return true;
}, 5);
return $result;
......@@ -150,14 +140,11 @@ class Integral extends Model
{
$redis = new RedisModel();
$integral = $redis->hget('ic_welfare_integrals', $id);
$result = false;
if ($integral) {
//为什么用array_merge,因为右边的数组的键值会覆盖左边的对应值
$data = array_merge(json_decode($integral, true), $data);
$result = $redis->hset('ic_welfare_integrals', $id, json_encode($data));
$redis->hset('ic_welfare_integrals', $id, json_encode($data));
}
return $result;
}
......@@ -169,10 +156,7 @@ class Integral extends Model
if (!$result) {
return false;
}
$result = $this->changeIntegralsFromRedis($ids);
if ($result === false) {
throw new \Exception('更新部分红包信息到redis失败', ErrorCode(21, 5));
}
$this->changeIntegralsFromRedis($ids);
return true;
}, 5);
......@@ -199,16 +183,10 @@ class Integral extends Model
$redis = new RedisModel();
if ($needDelete) {
$result = $redis->hdel('ic_welfare_integrals', $needDelete);
if ($result === false) {
return false;
}
$redis->hdel('ic_welfare_integrals', $needDelete);
}
if ($needUpdate) {
$result = $redis->hmset('ic_welfare_integrals', $needUpdate);
if ($result === false) {
return false;
}
$redis->hmset('ic_welfare_integrals', $needUpdate);
}
return true;
......
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;
class RedisModel extends Model
{
const WRITE_CONNECT_METHOD = ['set', 'del', 'rpush','lpush', 'expire', 'hset', 'hmset', 'hdel','hsetnx','hincrby'];
private $read = [];
private $write = [];
public function __construct($ConnectWrite = 'default', $ConnectRead = 'read')
{
parent::__construct();
$this->read = Redis::connection($ConnectRead);
$this->write = Redis::connection($ConnectWrite);
}
public function __call($method, $args)
{
$cls = &$this;
$tmp = function($method, $args) use ($cls) {
if (strpos($method, 'pipeline_')) {//使用管道
$method = substr($method, 9);
if (in_array($method, $cls::WRITE_CONNECT_METHOD)) {
return $cls->write->pipeline(function ($pipe) use ($args) {
foreach ($args as $arg) {
$pipe->$method(...$arg);
}
});
} else {
try {
return $cls->read->pipeline(function ($pipe) use ($args) {
foreach ($args as $arg) {
$pipe->$method(...$arg);
}
});
} catch (ConnectionException $e) {
return $cls->write->pipeline(function ($pipe) use ($args) {
foreach ($args as $arg) {
$pipe->$method(...$arg);
}
});
}
}
} else {
if (in_array($method, $cls::WRITE_CONNECT_METHOD)) {
return $cls->write->$method(...$args);
} else {
try {
return $cls->read->$method(...$args);
} catch (ConnectionException $e) {
return $cls->write->$method(...$args);
}
}
}
};
try {
return $tmp($method, $args);
} catch (\Exception $e) {
dd($e);
return false;
}
// if (strpos($method, 'catch_') === 0) {
// $method = substr($method, 6);
// try {
// return $tmp($method, $args);
// } catch (\Exception $e) {
// return null;
// }
// } else {
// return $tmp($method, $args);
// }
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment