Commit 7f2b8d27 by 杨树贤

完成商品兑换配置增删改查的redis缓存处理

parent 5aa8c0fb
...@@ -41,8 +41,11 @@ class ExchangeSettingsController extends Controller ...@@ -41,8 +41,11 @@ class ExchangeSettingsController extends Controller
{ {
$page = $request->has('page') ? $request->page : self::DEFAULT_PAGE; $page = $request->has('page') ? $request->page : self::DEFAULT_PAGE;
$pageSize = $request->has('page_size') ? $request->page_size : self::DEFAULT_PAGE_SIZE; $pageSize = $request->has('page_size') ? $request->page_size : self::DEFAULT_PAGE_SIZE;
if ($request->has('is_api') && $request->is_api) {
$result = $exchangeSetting->getExchangeSettingListForApi();
} else {
$result = $exchangeSetting->getExchangeSettingList($page, $pageSize, $filter); $result = $exchangeSetting->getExchangeSettingList($page, $pageSize, $filter);
}
return $this->Export(0, 'ok', $result); return $this->Export(0, 'ok', $result);
} }
......
...@@ -5,6 +5,7 @@ namespace App\Models; ...@@ -5,6 +5,7 @@ namespace App\Models;
use App\Http\Filters\QueryFilter; use App\Http\Filters\QueryFilter;
use Common\Model\RedisModel;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
...@@ -33,20 +34,149 @@ class ExchangeSetting extends Model ...@@ -33,20 +34,149 @@ class ExchangeSetting extends Model
return ['data' => $settings, 'count' => $count]; return ['data' => $settings, 'count' => $count];
} }
public function getExchangeSettingListForApi()
{
//先从redis里面查询是否有列表,没有的话从MySQL里面取出并且放入redis
$redis = new RedisModel();
$settings = $this->getExchangeSettingsFromRedis();
$count = 0;
if (!$settings) {
$result = $this->addExchangeSettingListToRedis();
if (!$result) {
ErrorLog(ErrorCode(19, 5), '写入商品配置列表到Redis失败');
$count = ExchangeSetting::where('status', 1)->count();
$settings = $this->getExchangeSettingsFromRedis();
}
} else {
$count = $redis->hlen('ic_welfare_integrals');
}
$settings = arraySequence($settings, 'id', 'SORT_ASC');
return ['data' => array_values($settings), 'count' => $count];
}
//从redis查询出商品配置项
public function getExchangeSettingsFromRedis()
{
$redis = new RedisModel();
$settings = $redis->hgetall('ic_exchange_settings');
$settings = array_map(function ($value) {
return json_decode($value, true);
}, $settings);
return $settings;
}
//添加商品配置项到Redis
public function addExchangeSettingListToRedis()
{
$settings = ExchangeSetting::where('status', 1)
->get()->toArray();
$redis = new RedisModel();
//转换数据存储到Redis的哈希类型
$data = [];
foreach ($settings as $key => $setting) {
$map = [
'id' => $setting['id'],
'name' => $setting['name'],
'amount' => $setting['amount'],
'type' => $setting['type'],
'status' => $setting['status'],
];
$data[$setting['id']] = json_encode($map);
}
//这里如果没有数据放到缓存了,直接删除key
if ($data) {
$redis->hmset('ic_exchange_settings', $data);
} else {
$redis->del('ic_exchange_settings');
}
}
public function addExchangeSetting($data = []) public function addExchangeSetting($data = [])
{ {
$result = DB::table('exchange_settings')->insert($data); $result = DB::transaction(function () use ($data) {
$id = DB::table('exchange_settings')->insertGetId($data);
$data['id'] = $id;
if (!$id) {
return false;
}
$this->addIntegralToRedis($id, $data);
return true;
}, 5);
return $result; return $result;
} }
//添加单个商品兑换配置到redis
public function addIntegralToRedis($id, $setting)
{
$redis = new RedisModel();
$data = [
'id' => $setting['id'],
'name' => $setting['name'],
'amount' => $setting['amount'],
'type' => $setting['type'],
'status' => $setting['status'],
];
//添加的时候,状态是可用的才会添加到redis
if ($setting['status']) {
$redis->hset('ic_exchange_settings', $id, json_encode($data));
}
}
public function updateExchangeSetting($id, $data = []) public function updateExchangeSetting($id, $data = [])
{ {
$result = DB::table('exchange_settings')->where('id', $id)->update($data); $result = DB::transaction(function () use ($id, $data) {
$result = DB::table('exchange_settings')->where('id', $id)
->update($data);
if (!$result) {
return false;
}
if ($result) {
$this->changeExchangeSettingFromRedis([$id]);
}
return true;
}, 5);
return $result; return $result;
} }
//因为修改状态会涉及到不少信息的更变,所以直接重新更新有变化的数据(根据ids)
public function changeExchangeSettingFromRedis($ids = [])
{
if ($ids) {
$changedSettings = DB::table('exchange_settings')
->whereIn('id', $ids)->get()->toArray();
$needDelete = $needUpdate = [];
foreach ($changedSettings as $key => $value) {
//判断是不是禁用,如果是禁用,就从redis里面删除
if (!$value->status) {
$needDelete[] = $value->id;
} else {
$needUpdate[$value->id] = json_encode($value);
}
}
$redis = new RedisModel();
if ($needDelete) {
$redis->hdel('ic_exchange_settings', $needDelete);
}
if ($needUpdate) {
$redis->hmset('ic_exchange_settings', $needUpdate);
}
}
return true;
}
public function deleteExchangeSetting($ids = []) public function deleteExchangeSetting($ids = [])
{ {
$res = DB::table('exchange_settings')->whereIn('id', $ids)->delete(); $res = DB::table('exchange_settings')->whereIn('id', $ids)->delete();
...@@ -63,11 +193,17 @@ class ExchangeSetting extends Model ...@@ -63,11 +193,17 @@ class ExchangeSetting extends Model
*/ */
public function batchUpdateStatus($status, $ids = []) public function batchUpdateStatus($status, $ids = [])
{ {
$res = DB::table('exchange_settings') $result = DB::transaction(function () use ($ids, $status) {
->whereIn('id', $ids) $result = DB::table('exchange_settings')->whereIn('id', $ids)->update(['status' => $status]);
->update(['status' => (int)$status, 'update_time' => time()]); if (!$result) {
return false;
}
$this->changeExchangeSettingFromRedis($ids);
return $res; return true;
}, 5);
return $result;
} }
......
common @ 6a492b76
Subproject commit f196a4b6f17e81c0e6df02710896f08973e89391 Subproject commit 6a492b76f3938205ed9d94cd12b81d66feea657d
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