redis 如何保证接口幂等性?

一个分佣系统中,根据各种订单进行下单后分佣,涉及到钱,如果重复分佣后果可想而知。

产景:前端多次请求同一个接口,主从数据有延迟,造成数据重复插入

 

处理方案:

        //方案①
        $come_in_times = \Yii::$app->redis->incr($data['order_id'].$data['type']);
        if($come_in_times > 1){
            \Yii::$app->redis->del($data['order_id'].$data['type']);
            return false;
        }
        //处理业务再删redis中的key
        \Yii::$app->redis->del($data['order_id'].$data['type']);
       

       
        //方案②
        if(\Yii::$app->redis->setnx($data['order_id'].$data['type'],1)){
        if(!$come_in_times){
            \Yii::$app->redis->del($data['order_id'].$data['type']);
            return false;
        }
        //处理业务再删redis中的key
        \Yii::$app->redis->del($data['order_id'].$data['type']);







        //方案③强制读主库https://segmentfault.com/a/1190000038304022?sort=newest
       $data = [
            'order_id'=>303130,
            'type'=>0,
        ];
        $users = ShareOrder::getDb()->useMaster(function($db) use ($data) {
            /** @var $db \yii\db\Connection */
            return $db->createCommand("SELECT * FROM " . ShareOrder::tableName() . " where `order_id` =:order_id  AND `type`=:type", [
                ':order_id' => $data['order_id'],
                ':type' => $data['type'],
            ])->queryOne(\PDO::FETCH_OBJ);
        });
        print_r(gettype($users));
//重复数据分佣数据
select order_no,count(*) as count from xcxmall_share_order group by order_no having count(*) > 1;

//除了已取消数据分佣数据
select id,order_id,order_no from xcxmall_share_order where order_no in (select order_no from xcxmall_share_order group by order_no having count(*) > 1) and order_status < 4;

//①先删除分佣详情表数据。!!!
select * from xcxmall_share_order_detail where share_order_id in (select id from xcxmall_share_order where order_no in (select order_no from xcxmall_share_order group by order_no having count(*) > 1) and order_status < 4 and id>=470589 GROUP BY order_id) GROUP BY order_id;

//②再删今天下午上线数据。
select * from xcxmall_share_order where order_no in (select order_no from xcxmall_share_order group by order_no having count(*) > 1) and order_status < 4 and id>=470589 GROUP BY order_id;


470589        516726        20220615145749114012
470679        516800        20220615162900569265
471077        517201        20220615205521880355
20220614号的数据要删掉一条,20220615145749114012 开始的,要删掉一条的同时删掉 xcxmall_share_order_detail中对应的一条。

select * from xcxmall_order where order_no='20200416175752802575'

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值