zoukankan      html  css  js  c++  java
  • Redis 扛 Mysq 并发方案小记

    前些日子的业务系统出现了并发现象, 我们的订单表由于对做对第三方开放的接口平台 而增加了 第三方的订单号 而这个订单号只能保证在该商户下唯一, 整个列中并不唯一, 且我们之前的订单由于没 第三方订单号 而为空, 因此没法建组合唯一索引

    利用 Redis 的 setnx 特性可以构建一个锁机制, 以下是实现的部分片段

     public static function Block($key, $action = TRUE, $options = [])
        {
            if(!defined('MERCHANT_TOKEN')) return FALSE;
            $res = FALSE;
            $redis = Yii::$app->redis;
            // 前缀
            $prefix = isset($options['prefix']) ? (string)$options['prefix'] : 'MOSN';
            // 过期时间
            $expire = isset($options['expire']) ? (integer)$options['expire'] : 300;
            $key = vsprintf('%s:%s:%s', [$prefix, MERCHANT_TOKEN, $key]);
            if ($action === TRUE){
                $res = $redis->executeCommand('SETNX', [$key, time() + $expire]);
                if ($res){
                    $redis->executeCommand('EXPIRE', [$key, $expire]);
                }
            }else if ($action === FALSE){
                $res = $redis->executeCommand('DEL', [$key]);
            }
            return $res ? TRUE : FALSE;
        }
    
    

    优化后

     public static function Block($key, $action = TRUE, $options = [])
        {
            if(!defined('MERCHANT_TOKEN')) return FALSE;
            $res = FALSE;
            $redis = Yii::$app->redis;
            // 前缀
            $prefix = isset($options['prefix']) ? (string)$options['prefix'] : 'MOSN';
            // 过期时间
            $expire = isset($options['expire']) ? (integer)$options['expire'] : 300;
            $key = vsprintf('%s:%s:%s', [$prefix, MERCHANT_TOKEN, $key]);
            $timeNow = time();
            if ($action === TRUE){
                if ($redis->executeCommand('SETNX', [$key, $timeNow + $expire]) || $redis->executeCommand('GET', [$key]) && $redis->executeCommand('GETSET', [$key, $timeNow + $expire + 1]) < $timeNow){
                    $res = TRUE;
                }else{
                    $res = FALSE;
                }
            }else if ($action === FALSE){
                $redis->executeCommand('SET', [$key, $timeNow]);
                $res = TRUE;
            }
            $redis->executeCommand('EXPIRE', [$key, $expire]);
            return $res;
        }
    
  • 相关阅读:
    通过WCF引用ListData.svc查询,更新数据
    SharePoint2010 BCS搜索结果的标题
    多个Finder方法的外部内容类型
    SharePoint 2010 中的BCS身份验证模式
    使用Visual Studio工作流发布SharePoint网页
    PerformancePoint Services故障排除
    使用SharePoint 2010模式对话框
    在自定义字段类型的CAML中使用AJAX jQuery
    扩展Visual Studio 2010服务器资源管理器中的SharePoint结点
    BCS配置文件页、爬网与搜索
  • 原文地址:https://www.cnblogs.com/dongyanglv/p/6767476.html
Copyright © 2011-2022 走看看